diff --git a/Cargo.lock b/Cargo.lock index a4c7419..1ea8bff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -948,6 +948,7 @@ dependencies = [ name = "skunkrts" version = "0.1.0" dependencies = [ + "rand", "skunk2d", ] diff --git a/Cargo.toml b/Cargo.toml index 89d1aba..ee45188 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] skunk2d = {path = "../skunk2d"} +rand = "0.8.5" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 798b049..a91f80f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,16 +7,20 @@ mod interpolator; use std::cell::RefCell; use std::rc::Rc; use std::time::{Duration, Instant}; -use skunk2d::{Event, HexMap, Image, run, Tileset, Vec2, WindowState}; +use rand::Rng; +use skunk2d::{Direction, Event, HexMap, Image, run, Tileset, Vec2, WindowState}; use crate::unit::Unit; use crate::unit_kind::UnitKind; +//todo: keep unit_map position and unit's position in sync... how? + struct Game { map: HexMap, + unit_map: Vec>>>, unit: Rc>, last_tick: Instant, tick_duration: Duration, - current_tick: i32 + current_tick: i32, } fn setup_palette(ws: &mut WindowState) { @@ -35,7 +39,10 @@ impl Game { pub fn tick(&mut self, window_state: &mut WindowState) { window_state.log(format!("tick: {}", self.current_tick).as_str()); - self.unit.borrow_mut().set_pos(Vec2{x: 5, y: self.current_tick % self.map.size().y}); + let mut unit = self.unit.borrow_mut(); + unit.cool(); + unit.move_unit(rand::thread_rng().gen(), &self.map); + //unit.set_pos(Vec2{x: 5, y: self.current_tick % self.map.size().y}); } } @@ -45,9 +52,12 @@ impl skunk2d::Game for Game { window_state.scramble_palette(); let tick_duration = Duration::from_secs(1) / 10; let tileset = Tileset::load("assets/hex3.gif", Vec2{x: 1, y: 1}); - let kind = UnitKind::new("tank", 1, tick_duration); + let kind = UnitKind::new("tank", 4, tick_duration); + let map = HexMap::new(Vec2{x: 26, y: 22}, tileset, Vec2{ x: 72, y: 24 }); + let idx_size = map.idx_size(); Game { - map: HexMap::new(Vec2{x: 26, y: 22}, tileset, Vec2{ x: 72, y: 24 }), + map, + unit_map: vec![None; idx_size], unit: kind.spawn(Vec2{x: 5, y: 5}), last_tick: Instant::now(), tick_duration, diff --git a/src/unit.rs b/src/unit.rs index b734136..3aa12ff 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -1,6 +1,6 @@ use std::rc::Rc; use std::time::Instant; -use skunk2d::{HexMap, Image, Vec2}; +use skunk2d::{HexMap, Image, Vec2, Direction}; use crate::interpolator::Interpolator; use crate::unit_kind::UnitKind; @@ -8,10 +8,15 @@ pub struct Unit { pub kind: Rc, pub pos: Vec2, pub interpolator: Interpolator, - pub old_pos: Vec2 + pub old_pos: Vec2, + pub cooldown: i32 } impl Unit { + pub fn cool(&mut self) { + self.cooldown = (self.cooldown - 1).max(0) + } + pub fn draw(&self, target: &mut Image, map: &HexMap, time: Instant) { let old_pos = map.coord_to_pixel(self.old_pos); let new_pos = map.coord_to_pixel(self.pos); @@ -20,6 +25,16 @@ impl Unit { target.draw_image( pos + Vec2{x: 0, y: -24}, &self.kind.sprite); } + pub fn move_unit(&mut self, dir: Direction, map: &HexMap) { + if self.cooldown == 0 { + let new_pos = HexMap::get_neighbour(self.pos, dir); + if map.is_valid_coord(new_pos) { + self.set_pos(new_pos); + self.cooldown = self.kind.speed; + } + } + } + pub fn set_pos(&mut self, pos: Vec2) { self.interpolator = Interpolator::new(Instant::now(), self.kind.movement_duration); self.old_pos = self.pos; diff --git a/src/unit_kind.rs b/src/unit_kind.rs index 6e9a296..bdf6c27 100644 --- a/src/unit_kind.rs +++ b/src/unit_kind.rs @@ -28,7 +28,8 @@ impl UnitKind { kind: self, pos, interpolator: Interpolator::fake(), - old_pos: pos + old_pos: pos, + cooldown: 0 }).into() } }