replace test tokens and add current scene function to game
This commit is contained in:
parent
2f95b1159d
commit
f59c06c348
4 changed files with 72 additions and 39 deletions
BIN
assets/pf2r/tokens/adams.png
Normal file
BIN
assets/pf2r/tokens/adams.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 100 KiB |
Binary file not shown.
Before Width: | Height: | Size: 150 KiB |
BIN
assets/pf2r/tokens/tim.png
Normal file
BIN
assets/pf2r/tokens/tim.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 88 KiB |
111
src/game/mod.rs
111
src/game/mod.rs
|
@ -10,7 +10,8 @@ pub mod chat_message;
|
|||
pub mod entry;
|
||||
pub mod scene;
|
||||
|
||||
pub trait GameImpl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Serialize + Deserialize<'a>> {
|
||||
pub trait GameImpl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Serialize + Deserialize<'a>>
|
||||
{
|
||||
/// Creates a new game
|
||||
fn new() -> Self;
|
||||
|
||||
|
@ -23,6 +24,7 @@ pub trait GameImpl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::
|
|||
// Scenes
|
||||
/// the list of available scenes
|
||||
fn scenes(&self) -> Vec<usize>;
|
||||
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>;
|
||||
|
@ -37,17 +39,47 @@ pub struct Game<C: Character<A> + Serialize, A: entry::GameEntry + Serialize> {
|
|||
_a: PhantomData<A>,
|
||||
characters: Vec<(C, CharacterInfo)>,
|
||||
scenes: HashMap<usize, Scene>,
|
||||
current_scene: usize,
|
||||
}
|
||||
impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Serialize + Deserialize<'a>> GameImpl<'a, C, A> for Game<C, A> {
|
||||
impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Serialize + Deserialize<'a>>
|
||||
GameImpl<'a, C, A> for Game<C, A>
|
||||
{
|
||||
fn new() -> Self {
|
||||
let mut tokens = HashMap::new();
|
||||
tokens.insert(0, TokenInfo { character: "bart".to_string(), img_source: "assets/pf2r/tokens/louise.jpg".to_string(), x: 2.0, y: 2.0 });
|
||||
tokens.insert(
|
||||
0,
|
||||
TokenInfo {
|
||||
character: "Adams".to_string(),
|
||||
img_source: "assets/pf2r/tokens/adams.png".to_string(),
|
||||
x: 2.0,
|
||||
y: 2.0,
|
||||
},
|
||||
);
|
||||
tokens.insert(
|
||||
1,
|
||||
TokenInfo {
|
||||
character: "Tim".to_string(),
|
||||
img_source: "assets/pf2r/tokens/tim.png".to_string(),
|
||||
x: 0.0,
|
||||
y: 2.0,
|
||||
},
|
||||
);
|
||||
let mut scenes = HashMap::new();
|
||||
scenes.insert(0, Scene { map: None, characters: vec![(0, Party(true))] });
|
||||
scenes.insert(
|
||||
0,
|
||||
Scene {
|
||||
map: Some(scene::Map {
|
||||
background: String::new(),
|
||||
tokens,
|
||||
}),
|
||||
characters: vec![(0, Party(true))],
|
||||
},
|
||||
);
|
||||
Self {
|
||||
_a: PhantomData,
|
||||
characters: Vec::new(),
|
||||
scenes,
|
||||
current_scene: 0,
|
||||
}
|
||||
}
|
||||
fn create_character(&mut self) -> usize {
|
||||
|
@ -55,7 +87,8 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
|||
self.characters.len() - 1
|
||||
}
|
||||
fn move_token(&mut self, scene: usize, token_id: usize, x: f32, y: f32) -> bool {
|
||||
let token = self.scenes
|
||||
let token = self
|
||||
.scenes
|
||||
.get_mut(&scene)
|
||||
.map(|s| s.map.as_mut())
|
||||
.flatten()
|
||||
|
@ -65,43 +98,35 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
|||
ti.x = x;
|
||||
ti.y = y;
|
||||
true
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
fn token_info(&self, scene: usize, token_id: usize) -> Option<&TokenInfo> {
|
||||
let token = self.scenes
|
||||
let token = self
|
||||
.scenes
|
||||
.get(&scene)
|
||||
.map(|s| s.map.as_ref())
|
||||
.flatten()
|
||||
.map(|m| m.tokens.get(&token_id))
|
||||
.flatten();
|
||||
if let Some(ti) = token {
|
||||
Some(ti)
|
||||
}
|
||||
else {
|
||||
None
|
||||
}
|
||||
if let Some(ti) = token { Some(ti) } else { None }
|
||||
}
|
||||
|
||||
|
||||
fn available_tokens(&self, scene: usize) -> Vec<usize> {
|
||||
self.scenes
|
||||
.get(&scene)
|
||||
.map(|s| s.map.as_ref())
|
||||
.flatten()
|
||||
.map(|m| m.tokens
|
||||
.keys()
|
||||
.into_iter()
|
||||
.map(|k| *k)
|
||||
.collect()
|
||||
) // this map feels stupid but keys() turns into a &usize iterator so :shrug:
|
||||
// this map feels stupid but keys() turns into a &usize iterator so :shrug:
|
||||
.map(|m| m.tokens.keys().into_iter().map(|k| *k).collect())
|
||||
.unwrap_or(Vec::new())
|
||||
}
|
||||
|
||||
|
||||
fn create_token(&mut self, scene: usize, character: String, img_source: String, x: f32, y: f32) -> usize {
|
||||
let mut id = 0;
|
||||
let tokens = self.scenes
|
||||
let tokens = self
|
||||
.scenes
|
||||
.get_mut(&scene)
|
||||
.map(|s| s.map.as_mut())
|
||||
.flatten()
|
||||
|
@ -110,62 +135,70 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
|||
while tokens.contains_key(&id) {
|
||||
id += 1;
|
||||
}
|
||||
tokens.insert(id, TokenInfo { character, img_source, x, y });
|
||||
tokens.insert(
|
||||
id,
|
||||
TokenInfo {
|
||||
character,
|
||||
img_source,
|
||||
x,
|
||||
y,
|
||||
},
|
||||
);
|
||||
id
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn display_character(&self, character_id: usize, access: AccessLevel) -> Vec<Option<(String, EntryType)>> {
|
||||
self.characters
|
||||
.get(character_id)
|
||||
.map(|c| c.0.display(access))
|
||||
.unwrap_or(Vec::new())
|
||||
}
|
||||
|
||||
|
||||
fn characters(&self) -> Vec<usize> {
|
||||
// this is stupid i should redo the characters data struct to actually have ids rather than use an index
|
||||
// (as deletion of a character f-s up the ids)
|
||||
(0_usize..self.characters.len()).collect()
|
||||
}
|
||||
|
||||
|
||||
fn character_short(&self, character_id: usize) -> Option<CharacterShort> {
|
||||
self.characters
|
||||
.get(character_id)
|
||||
.map(|c| c.0.short().with_id(character_id))
|
||||
}
|
||||
|
||||
|
||||
fn scenes(&self) -> Vec<usize> {
|
||||
self.scenes.keys().map(|k| *k).collect()
|
||||
}
|
||||
|
||||
|
||||
fn scene_characters(&self, scene: usize, id: usize) -> Option<Vec<CharacterShort>> {
|
||||
let party = self.characters.get(id).map(|c| c.1.party).unwrap_or_default();
|
||||
self.scenes
|
||||
.get(&scene)
|
||||
.map(|s| s.characters
|
||||
self.scenes.get(&scene).map(|s| {
|
||||
s.characters
|
||||
.iter()
|
||||
.filter(|c| party.can_see(c.1))
|
||||
.map(|c| self.characters.get(c.0).map(|e| e.0.short().with_id(c.0)))
|
||||
.flatten()
|
||||
.collect()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
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 current_scene(&self) -> usize {
|
||||
self.current_scene
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
|
||||
struct CharacterInfo {
|
||||
pub owner: String,
|
||||
pub party: Party,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue