From 30fab7959d5a1e4e0788b63fc80526f22cfbc8d7 Mon Sep 17 00:00:00 2001 From: Ipagaxi Date: Mon, 15 May 2023 20:48:06 +0200 Subject: [PATCH] implementation of card evaluation --- Cargo.lock | 1 + models/src/cards.rs | 20 ++++++++++-- models/src/lib.rs | 1 + pc/Cargo.toml | 1 + pc/src/cards.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++ pc/src/main.rs | 1 + 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 pc/src/cards.rs diff --git a/Cargo.lock b/Cargo.lock index 37b83bb..89f28e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1260,6 +1260,7 @@ dependencies = [ "log", "m3-macro", "m3-map", + "m3-models", "my-env-logger-style", "num_enum 0.6.1", "once_cell", diff --git a/models/src/cards.rs b/models/src/cards.rs index 40c1cc5..8004a7b 100644 --- a/models/src/cards.rs +++ b/models/src/cards.rs @@ -3,8 +3,16 @@ use strum_macros::{AsRefStr, EnumIter}; #[derive(AsRefStr, Clone, Copy, Debug, EnumIter)] pub enum Card { + /// Turn Left Left, - Right + /// Turn Right + Right, + /// Keep doing current action + Wait(u8), + /// Starts driving foward + MotorOn, + /// Stops driving + MotorOff } impl Card { @@ -22,7 +30,10 @@ impl Card { #[derive(Clone, Debug, Decode, Default, Encode)] pub struct AvailableCards { pub left: u8, - pub right: u8 + pub right: u8, + pub wait: u8, + pub motor_on: u8, + pub motor_off: u8 } impl AvailableCards { @@ -30,7 +41,10 @@ impl AvailableCards { pub fn card_count(&self, card: Card) -> u8 { match card { Card::Left => self.left, - Card::Right => self.right + Card::Right => self.right, + Card::Wait(_i) => self.wait, + Card::MotorOn => self.motor_on, + Card::MotorOff => self.motor_off } } } diff --git a/models/src/lib.rs b/models/src/lib.rs index 7de54ad..176abeb 100644 --- a/models/src/lib.rs +++ b/models/src/lib.rs @@ -11,3 +11,4 @@ pub enum MessageToPc {} pub enum MessageToPyBadge { NewLevel(AvailableCards) } + diff --git a/pc/Cargo.toml b/pc/Cargo.toml index a926ed0..cdb6cc1 100644 --- a/pc/Cargo.toml +++ b/pc/Cargo.toml @@ -14,6 +14,7 @@ sdl2_bundled = ["tetra/sdl2_bundled"] log = "0.4.17" m3-macro = { version = "0.1.0", path = "../macro" } m3-map = { version = "0.1.0", path = "../map" } +m3-models = { version = "0.1.0", path = "../models" } my-env-logger-style = "0.1.0" num_enum = "0.6.1" once_cell = { version = "1.17.1", features = ["parking_lot"] } diff --git a/pc/src/cards.rs b/pc/src/cards.rs new file mode 100644 index 0000000..f6bbff4 --- /dev/null +++ b/pc/src/cards.rs @@ -0,0 +1,74 @@ +use m3_models::Card; +use m3_models::Card::*; + + + +pub enum CarAction { + TurnLeft, + TurnRight, + DriveForward, + Nothing +} +pub struct CardChanges<'a> { + /// Position of card in vector + card_pos: usize, + /// Relative y-position to former position + wait_counter: u8, + driving: bool, + cards: &'a Vec +} + + +impl<'a> Iterator for CardChanges<'a> { + type Item = Option; + + fn next(&mut self) -> Option { + match self.cards.get(self.card_pos) { + None => { + if self.driving { + Some(Some(CarAction::DriveForward)) + } else { + Some(Some(CarAction::Nothing)) + } + } + Some(card) => { + match card { + Card::Left => { + self.card_pos += 1; + self.driving = true; + Some(Some(CarAction::TurnLeft)) + } + Right => { + self.card_pos += 1; + self.driving = true; + Some(Some(CarAction::TurnRight)) + } + Wait(i) => { + if self.wait_counter < *i { + self.wait_counter += 1; + if self.driving { + Some(Some(CarAction::DriveForward)) + } else { + Some(Some(CarAction::Nothing)) + } + } else { + Some(Some(CarAction::Nothing)) + } + } + MotorOn => { + self.driving = true; + Some(Some(CarAction::Nothing)) + } + MotorOff => { + self.driving = false; + Some(Some(CarAction::Nothing)) + } + } + } + } + } +} + +pub fn evaluateCards(cards:&Vec) -> CardChanges{ + CardChanges { card_pos: 0, wait_counter: 0, driving: true, cards: cards } +} \ No newline at end of file diff --git a/pc/src/main.rs b/pc/src/main.rs index a3828fb..c1766e7 100644 --- a/pc/src/main.rs +++ b/pc/src/main.rs @@ -15,6 +15,7 @@ use tetra::{ mod tiles; use tiles::Textures; +mod cards; const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME"); const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");