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
}
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
}

View file

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

View file

@ -11,6 +11,9 @@ use game::MillGame;
fn main() -> Result<()> {
let mut world_builder = World::builder();
create_engine(&mut world_builder)?;
world_builder.add_system(MillGame::update);
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.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 =
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_mouse = true;
let engine = Engine::new(create_info, world_builder)?;
let engine = Engine::new::<MillGame>(create_info, world_builder)?;
Ok(())
}