add level swith controls to mpsc
This commit is contained in:
parent
1c761e97b8
commit
7bd62b15da
@ -2,27 +2,19 @@ use std::{cell::RefCell, sync::mpsc::Sender};
|
|||||||
|
|
||||||
use discord_sdk::activity::ActivityBuilder;
|
use discord_sdk::activity::ActivityBuilder;
|
||||||
|
|
||||||
use crate::{GameConfig, utilities::non_ref_raylib::HackedRaylibHandle};
|
use crate::{utilities::non_ref_raylib::HackedRaylibHandle, GameConfig};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ControlFlag {
|
pub enum ControlFlag {
|
||||||
Quit
|
Quit,
|
||||||
|
SwitchLevel(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct GameContext {
|
pub struct GameContext {
|
||||||
pub renderer: RefCell<HackedRaylibHandle>,
|
pub renderer: RefCell<HackedRaylibHandle>,
|
||||||
pub config: GameConfig,
|
pub config: GameConfig,
|
||||||
|
pub current_level: usize,
|
||||||
pub discord_rpc_send: Sender<Option<ActivityBuilder>>,
|
pub discord_rpc_send: Sender<Option<ActivityBuilder>>,
|
||||||
pub flag_send: Sender<Option<ControlFlag>>
|
pub flag_send: Sender<Option<ControlFlag>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl GameContext {
|
|
||||||
// /// Construct a new game context.
|
|
||||||
// pub fn new(raylib: RefCell<HackedRaylibHandle>) -> Self {
|
|
||||||
// Self {
|
|
||||||
// renderer: raylib
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
@ -70,7 +70,7 @@
|
|||||||
)]
|
)]
|
||||||
#![clippy::msrv = "1.57.0"]
|
#![clippy::msrv = "1.57.0"]
|
||||||
|
|
||||||
use std::{cell::RefCell, sync::mpsc::TryRecvError};
|
use std::{borrow::BorrowMut, cell::RefCell, sync::mpsc::TryRecvError};
|
||||||
|
|
||||||
use discord_sdk::activity::ActivityBuilder;
|
use discord_sdk::activity::ActivityBuilder;
|
||||||
use raylib::prelude::*;
|
use raylib::prelude::*;
|
||||||
@ -147,7 +147,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
|
|||||||
// Build an MPSC for signaling the control thread
|
// Build an MPSC for signaling the control thread
|
||||||
let (send_control_signal, recv_control_signal) = std::sync::mpsc::channel();
|
let (send_control_signal, recv_control_signal) = std::sync::mpsc::channel();
|
||||||
|
|
||||||
let context;
|
let mut context;
|
||||||
let raylib_thread;
|
let raylib_thread;
|
||||||
{
|
{
|
||||||
// Set up FFI access to raylib
|
// Set up FFI access to raylib
|
||||||
@ -170,6 +170,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
|
|||||||
context = Box::new(GameContext {
|
context = Box::new(GameContext {
|
||||||
renderer: RefCell::new(rl.into()),
|
renderer: RefCell::new(rl.into()),
|
||||||
config: game_config.clone(),
|
config: game_config.clone(),
|
||||||
|
current_level: 0,
|
||||||
discord_rpc_send: send_discord_rpc,
|
discord_rpc_send: send_discord_rpc,
|
||||||
flag_send: send_control_signal,
|
flag_send: send_control_signal,
|
||||||
});
|
});
|
||||||
@ -315,6 +316,9 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
|
|||||||
if let Some(flag) = flag {
|
if let Some(flag) = flag {
|
||||||
match flag {
|
match flag {
|
||||||
context::ControlFlag::Quit => break,
|
context::ControlFlag::Quit => break,
|
||||||
|
context::ControlFlag::SwitchLevel(level) => {
|
||||||
|
context.as_mut().current_level = level;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
|
|||||||
self.player.reset();
|
self.player.reset();
|
||||||
|
|
||||||
// Set the player to running
|
// Set the player to running
|
||||||
let cur_level = self.levels.get(self.current_level_idx).unwrap();
|
let cur_level = self.levels.get(context.current_level).unwrap();
|
||||||
let _ = self.player.update_player(
|
let _ = self.player.update_player(
|
||||||
Some(CharacterState::Running),
|
Some(CharacterState::Running),
|
||||||
&cur_level.colliders,
|
&cur_level.colliders,
|
||||||
@ -94,6 +94,7 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
|
|||||||
) -> Result<dirty_fsm::ActionFlag<Scenes>, ScreenError> {
|
) -> Result<dirty_fsm::ActionFlag<Scenes>, ScreenError> {
|
||||||
puffin::profile_function!();
|
puffin::profile_function!();
|
||||||
trace!("execute() called on InGameScreen");
|
trace!("execute() called on InGameScreen");
|
||||||
|
self.current_level_idx = context.current_level;
|
||||||
|
|
||||||
// Grab exclusive access to the renderer
|
// Grab exclusive access to the renderer
|
||||||
let mut renderer = context.renderer.borrow_mut();
|
let mut renderer = context.renderer.borrow_mut();
|
||||||
|
Reference in New Issue
Block a user