skunkrts/src/main.rs

74 lines
2.0 KiB
Rust

mod unit_kind;
mod unit;
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<RefCell<Unit>>,
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().pos.y = self.current_tick % self.map.size().y;
}
}
impl skunk2d::Game for Game {
fn new(window_state: &mut WindowState) -> Self {
setup_palette(window_state);
let tileset = Tileset::load("assets/hex2.gif", Vec2{x: 2, y: 1});
let kind = UnitKind::new("bob");
Game {
map: HexMap::new(Vec2{x: 11, y: 11}, tileset, Vec2{ x: 23, y: 13 }),
unit: kind.spawn(Vec2{x: 5, y: 5}),
last_tick: Instant::now(),
tick_duration: Duration::from_secs(1) / 10,
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) {
target.clear();
target.draw_hexmap(Vec2::zero(), &self.map);
self.unit.borrow().draw(target, &self.map);
}
}
fn main() {
run::<Game>(640, 360);
}