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 entry;
|
||||||
pub mod scene;
|
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
|
/// Creates a new game
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
|
|
||||||
|
@ -23,6 +24,7 @@ pub trait GameImpl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::
|
||||||
// Scenes
|
// Scenes
|
||||||
/// the list of available scenes
|
/// the list of available scenes
|
||||||
fn scenes(&self) -> Vec<usize>;
|
fn scenes(&self) -> Vec<usize>;
|
||||||
|
fn current_scene(&self) -> usize;
|
||||||
/// Gets the map background (file path)
|
/// Gets the map background (file path)
|
||||||
fn scene_characters(&self, scene: usize, character_id: usize) -> Option<Vec<CharacterShort>>;
|
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<String>;
|
||||||
|
@ -37,17 +39,47 @@ pub struct Game<C: Character<A> + Serialize, A: entry::GameEntry + Serialize> {
|
||||||
_a: PhantomData<A>,
|
_a: PhantomData<A>,
|
||||||
characters: Vec<(C, CharacterInfo)>,
|
characters: Vec<(C, CharacterInfo)>,
|
||||||
scenes: HashMap<usize, Scene>,
|
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 {
|
fn new() -> Self {
|
||||||
let mut tokens = HashMap::new();
|
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();
|
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 {
|
Self {
|
||||||
_a: PhantomData,
|
_a: PhantomData,
|
||||||
characters: Vec::new(),
|
characters: Vec::new(),
|
||||||
scenes,
|
scenes,
|
||||||
|
current_scene: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn create_character(&mut self) -> usize {
|
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
|
self.characters.len() - 1
|
||||||
}
|
}
|
||||||
fn move_token(&mut self, scene: usize, token_id: usize, x: f32, y: f32) -> bool {
|
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)
|
.get_mut(&scene)
|
||||||
.map(|s| s.map.as_mut())
|
.map(|s| s.map.as_mut())
|
||||||
.flatten()
|
.flatten()
|
||||||
|
@ -65,43 +98,35 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
||||||
ti.x = x;
|
ti.x = x;
|
||||||
ti.y = y;
|
ti.y = y;
|
||||||
true
|
true
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn token_info(&self, scene: usize, token_id: usize) -> Option<&TokenInfo> {
|
fn token_info(&self, scene: usize, token_id: usize) -> Option<&TokenInfo> {
|
||||||
let token = self.scenes
|
let token = self
|
||||||
|
.scenes
|
||||||
.get(&scene)
|
.get(&scene)
|
||||||
.map(|s| s.map.as_ref())
|
.map(|s| s.map.as_ref())
|
||||||
.flatten()
|
.flatten()
|
||||||
.map(|m| m.tokens.get(&token_id))
|
.map(|m| m.tokens.get(&token_id))
|
||||||
.flatten();
|
.flatten();
|
||||||
if let Some(ti) = token {
|
if let Some(ti) = token { Some(ti) } else { None }
|
||||||
Some(ti)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn available_tokens(&self, scene: usize) -> Vec<usize> {
|
fn available_tokens(&self, scene: usize) -> Vec<usize> {
|
||||||
self.scenes
|
self.scenes
|
||||||
.get(&scene)
|
.get(&scene)
|
||||||
.map(|s| s.map.as_ref())
|
.map(|s| s.map.as_ref())
|
||||||
.flatten()
|
.flatten()
|
||||||
.map(|m| m.tokens
|
// this map feels stupid but keys() turns into a &usize iterator so :shrug:
|
||||||
.keys()
|
.map(|m| m.tokens.keys().into_iter().map(|k| *k).collect())
|
||||||
.into_iter()
|
|
||||||
.map(|k| *k)
|
|
||||||
.collect()
|
|
||||||
) // this map feels stupid but keys() turns into a &usize iterator so :shrug:
|
|
||||||
.unwrap_or(Vec::new())
|
.unwrap_or(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_token(&mut self, scene: usize, character: String, img_source: String, x: f32, y: f32) -> usize {
|
fn create_token(&mut self, scene: usize, character: String, img_source: String, x: f32, y: f32) -> usize {
|
||||||
let mut id = 0;
|
let mut id = 0;
|
||||||
let tokens = self.scenes
|
let tokens = self
|
||||||
|
.scenes
|
||||||
.get_mut(&scene)
|
.get_mut(&scene)
|
||||||
.map(|s| s.map.as_mut())
|
.map(|s| s.map.as_mut())
|
||||||
.flatten()
|
.flatten()
|
||||||
|
@ -110,62 +135,70 @@ impl<'a, C: Character<A> + Serialize + Deserialize<'a>, A: entry::GameEntry + Se
|
||||||
while tokens.contains_key(&id) {
|
while tokens.contains_key(&id) {
|
||||||
id += 1;
|
id += 1;
|
||||||
}
|
}
|
||||||
tokens.insert(id, TokenInfo { character, img_source, x, y });
|
tokens.insert(
|
||||||
|
id,
|
||||||
|
TokenInfo {
|
||||||
|
character,
|
||||||
|
img_source,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
},
|
||||||
|
);
|
||||||
id
|
id
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display_character(&self, character_id: usize, access: AccessLevel) -> Vec<Option<(String, EntryType)>> {
|
fn display_character(&self, character_id: usize, access: AccessLevel) -> Vec<Option<(String, EntryType)>> {
|
||||||
self.characters
|
self.characters
|
||||||
.get(character_id)
|
.get(character_id)
|
||||||
.map(|c| c.0.display(access))
|
.map(|c| c.0.display(access))
|
||||||
.unwrap_or(Vec::new())
|
.unwrap_or(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn characters(&self) -> Vec<usize> {
|
fn characters(&self) -> Vec<usize> {
|
||||||
// this is stupid i should redo the characters data struct to actually have ids rather than use an index
|
// 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)
|
// (as deletion of a character f-s up the ids)
|
||||||
(0_usize..self.characters.len()).collect()
|
(0_usize..self.characters.len()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn character_short(&self, character_id: usize) -> Option<CharacterShort> {
|
fn character_short(&self, character_id: usize) -> Option<CharacterShort> {
|
||||||
self.characters
|
self.characters
|
||||||
.get(character_id)
|
.get(character_id)
|
||||||
.map(|c| c.0.short().with_id(character_id))
|
.map(|c| c.0.short().with_id(character_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scenes(&self) -> Vec<usize> {
|
fn scenes(&self) -> Vec<usize> {
|
||||||
self.scenes.keys().map(|k| *k).collect()
|
self.scenes.keys().map(|k| *k).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scene_characters(&self, scene: usize, id: usize) -> Option<Vec<CharacterShort>> {
|
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();
|
let party = self.characters.get(id).map(|c| c.1.party).unwrap_or_default();
|
||||||
self.scenes
|
self.scenes.get(&scene).map(|s| {
|
||||||
.get(&scene)
|
s.characters
|
||||||
.map(|s| s.characters
|
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|c| party.can_see(c.1))
|
.filter(|c| party.can_see(c.1))
|
||||||
.map(|c| self.characters.get(c.0).map(|e| e.0.short().with_id(c.0)))
|
.map(|c| self.characters.get(c.0).map(|e| e.0.short().with_id(c.0)))
|
||||||
.flatten()
|
.flatten()
|
||||||
.collect()
|
.collect()
|
||||||
)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scene_map(&self, scene_id: usize) -> Option<String> {
|
fn scene_map(&self, scene_id: usize) -> Option<String> {
|
||||||
self.scenes
|
self.scenes
|
||||||
.get(&scene_id)
|
.get(&scene_id)
|
||||||
.map(|s| s.map.as_ref().map(|m| m.background.clone()))
|
.map(|s| s.map.as_ref().map(|m| m.background.clone()))
|
||||||
.flatten()
|
.flatten()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
fn current_scene(&self) -> usize {
|
||||||
|
self.current_scene
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
|
||||||
struct CharacterInfo {
|
struct CharacterInfo {
|
||||||
pub owner: String,
|
pub owner: String,
|
||||||
pub party: Party,
|
pub party: Party,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue