skunkrts/src/unit_kind.rs

37 lines
902 B
Rust

use std::cell::RefCell;
use std::ops::Mul;
use std::rc::Rc;
use std::time::Duration;
use skunk2d::{Image, Vec2};
use crate::interpolator::Interpolator;
use crate::unit::Unit;
pub struct UnitKind {
pub sprite: Rc<Image>,
pub name: String,
pub speed: i32,
pub movement_duration: Duration
}
impl UnitKind {
pub fn new(name: &str, speed: i32, tick_duration: Duration) -> Rc<Self> {
UnitKind {
sprite: Image::load(format!("assets/units/{}.gif", name).as_str()),
name: name.into(),
speed,
movement_duration: tick_duration.mul(speed as u32)
}.into()
}
pub fn spawn(self: Rc<Self>, pos: Vec2<i32>) -> Rc<RefCell<Unit>> {
RefCell::new(Unit {
kind: self,
pos,
interpolator: Interpolator::fake(),
old_pos: pos,
cooldown: 0
}).into()
}
}