This commit is contained in:
Michael Zhang 2022-07-05 10:25:14 -05:00
commit e4559ac6f2
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
7 changed files with 2817 additions and 0 deletions

13
.editorconfig Normal file
View file

@ -0,0 +1,13 @@
root = true
[*]
end_of_file = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = space
indent_size = 4
[*.md]
indent_size = 2
trim_trailing_whitespace = false

9
.gitignore vendored Normal file
View file

@ -0,0 +1,9 @@
/target
**/*.rs.bk
# Added by cargo
#
# already existing elements were commented out
#/target

2648
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "wedge"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ggez = "0.6.1"

55
src/main.rs Normal file
View file

@ -0,0 +1,55 @@
mod screens;
use std::time::Instant;
use ggez::event::{self, EventHandler};
use ggez::graphics::{self, Color};
use ggez::{Context, ContextBuilder, GameResult};
use crate::screens::{Screens, Screen};
use crate::screens::menu::MenuScreen;
fn main() {
// Make a Context.
let (mut ctx, event_loop) = ContextBuilder::new("my_game", "Cool Game Author")
.build()
.expect("aieee, could not create ggez context!");
// Create an instance of your event handler.
// Usually, you should provide it with the Context object to
// use when setting your game up.
let my_game = MyGame::new(&mut ctx);
// Run!
event::run(ctx, event_loop, my_game);
}
struct MyGame {
screens: Screens,
last_update: Instant,
}
impl MyGame {
pub fn new(_ctx: &mut Context) -> MyGame {
// Load/create resources such as images here.
MyGame {
last_update: Instant::now(),
screens: Screens::init(Screen::Menu(MenuScreen::default())),
}
}
}
impl EventHandler<ggez::GameError> for MyGame {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
let now = Instant::now();
let elapsed = now - self.last_update;
self.screens.update(elapsed);
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, Color::WHITE);
self.screens.render(ctx);
graphics::present(ctx)
}
}

18
src/screens/menu.rs Normal file
View file

@ -0,0 +1,18 @@
use std::time::Duration;
use ggez::Context;
use super::ScreenAction;
#[derive(Default)]
pub struct MenuScreen {
}
impl MenuScreen {
pub fn update(&mut self, delta: Duration) -> ScreenAction {
ScreenAction::None
}
pub fn render(&self, ctx: &mut Context) {
}
}

65
src/screens/mod.rs Normal file
View file

@ -0,0 +1,65 @@
pub mod menu;
use std::time::Duration;
use ggez::Context;
use self::menu::MenuScreen;
pub struct Screens {
stack: Vec<Screen>,
}
pub enum ScreenAction {
None,
Push(Screen),
}
impl Screens {
pub fn init(base: Screen) -> Self {
Screens {
stack: vec![ base ],
}
}
fn top(&self) -> &Screen {
self.stack.last().unwrap()
}
fn top_mut(&mut self) -> &mut Screen {
self.stack.last_mut().unwrap()
}
pub fn update(&mut self, delta: Duration) {
let top = self.top_mut();
match top.update(delta) {
ScreenAction::None => (),
ScreenAction::Push(new) => {
self.stack.push(new);
}
}
}
pub fn render(&self, ctx: &mut Context) {
let top = self.top();
top.render(ctx);
}
}
pub enum Screen {
Menu(MenuScreen),
}
impl Screen {
fn update(&mut self, delta: Duration) -> ScreenAction {
match self {
Screen::Menu(screen) => screen.update(delta),
}
}
fn render(&self, ctx: &mut Context) {
match self {
Screen::Menu(screen) => screen.render(ctx),
}
}
}