Skip to content

Commit

Permalink
🚩 wip: team & groups
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Jan 20, 2024
1 parent 7193e31 commit 6ef4629
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
18 changes: 12 additions & 6 deletions backend/src/game/players.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ pub struct Player {
id: u64,
x: i16,
y: i16,
team: Option<i8>,
class: Option<String>,
groups: Option<Vec<String>>,
}

impl Player {
fn new(x: i16, y: i16) -> Self {
fn new(x: i16, y: i16, team: Option<i8>, class: Option<String>, groups: Option<Vec<String>>) -> Self {
static mut NEXT_ID: u64 = 0;

// Use unsafe block to increment and assign the ID
Expand All @@ -20,7 +23,7 @@ impl Player {
current_id
};

Player { id, x, y}
Player { id, x, y, team, class, groups }
}
}

Expand All @@ -30,22 +33,22 @@ pub struct Players {
players_map: HashMap<u64, Player>,
}

impl Players {
pub fn new() -> Self {
impl Players { pub fn new() -> Self {
Players {
players_map: HashMap::new(),
}
}

pub fn add_player(&mut self, x: i16, y: i16) {
let player = Player::new(x, y);
pub fn add_player(&mut self, x: i16, y: i16, team: Option<i8>, class: Option<String>, groups: Option<Vec<String>>) {
let player = Player::new(x, y, team, class, groups);
self.players_map.insert(player.id, player);
}

pub fn remove_player(&mut self, id: u64) {
self.players_map.remove(&id);
}

#[allow(dead_code)]
pub fn update_player(&mut self, id: u64, new_x: i16, new_y: i16) {
if let Some(player) = self.players_map.get_mut(&id) {
player.x = new_x;
Expand Down Expand Up @@ -80,6 +83,9 @@ impl Players {
id,
x: player.x.clone(),
y: player.y.clone(),
team: player.team.clone(),
class: player.class.clone(),
groups: player.groups.clone(),
})
.collect();
// Serialize the Vec<Player> to a JSON string
Expand Down
7 changes: 3 additions & 4 deletions backend/src/network/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ pub fn ws_connect(
) {
println!("A client connected with id #{}.", client_id);
clients.insert(client_id, responder.clone());
clients.get(&client_id).unwrap().send(Message::Text(
format!("client::id::#{}", client_id)
));
if let Some((x, y)) = get_coordinates(get_spawn()) {
players.add_player(x, y);
let team: i8 = client_id.try_into().unwrap(); // Convert to 8 bits integer
players.add_player(x, y, Some(team), Some(String::from("Default")), Some(Vec::new()));
update_players(clients, players);
responder.send(Message::Text(String::from(format!("client::id::#{:?}", client_id))));
} else {
responder.send(Message::Text(String::from("Error: could not perform connection due to bad Cooordinates")));
}
Expand Down
3 changes: 2 additions & 1 deletion backend/src/network/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn get_players(text: String) -> Option<Vec<Player>> {
Some(players)
},
Err(e) => {
println!("players: {:?}", e);
println!("Error while handling player::move JSON: {:?}", e);
None
},
}
Expand All @@ -25,6 +25,7 @@ pub fn get_players(text: String) -> Option<Vec<Player>> {
}
}

#[allow(dead_code)]
pub fn get_id(text: String) -> Option<u64> {
let regex = regex::Regex::new(r"#(\d+)").unwrap();
if let Some(captures) = regex.captures(&text) {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Warfrost extends Phaser.Scene {
// Player socket event listener
this.socket.on("players::update", this);

// Create or Update New Players!
this?.playersData?.forEach((player: Models.PlayerData) => {
// If player already exists, update its position
if (this.players[player.id]) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/network/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Socket {
return;
}
this.messageQueue.push(this.message);
console.log(event.data);
console.log(`Back-end: ${event.data}`);
};

// Handle WebSocket connection close event
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/warfrost/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Player extends Phaser.GameObjects.Sprite {
body: Phaser.Physics.Arcade.Body;

public id : number;
public team : number;
private speed : number = 100; // TODO: Acceleration too
private health : number = 100;
private position : Phaser.Math.Vector2;
public player : Phaser.GameObjects.Sprite;
private WF : Warfrost;

Expand All @@ -23,6 +23,7 @@ class Player extends Phaser.GameObjects.Sprite {
this.setDepth(1);
this.setData('id', player.id);
this.setData('selected', false);
this.setData('team', WF.clientId);

// GLOBAL STUFF
this.scene.physics.world.enableBody(this);
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/warfrost/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import Warfrost from '../game';

export class Selection {
private graphics: Phaser.GameObjects.Graphics;
private scene: Phaser.Scene;
private scene: Warfrost;
private isSelecting: boolean;
private startPos: Phaser.Math.Vector2;
private selectionRect: Phaser.Geom.Rectangle;

constructor(scene: Phaser.Scene) {
constructor(scene: Warfrost) {
this.scene = scene;
this.graphics = this.scene.add.graphics();
this.graphics.setDepth(2);
Expand Down Expand Up @@ -45,6 +45,7 @@ export class Selection {
// Check for spriting collision with selection zone
if (Phaser.Geom.Intersects.RectangleToRectangle(player.getBounds(), this.selectionRect)) {
if (isSelected) return;
if (this.scene.clientId != player.getData('team')) return;
player.setTint(0x00ff00);
player.setData('selected', true);
} else {
Expand Down

0 comments on commit 6ef4629

Please sign in to comment.