skunkrts/src/main.rs

90 lines
2.5 KiB
Rust

#![feature(div_duration)]
mod unit_kind;
mod unit;
mod interpolator;
use std::cell::RefCell;
use std::rc::Rc;
use std::time::{Duration, Instant};
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,
}
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());
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});
}
}
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", 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,
unit_map: vec![None; idx_size],
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::<Game>(1920, 1080, 60);
}