//! Game piece models, ported from `shared/star_engine/lib/src/model`. use crate::hex::{Hex, HexFace, HexTransform}; /// A directional ray attack. Mirrors `RayAttack`. #[derive(Clone, PartialEq, Eq, Debug)] pub struct RayAttack { pub name: String, pub face: HexFace, pub source: Hex, } impl RayAttack { pub fn new(name: &str, face: HexFace, source: Hex) -> RayAttack { RayAttack { name: name.to_string(), face, source, } } fn direction(&self) -> Hex { self.face.vector() } /// The two hexes this attack threatens, nearest first. Mirrors /// `RayAttack.targets` (which yields source+dir then source+2dir). pub fn targets(&self) -> Vec { let dir = self.direction(); vec![self.source + dir, self.source + dir + dir] } pub fn hits(&self, hex: Hex) -> bool { self.targets().contains(&hex) } /// Damage dealt at a target hex: 2 at range 1, 1 at range 2. pub fn damage_at(&self, hex: Hex) -> i32 { if !self.hits(hex) { return 0; } let ring = (hex - self.source).ring() - 1; 2 - ring } } /// A move a unit can make. Mirrors `Move`. #[derive(Clone, PartialEq, Eq, Debug)] pub struct Move { pub name: String, pub destination: Hex, /// Final facing; corresponds to a sixth-of-a-turn rotation. pub direction: i32, } impl Move { pub fn new(name: &str, destination: Hex, direction: i32) -> Move { Move { name: name.to_string(), destination, direction, } } pub fn to_transform(&self) -> HexTransform { HexTransform::new(self.destination, self.direction.rem_euclid(6)) } } /// A kind of unit (Fighter, Cruiser, ...). Mirrors `KindOfUnit`. Only the /// fields exercised by the engine logic are modelled. #[derive(Clone, PartialEq, Eq, Debug)] pub struct KindOfUnit { pub name: String, pub body: Vec, /// Number of moves per turn (`move` in Dart). pub move_: i32, pub ray_attacks: Vec, pub moves: Vec, pub health: i32, } impl KindOfUnit { /// Body hexes expressed in the given transform's outer coordinates. pub fn body_transformed(&self, transform: HexTransform) -> Vec { self.body.iter().map(|&h| transform.from_local(h)).collect() } pub fn new_unit(&self, owner: i32, id: i32, position: HexTransform, initial_damage: i32) -> Unit { Unit { id, kind: self.clone(), position, damage: initial_damage, fired_this_turn: Vec::new(), moves_this_turn: 0, owner, } } /// `(attackIndex, localTargetHex)` pairs, attack order, nearest target /// first within each attack. Mirrors `KindOfUnit.threats`. pub fn threats(&self) -> Vec<(usize, Hex)> { let mut out = Vec::new(); for (i, attack) in self.ray_attacks.iter().enumerate() { for t in attack.targets() { out.push((i, t)); } } out } } /// A placed unit on the board. Mirrors `Unit`. #[derive(Clone, PartialEq, Eq, Debug)] pub struct Unit { pub id: i32, pub kind: KindOfUnit, pub position: HexTransform, pub damage: i32, /// Attack indices already fired this turn (kept sorted & unique). pub fired_this_turn: Vec, pub moves_this_turn: i32, pub owner: i32, } impl Unit { /// Body hexes in board (outer) coordinates. pub fn body(&self) -> Vec { self.kind .body .iter() .map(|&h| self.position.from_local(h)) .collect() } pub fn remaining_health(&self) -> i32 { (self.kind.health - self.damage).max(0) } /// Applies a move by index, advancing position and incrementing the /// per-turn move counter. Mirrors `Unit.move` + `moveTransform`. pub fn moved(&self, move_id: usize) -> Unit { let m = &self.kind.moves[move_id]; let mut u = self.clone(); u.position = self.position.combine(m.to_transform()); u.moves_this_turn = self.moves_this_turn + 1; u } pub fn deal_damage(&self, amount: i32) -> Unit { let mut u = self.clone(); u.damage = self.damage + amount; u } pub fn with_fired(&self, attack_id: i32) -> Unit { let mut u = self.clone(); if !u.fired_this_turn.contains(&attack_id) { u.fired_this_turn.push(attack_id); u.fired_this_turn.sort_unstable(); } u } }