map grid
This commit is contained in:
parent
2a7324b133
commit
bb7d3c48ea
6 changed files with 98 additions and 44 deletions
|
@ -75,11 +75,23 @@ pub enum Response {
|
|||
Login(login::LoginResult),
|
||||
Message(ChatMessage),
|
||||
GetChatHistory(Vec<ChatMessage>),
|
||||
ShowScene { scene: usize, tokens: Vec<SpawnToken>, background: Option<String> },
|
||||
MoveToken { token_id: usize, x: f32, y: f32 },
|
||||
ShowScene {
|
||||
scene: usize,
|
||||
tokens: Vec<SpawnToken>,
|
||||
background: Option<String>,
|
||||
grid_cell_size: Option<f32>,
|
||||
grid_offset: Option<[f32; 2]>,
|
||||
},
|
||||
MoveToken {
|
||||
token_id: usize,
|
||||
x: f32,
|
||||
y: f32,
|
||||
},
|
||||
SpawnToken(SpawnToken),
|
||||
CharacterCreated(usize),
|
||||
Quit { id: String },
|
||||
Quit {
|
||||
id: String,
|
||||
},
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ use character_sheet::{AccessLevel, Character, CharacterShort, EntryType};
|
|||
use scene::{Party, Scene, TokenInfo};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::game::scene::Map;
|
||||
|
||||
pub mod character_sheet;
|
||||
pub mod chat_message;
|
||||
pub mod entry;
|
||||
|
@ -27,7 +29,7 @@ pub trait GameImpl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::
|
|||
fn current_scene(&self) -> usize;
|
||||
/// Gets the map background (file path)
|
||||
fn scene_characters(&self, scene: usize, character_id: usize) -> Option<Vec<CharacterShort>>;
|
||||
fn scene_map(&self, scene_id: usize) -> Option<String>;
|
||||
fn scene_map(&self, scene_id: usize) -> Option<&Map>;
|
||||
fn create_token(&mut self, scene_id: usize, character: String, img_source: String, x: f32, y: f32) -> usize;
|
||||
fn move_token(&mut self, scene_id: usize, token_id: usize, x: f32, y: f32) -> bool;
|
||||
fn token_info(&self, scene: usize, token_id: usize) -> Option<&TokenInfo>;
|
||||
|
@ -70,6 +72,8 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
|||
Scene {
|
||||
map: Some(scene::Map {
|
||||
background: "assets/pf2r/maps/testmap.jpg".to_string(),
|
||||
grid_cell_size: 150.0,
|
||||
grid_offset: [80.0, 35.0],
|
||||
tokens,
|
||||
}),
|
||||
characters: vec![(0, Party(true))],
|
||||
|
@ -183,11 +187,8 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
|||
})
|
||||
}
|
||||
|
||||
fn scene_map(&self, scene_id: usize) -> Option<String> {
|
||||
self.scenes
|
||||
.get(&scene_id)
|
||||
.map(|s| s.map.as_ref().map(|m| m.background.clone()))
|
||||
.flatten()
|
||||
fn scene_map(&self, scene_id: usize) -> Option<&Map> {
|
||||
self.scenes.get(&scene_id).map(|s| s.map.as_ref()).flatten()
|
||||
}
|
||||
|
||||
fn current_scene(&self) -> usize {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Scene {
|
||||
|
@ -13,6 +13,10 @@ pub struct Scene {
|
|||
pub struct Map {
|
||||
/// Image source for the background of the map
|
||||
pub background: String,
|
||||
// Cell size of the map grid
|
||||
pub grid_cell_size: f32,
|
||||
// Grid offset from top left, [x, y]
|
||||
pub grid_offset: [f32; 2],
|
||||
/// Tokens in the current map (should be of characters), maps from token_id to its info
|
||||
pub tokens: HashMap<usize, TokenInfo>,
|
||||
}
|
||||
|
@ -23,7 +27,7 @@ pub struct TokenInfo {
|
|||
pub character: String,
|
||||
/// Token image source, as path relative to the data directory
|
||||
pub img_source: String,
|
||||
// x, y are floats to allow 'free movement'
|
||||
// x, y are floats to allow 'free movement'
|
||||
/// X position, in grid slots units (integers are grid aligned)
|
||||
pub x: f32,
|
||||
/// Y position, in grid slots units (integers are grid aligned)
|
||||
|
@ -37,4 +41,4 @@ impl Party {
|
|||
pub fn can_see(&self, other: Party) -> bool {
|
||||
!self.0 || other.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -171,12 +171,15 @@ impl GameServer {
|
|||
img: info.img_source.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let map = self.game.scene_map(scene);
|
||||
_ = broadcast.send((
|
||||
Some(id.clone()),
|
||||
api::Response::ShowScene {
|
||||
scene: scene,
|
||||
tokens: scene_tokens,
|
||||
background: self.game.scene_map(scene),
|
||||
background: map.map(|m| m.background.clone()),
|
||||
grid_cell_size: map.map(|m| m.grid_cell_size),
|
||||
grid_offset: map.map(|m| m.grid_offset.clone()),
|
||||
},
|
||||
));
|
||||
}
|
||||
|
@ -190,7 +193,12 @@ impl GameServer {
|
|||
let token_id = self.game.create_token(map_id, character, img_path.clone(), x, y);
|
||||
_ = broadcast.send((
|
||||
Some(id.clone()),
|
||||
api::Response::SpawnToken(SpawnToken { token_id, x, y, img: img_path.clone() }),
|
||||
api::Response::SpawnToken(SpawnToken {
|
||||
token_id,
|
||||
x,
|
||||
y,
|
||||
img: img_path.clone(),
|
||||
}),
|
||||
));
|
||||
}
|
||||
api::Request::MoveToken { token_id, x, y } => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue