Add some convient trait implementations for the Position component

This commit is contained in:
Timothy Warren 2021-12-01 15:25:48 -05:00
parent e55218c325
commit c7cd65af7a
2 changed files with 35 additions and 12 deletions

View File

@ -1,4 +1,4 @@
use rltk::RGB;
use rltk::{Point, RGB};
use serde::{Deserialize, Serialize};
use specs::error::NoError;
use specs::prelude::*;
@ -11,6 +11,33 @@ pub struct Position {
pub y: i32,
}
impl From<(i32, i32)> for Position {
fn from(f: (i32, i32)) -> Self {
Position {
x: f.0,
y: f.1,
}
}
}
impl From<Point> for Position {
fn from(p: Point) -> Self {
Position {
x: p.x,
y: p.y,
}
}
}
impl Into<Point> for Position {
fn into(self) -> Point {
Point {
x: self.x,
y: self.y,
}
}
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct Renderable {
pub glyph: rltk::FontCharType,
@ -24,7 +51,7 @@ pub struct Player {}
#[derive(Component, ConvertSaveload, Clone)]
pub struct Viewshed {
pub visible_tiles: Vec<rltk::Point>,
pub visible_tiles: Vec<Point>,
pub range: i32,
pub dirty: bool,
}
@ -134,7 +161,7 @@ pub struct WantsToPickupItem {
#[derive(Component, Debug, ConvertSaveload)]
pub struct WantsToUseItem {
pub item: Entity,
pub target: Option<rltk::Point>,
pub target: Option<Point>,
}
#[derive(Component, Debug, ConvertSaveload)]

View File

@ -476,14 +476,13 @@ impl State {
builder.spawn_entities(&mut self.ecs);
// Place the player and update resources
let (player_x, player_y) = (player_start.x, player_start.y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
*player_position = player_start.into();
let mut position_components = self.ecs.write_storage::<Position>();
let player_entity = self.ecs.fetch::<Entity>();
if let Some(player_pos_comp) = position_components.get_mut(*player_entity) {
player_pos_comp.x = player_x;
player_pos_comp.y = player_y;
player_pos_comp.x = player_start.x;
player_pos_comp.y = player_start.y;
}
// Mark the player's visibility as dirty
@ -546,12 +545,9 @@ fn main() -> rltk::BError {
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
gs.ecs.insert(Map::new(1));
gs.ecs.insert(Point::new(0, 0));
gs.ecs.insert(Point::zero());
gs.ecs.insert(rltk::RandomNumberGenerator::new());
let player_entity = spawner::player(&mut gs.ecs, 0, 0);
gs.ecs.insert(player_entity);
gs.ecs.insert(spawner::player(&mut gs.ecs, 0, 0));
gs.ecs.insert(RunState::MapGeneration {});
gs.ecs.insert(GameLog::new("Welcome to Rusty Roguelike"));
gs.ecs.insert(particle_system::ParticleBuilder::new());