Continue fixing

This commit is contained in:
Michael Huebner 2025-03-25 12:39:50 +01:00
parent b55e4fbbba
commit e5d94f32f0
3 changed files with 61 additions and 73 deletions

View file

@ -269,7 +269,11 @@ impl Board {
&self.black_start_slots &self.black_start_slots
} }
pub fn slots(&mut self) -> &mut [[[BoardSlot; 3]; 3]; 3] { pub fn slots(&self) -> &[[[BoardSlot; 3]; 3]; 3] {
&self.slots
}
pub fn slots_mut(&mut self) -> &mut [[[BoardSlot; 3]; 3]; 3] {
&mut self.slots &mut self.slots
} }

View file

@ -1,12 +1,4 @@
use std::{ use std::{fs::OpenOptions, io::Write, sync::Arc, time::Duration};
fs::OpenOptions,
io::Write,
sync::{
atomic::{AtomicBool, AtomicU32, Ordering::SeqCst},
Arc, Mutex, MutexGuard,
},
time::Duration,
};
use anyhow::Result; use anyhow::Result;
use assetpath::AssetPath; use assetpath::AssetPath;
@ -63,6 +55,7 @@ pub enum LogSeverity {
const LOG_SEVERITY: LogSeverity = LogSeverity::Basic; const LOG_SEVERITY: LogSeverity = LogSeverity::Basic;
const LOG_FILE: &str = "millgame.log"; const LOG_FILE: &str = "millgame.log";
const EXTRA_LOG_FILE: &str = "millgame_extra.log";
impl PlayerColor { impl PlayerColor {
pub fn swap(&mut self) { pub fn swap(&mut self) {
@ -96,50 +89,44 @@ struct TurnState {
} }
impl TurnState { impl TurnState {
pub fn new(game: &MillGame) -> Result<Self> { pub fn new(world: &World, game: &MillGame) -> Result<Self> {
let mut white_infos = Vec::new(); let mut white_infos = Vec::new();
let mut black_infos = Vec::new(); let mut black_infos = Vec::new();
game.scene.lock().unwrap().on_scene(|scene| { for white_stone in game.white_stones.iter() {
for white_stone in game.white_stones.lock().unwrap().iter() { let pos = match white_stone.state {
let pos = match white_stone.state { StoneState::ReadyToBePlaced => None,
StoneState::ReadyToBePlaced => None, StoneState::Placed => Some(
StoneState::Placed => Some( world
scene .entity(white_stone.stone)?
.entity(white_stone.stone)? .get_component::<Location>()?
.get_component::<Location>()? .position()
.position() .truncate(),
.truncate(), ),
), StoneState::Dead => None,
StoneState::Dead => None, };
};
white_infos.push((white_stone.state, white_stone.stone, pos)); white_infos.push((white_stone.state, white_stone.stone, pos));
} }
for black_stone in game.black_stones.lock().unwrap().iter() { for black_stone in game.black_stones.iter() {
let pos = match black_stone.state { let pos = match black_stone.state {
StoneState::ReadyToBePlaced => None, StoneState::ReadyToBePlaced => None,
StoneState::Placed => Some( StoneState::Placed => Some(
scene world
.entity(black_stone.stone)? .entity(black_stone.stone)?
.get_component::<Location>()? .get_component::<Location>()?
.position() .position()
.truncate(), .truncate(),
), ),
StoneState::Dead => None, StoneState::Dead => None,
}; };
black_infos.push((black_stone.state, black_stone.stone, pos)); black_infos.push((black_stone.state, black_stone.stone, pos));
} }
Ok(())
})?;
let board_infos: Vec<((usize, usize, usize), Vector2<f32>, BoardSlotState)> = game let board_infos: Vec<((usize, usize, usize), Vector2<f32>, BoardSlotState)> = game
.board .board
.lock()
.unwrap()
.slots() .slots()
.iter() .iter()
.flatten() .flatten()
@ -251,7 +238,7 @@ impl MillGame {
let board = Board::new(world)?; let board = Board::new(world)?;
// add light // add light
let mut sun_light = Light::directional_light(world.resources.get::<Context>().device()); let mut sun_light = Light::directional_light(world.resources.get::<Context>().device())?;
sun_light.set_direction(vec3(10.0, -4.0, -20.0))?; sun_light.set_direction(vec3(10.0, -4.0, -20.0))?;
sun_light.set_color(vec3(1.0, 1.0, 1.0))?; sun_light.set_color(vec3(1.0, 1.0, 1.0))?;
sun_light.set_position(vec3(0.0, 0.0, 100.0))?; sun_light.set_position(vec3(0.0, 0.0, 100.0))?;
@ -376,15 +363,15 @@ impl MillGame {
Ok(()) Ok(())
} }
pub fn finish_turn(&self) { pub fn finish_turn(&mut self) {
Self::log( Self::log(
&format!("{:?} finished turn", *self.current_player.lock().unwrap()), &format!("{:?} finished turn", self.current_player),
LogSeverity::Debug, LogSeverity::Debug,
); );
*self.last_turn_timing.lock().unwrap() = self.engine.time(); self.last_turn_timing = self.engine.time();
self.turn_finished.store(true, SeqCst); self.turn_finished = true;
*self.selected_field.lock().unwrap() = None; self.selected_field = None;
} }
fn print_game_state(&self, severity: LogSeverity, log_state: bool) -> Result<()> { fn print_game_state(&self, severity: LogSeverity, log_state: bool) -> Result<()> {
@ -856,9 +843,7 @@ impl MillGame {
Self::log("check_mouse_click", LogSeverity::Debug); Self::log("check_mouse_click", LogSeverity::Debug);
self.scene.lock().unwrap().on_scene(|scene| { self.scene.lock().unwrap().on_scene(|scene| {
if let Some(world_space) = if let Some(world_space) = scene.screen_space_to_world(self.mouse_x, self.mouse_y)? {
scene.screen_space_to_world(self.mouse_x.load(SeqCst), self.mouse_y.load(SeqCst))?
{
let mut board = self.board.lock().unwrap(); let mut board = self.board.lock().unwrap();
if let Some(slot) = board.close_to_marker(world_space.truncate()) { if let Some(slot) = board.close_to_marker(world_space.truncate()) {
@ -906,35 +891,33 @@ impl MillGame {
if let Ok(mut file) = OpenOptions::new() if let Ok(mut file) = OpenOptions::new()
.append(true) .append(true)
.create(true) .create(true)
.open("millgame_extra.log") .open(EXTRA_LOG_FILE)
{ {
if let Err(_) = file.write_all(format!("{}\n", s.to_string()).as_bytes()) {} if let Err(_) = file.write_all(format!("{}\n", s.to_string()).as_bytes()) {}
} }
} }
}
impl EngineObject for MillGame { pub fn update(world: &mut World) -> Result<bool> {
fn name(&self) -> &str { let now = world.now();
"MillGame" let me = world.resources.get_mut::<Self>();
}
fn update(&self) -> Result<()> { if me.turn_finished {
if self.turn_finished.load(SeqCst) { if (me.last_turn_timing + Self::TURN_WAIT_TIME) < now {
if (*self.last_turn_timing.lock().unwrap() + Self::TURN_WAIT_TIME) < self.engine.time() me.turn_finished = false;
{ me.next_game_step()?;
self.turn_finished.store(false, SeqCst);
self.next_game_step()?;
} }
} }
Ok(()) Ok(true)
} }
}
fn event(&self, event: EngineEvent) -> Result<()> { impl EventConsumer for MillGame {
fn event(&mut self, world: &mut World, event: EngineEvent) -> Result<()> {
match event { match event {
EngineEvent::MouseMotion(x, y) => { EngineEvent::MouseMotion(x, y) => {
self.mouse_x.store(x, SeqCst); self.mouse_x = x;
self.mouse_y.store(y, SeqCst); self.mouse_y = y;
self.camera_controls.lock().unwrap().mouse_move( self.camera_controls.lock().unwrap().mouse_move(
x, x,

View file

@ -11,6 +11,9 @@ use game::MillGame;
fn main() -> Result<()> { fn main() -> Result<()> {
let mut world_builder = World::builder(); let mut world_builder = World::builder();
create_engine(&mut world_builder)?;
world_builder.add_system(MillGame::update);
world_builder.build().run() world_builder.build().run()
} }
@ -20,8 +23,6 @@ fn create_engine(world_builder: &mut WorldBuilder) -> Result<()> {
create_info.resource_base_path = "resources".to_string(); create_info.resource_base_path = "resources".to_string();
create_info.gui_info.font = Font::Path(AssetPath::from(("resources", "ExportedFont.png"))); create_info.gui_info.font = Font::Path(AssetPath::from(("resources", "ExportedFont.png")));
create_info.gui_info.menu_button = AssetPath::from("button_dark.png");
create_info.gui_info.menu_button_selected = AssetPath::from("button_light.png");
create_info.gui_info.resource_directory = create_info.gui_info.resource_directory =
AssetPath::from((create_info.resource_base_path.as_str(), "")).into(); AssetPath::from((create_info.resource_base_path.as_str(), "")).into();
@ -38,7 +39,7 @@ fn create_engine(world_builder: &mut WorldBuilder) -> Result<()> {
create_info.enable_keyboard = true; create_info.enable_keyboard = true;
create_info.enable_mouse = true; create_info.enable_mouse = true;
let engine = Engine::new(create_info, world_builder)?; let engine = Engine::new::<MillGame>(create_info, world_builder)?;
Ok(()) Ok(())
} }