Skip to content

Commit

Permalink
Merge pull request #55 from Brian-Catcow-B/rb-rotatris-mode
Browse files Browse the repository at this point in the history
Rb rotatris mode
  • Loading branch information
Brian-Catcow-B authored Oct 13, 2021
2 parents 64d404e + 791a231 commit 5005e00
Show file tree
Hide file tree
Showing 17 changed files with 2,631 additions and 1,211 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[alias]
optwin = "build -Z unstable-options --profile opt --target x86_64-pc-windows-gnu"
relwin = "build --release --target x86_64-pc-windows-gnu"
relpi = "build --release --target armv7-unknown-linux-gnueabihf"

13 changes: 6 additions & 7 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
MIT/X Consortium License
Custom License

@ 2020 Brian "Catcow" B <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
to deal in the Software without restriction except for selling the Software
or otherwise distributing the Software for gain. Distribution (with no gain),
modifications, and merge requests are welcomed.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software unless there is written approval
in the form of an email from the author(s).
all copies or substantial portions of the Software unless there is written
approval in the form of an email from the author(s).

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ In case a gamepad is not detected or a different layout is desired, here is a cr

Then the gamepad should be recognized when the program is opened again. When creating a gamepad mapping, consider which buttons the program has set to do which action:
```
Axis::LeftAxisX- and Button::DPadLeft -> Left
Axis::LeftAxisX+ and Button::DPadRight -> Right
Axis::LeftAxisY- and Button::DPadDown -> Down
Button::West -> RotateCw
Axis::LeftAxisX-/Button::DPadLeft -> Left
Axis::LeftAxisX+/Button::DPadRight -> Right
Axis::LeftAxisY-/Button::DPadDown -> Down
Button::East -> RotateCw
Button::South -> RotateCcw
Button::North -> RotateBoardCw
Button::West -> RotateBoardCcw
Button::Start -> Start
```
where, in the graphic, `Button::Start` is the small button just to the right of the circle button in the middle, and the compass directions refer to the four buttons on the right.
77 changes: 42 additions & 35 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ use ggez::timer;
use ggez::{Context, GameResult};

use crate::game::{Game, GameOptions};
use crate::menu::{Menu, MenuGameOptions};
use crate::menu::{menuhelpers::MenuGameOptions, Menu};

static STATE_MENU_BUT_MENU_NONE: &str =
"[!] control.state == ProgramState::Menu but control.menu == None";
static STATE_GAME_BUT_GAME_NONE: &str =
"[!] control.state == ProgramState::Game but control.game == None";

#[repr(u8)]
#[derive(PartialEq, Eq, Copy, Clone)]
Expand All @@ -18,34 +23,28 @@ pub struct Control {
state: ProgramState,
menu: Option<Menu>,
game: Option<Game>,
game_options: Option<MenuGameOptions>,
game_options: MenuGameOptions,
}

impl Control {
pub fn new(ctx: &mut Context) -> Control {
let menu_game_options = MenuGameOptions::default();
Self {
state: ProgramState::Menu,
menu: Some(Menu::new(ctx, &None)),
menu: Some(Menu::new(ctx, &menu_game_options)),
game: None,
game_options: None,
game_options: menu_game_options,
}
}

pub fn change_state(&mut self, ctx: &mut Context, new_state: ProgramState) {
fn change_state(&mut self, ctx: &mut Context, new_state: ProgramState) {
self.state = match new_state {
ProgramState::Menu => {
self.menu = Some(Menu::new(ctx, &self.game_options));
ProgramState::Menu
}
ProgramState::Game => {
self.game = Some(Game::new(
ctx,
&GameOptions::from(
self.game_options
.as_ref()
.expect("[!] attempted to start Game with no GameOptions"),
),
));
self.game = Some(Game::new(ctx, &GameOptions::from(&self.game_options)));
ProgramState::Game
}
};
Expand All @@ -61,24 +60,20 @@ impl EventHandler for Control {
match self.state {
ProgramState::Menu => {
// update the menu and get the state with GameOptions if ProgramState is changing
if let Some(state_and_gameoptions) = self
if let Some(new_state) = self
.menu
.as_mut()
.expect("[!] control.state == ProgramState::Menu but control.menu == None")
.update()
.expect(STATE_MENU_BUT_MENU_NONE)
.update(&mut self.game_options)
{
self.menu = None;
self.game_options = Some(state_and_gameoptions.1);
self.change_state(ctx, state_and_gameoptions.0);
self.change_state(ctx, new_state);
}
}
ProgramState::Game => {
// update the game and get the state that the program should be in
let state_returned = self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.update();
let state_returned =
self.game.as_mut().expect(STATE_GAME_BUT_GAME_NONE).update();
// should we change states?
if self.state != state_returned {
self.game = None;
Expand All @@ -102,12 +97,12 @@ impl EventHandler for Control {
ProgramState::Menu => self
.menu
.as_mut()
.expect("[!] control.state == ProgramState::Menu but control.menu == None")
.expect(STATE_MENU_BUT_MENU_NONE)
.key_down_event(keycode, repeat),
ProgramState::Game => self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.key_down_event(keycode, repeat),
};
}
Expand All @@ -117,12 +112,12 @@ impl EventHandler for Control {
ProgramState::Menu => self
.menu
.as_mut()
.expect("[!] control.state == ProgramState::Menu but control.menu == None")
.expect(STATE_MENU_BUT_MENU_NONE)
.key_up_event(keycode),
ProgramState::Game => self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.key_up_event(keycode),
};
}
Expand All @@ -133,7 +128,7 @@ impl EventHandler for Control {
ProgramState::Game => self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.gamepad_button_down_event(btn, id),
};
}
Expand All @@ -144,7 +139,7 @@ impl EventHandler for Control {
ProgramState::Game => self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.gamepad_button_up_event(btn, id),
};
}
Expand All @@ -155,7 +150,7 @@ impl EventHandler for Control {
ProgramState::Game => self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.gamepad_axis_event(axis, value, id),
}
}
Expand All @@ -165,29 +160,41 @@ impl EventHandler for Control {
ProgramState::Menu => self
.menu
.as_mut()
.expect("[!] control.state == ProgramState::Menu but control.menu == None")
.draw(ctx),
.expect(STATE_MENU_BUT_MENU_NONE)
.draw(ctx, &self.game_options),
ProgramState::Game => self
.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.draw(ctx),
};

graphics::present(ctx)
}

// this seems unused but is called somewhere in ggez to ultimately make things scale and get placed correctly when changing window size
fn resize_event(&mut self, ctx: &mut Context, width: f32, height: f32) {
let new_rect = graphics::Rect::new(0.0, 0.0, width, height);
graphics::set_screen_coordinates(ctx, new_rect).unwrap();

match self.state {
ProgramState::Menu => self
.menu
.as_mut()
.expect(STATE_MENU_BUT_MENU_NONE)
.resize_event((width, height)),
ProgramState::Game => self
.game
.as_mut()
.expect(STATE_GAME_BUT_GAME_NONE)
.resize_event(width, height),
};
}

fn focus_event(&mut self, _ctx: &mut Context, gained: bool) {
if self.state == ProgramState::Game {
self.game
.as_mut()
.expect("[!] control.state == ProgramState::Game but control.game == None")
.expect(STATE_GAME_BUT_GAME_NONE)
.focus_event(gained);
}
}
Expand Down
Loading

0 comments on commit 5005e00

Please sign in to comment.