added unit stuffs

This commit is contained in:
dani 2023-07-06 11:04:45 +00:00
parent a4ef5b27ce
commit 74fa53a30a
8 changed files with 101 additions and 3 deletions

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

BIN
assets/hex3.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
assets/units/bob.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 910 B

1
assets/units/tank.pal Normal file
View File

@ -0,0 +1 @@
ŚŠŚüţü¤˘¤””DBD¬®¬””|~|\Z\śžś´˛´ŚŽŚ¤¦¤

BIN
assets/units/tank.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -1,16 +1,61 @@
use skunk2d::{Event, Image, run, WindowState}; 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 { 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 { impl skunk2d::Game for Game {
fn new(window_state: &mut WindowState) -> Self { fn new(window_state: &mut WindowState) -> Self {
window_state.scramble_palette(); setup_palette(window_state);
Game {} 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) { 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 on_event(&mut self, window_state: &mut WindowState, event: Event) {
@ -18,6 +63,8 @@ impl skunk2d::Game for Game {
fn draw(&self, target: &mut Image) { fn draw(&self, target: &mut Image) {
target.clear(); target.clear();
target.draw_hexmap(Vec2::zero(), &self.map);
self.unit.borrow().draw(target, &self.map);
} }
} }

14
src/unit.rs Normal file
View File

@ -0,0 +1,14 @@
use std::rc::Rc;
use skunk2d::{HexMap, Image, Vec2};
use crate::unit_kind::UnitKind;
pub struct Unit {
pub kind: Rc<UnitKind>,
pub pos: Vec2<i32>
}
impl Unit {
pub fn draw(&self, target: &mut Image, map: &HexMap) {
target.draw_image( map.coord_to_pixel(self.pos), &self.kind.sprite);
}
}

26
src/unit_kind.rs Normal file
View File

@ -0,0 +1,26 @@
use std::cell::RefCell;
use std::rc::Rc;
use skunk2d::{Image, Vec2};
use crate::unit::Unit;
pub struct UnitKind {
pub sprite: Rc<Image>,
pub name: String
}
impl UnitKind {
pub fn new(name: &str) -> Rc<Self> {
UnitKind {
sprite: Image::load(format!("assets/units/{}.gif", name).as_str()),
name: name.into()
}.into()
}
pub fn spawn(self: Rc<Self>, pos: Vec2<i32>) -> Rc<RefCell<Unit>> {
RefCell::new(Unit {
kind: self,
pos
}).into()
}
}