#![feature(div_duration)] mod unit_kind; mod unit; 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 crate::unit::Unit; use crate::unit_kind::UnitKind; struct Game { map: HexMap, unit: Rc>, last_tick: Instant, tick_duration: Duration, current_tick: i32 } fn setup_palette(ws: &mut WindowState) { ws.set_palette(0, 0xc0, 0xf0, 0xd0); ws.set_palette(2, 0x10, 0x30, 0x20); ws.set_palette(1, 0xb0, 0xe0, 0xc0); ws.set_palette(3, 0xa0, 0xd0, 0xb0); ws.set_palette(255, 0xff, 0xff, 0xff); ws.set_palette(254, 0x00, 0x00, 0x00); ws.set_palette(253, 0x80, 0x80, 0x80); } 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}); } } impl skunk2d::Game for Game { fn new(window_state: &mut WindowState) -> Self { setup_palette(window_state); 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); Game { map: HexMap::new(Vec2{x: 26, y: 22}, tileset, Vec2{ x: 72, y: 24 }), unit: kind.spawn(Vec2{x: 5, y: 5}), last_tick: Instant::now(), tick_duration, current_tick: 0 } } fn update(&mut self, window_state: &mut WindowState) { if Instant::now() - self.last_tick >= self.tick_duration { self.tick(window_state); self.last_tick += self.tick_duration; self.current_tick += 1; } } fn on_event(&mut self, window_state: &mut WindowState, event: Event) { } fn draw(&self, target: &mut Image) { let now = Instant::now(); target.clear(); target.draw_hexmap(Vec2::zero(), &self.map); self.unit.borrow().draw(target, &self.map, now); } } fn main() { run::(1920, 1080); }