2023-03-11 16:58:13 -05:00
|
|
|
use ggdt::prelude::*;
|
2022-05-15 16:34:25 -04:00
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
pub const BALL_SIZE: i32 = 8;
|
|
|
|
pub const NUM_BALLS: usize = 32;
|
|
|
|
pub const NUM_BALL_SPRITES: usize = 16;
|
|
|
|
|
|
|
|
pub struct Context {
|
2023-03-02 15:10:27 -05:00
|
|
|
pub delta: f32,
|
2023-03-07 16:46:16 -05:00
|
|
|
pub system: System<DosLike>,
|
2023-03-02 15:10:27 -05:00
|
|
|
pub font: BitmaskFont,
|
2023-03-11 16:58:13 -05:00
|
|
|
pub sprites: Vec<IndexedBitmap>,
|
2023-03-02 15:10:27 -05:00
|
|
|
pub entities: Entities,
|
|
|
|
pub event_publisher: EventPublisher<Event>,
|
2022-05-15 16:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Game {
|
2023-03-02 15:10:27 -05:00
|
|
|
pub context: Context,
|
|
|
|
pub component_systems: ComponentSystems<Context, Context>,
|
|
|
|
pub event_listeners: EventListeners<Event, Context>,
|
2022-05-15 16:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Game {
|
2023-03-07 16:46:16 -05:00
|
|
|
pub fn new(mut system: System<DosLike>) -> Result<Self> {
|
2023-03-02 15:10:27 -05:00
|
|
|
let font = BitmaskFont::new_vga_font()?;
|
|
|
|
|
2023-03-11 16:58:13 -05:00
|
|
|
let (balls_bmp, balls_palette) = IndexedBitmap::load_pcx_file(Path::new("./assets/balls.pcx"))?;
|
2023-03-07 16:46:16 -05:00
|
|
|
system.res.palette = balls_palette.clone();
|
2023-03-02 15:10:27 -05:00
|
|
|
|
|
|
|
let mut sprites = Vec::new();
|
|
|
|
for i in 0..NUM_BALL_SPRITES {
|
2023-03-11 16:58:13 -05:00
|
|
|
let mut sprite = IndexedBitmap::new(BALL_SIZE as u32, BALL_SIZE as u32)?;
|
2023-03-02 15:10:27 -05:00
|
|
|
sprite.blit_region(
|
2023-03-11 16:58:13 -05:00
|
|
|
IndexedBlitMethod::Solid,
|
2023-03-02 15:10:27 -05:00
|
|
|
&balls_bmp,
|
|
|
|
&Rect::new(i as i32 * BALL_SIZE as i32, 0, BALL_SIZE as u32, BALL_SIZE as u32),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
sprites.push(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
let entities = Entities::new();
|
|
|
|
let mut component_systems = ComponentSystems::new();
|
|
|
|
let event_publisher = EventPublisher::new();
|
|
|
|
let mut event_listeners = EventListeners::new();
|
|
|
|
|
|
|
|
init_component_system(&mut component_systems);
|
|
|
|
init_event_listeners(&mut event_listeners);
|
|
|
|
|
|
|
|
Ok(Game {
|
|
|
|
context: Context {
|
|
|
|
delta: 0.0,
|
|
|
|
system,
|
|
|
|
font,
|
|
|
|
sprites,
|
|
|
|
entities,
|
|
|
|
event_publisher,
|
|
|
|
},
|
|
|
|
component_systems,
|
|
|
|
event_listeners,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_events(&mut self) {
|
|
|
|
self.event_listeners.take_queue_from(&mut self.context.event_publisher);
|
|
|
|
self.event_listeners.dispatch_queue(&mut self.context);
|
|
|
|
}
|
2022-05-15 16:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SimulationState;
|
|
|
|
|
2023-02-03 18:15:46 -05:00
|
|
|
impl AppState<Game> for SimulationState {
|
2023-03-02 15:10:27 -05:00
|
|
|
fn update(&mut self, _state: State, context: &mut Game) -> Option<StateChange<Game>> {
|
2023-03-07 16:46:16 -05:00
|
|
|
if context.context.system.res.keyboard.is_key_up(Scancode::S) {
|
2023-03-02 15:10:27 -05:00
|
|
|
context.do_events();
|
|
|
|
context.component_systems.update(&mut context.context);
|
|
|
|
}
|
|
|
|
|
2023-03-07 16:46:16 -05:00
|
|
|
if context.context.system.res.keyboard.is_key_pressed(Scancode::Escape) {
|
2023-03-02 15:10:27 -05:00
|
|
|
return Some(StateChange::Pop(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&mut self, _state: State, context: &mut Game) {
|
2023-03-07 16:46:16 -05:00
|
|
|
context.context.system.res.video.clear(2);
|
2023-03-02 15:10:27 -05:00
|
|
|
context.component_systems.render(&mut context.context);
|
2023-03-07 16:46:16 -05:00
|
|
|
context.context.system.res.video.print_string("hello, world!", 10, 10, FontRenderOpts::Color(15), &context.context.font);
|
2023-03-02 15:10:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn transition(&mut self, _state: State, _context: &mut Game) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn state_change(&mut self, new_state: State, _old_state: State, context: &mut Game) {
|
|
|
|
if new_state == State::Pending {
|
|
|
|
init_entities(&mut context.context.entities);
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 16:34:25 -04:00
|
|
|
}
|