movement speed, directional movement, etc
This commit is contained in:
parent
c64d698e04
commit
4ac52e886b
|
@ -948,6 +948,7 @@ dependencies = [
|
|||
name = "skunkrts"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"skunk2d",
|
||||
]
|
||||
|
||||
|
|
|
@ -7,3 +7,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
skunk2d = {path = "../skunk2d"}
|
||||
rand = "0.8.5"
|
20
src/main.rs
20
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<Option<Rc<RefCell<Unit>>>>,
|
||||
unit: Rc<RefCell<Unit>>,
|
||||
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,
|
||||
|
|
19
src/unit.rs
19
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<UnitKind>,
|
||||
pub pos: Vec2<i32>,
|
||||
pub interpolator: Interpolator,
|
||||
pub old_pos: Vec2<i32>
|
||||
pub old_pos: Vec2<i32>,
|
||||
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<i32>) {
|
||||
self.interpolator = Interpolator::new(Instant::now(), self.kind.movement_duration);
|
||||
self.old_pos = self.pos;
|
||||
|
|
|
@ -28,7 +28,8 @@ impl UnitKind {
|
|||
kind: self,
|
||||
pos,
|
||||
interpolator: Interpolator::fake(),
|
||||
old_pos: pos
|
||||
old_pos: pos,
|
||||
cooldown: 0
|
||||
}).into()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue