- added a new API for switching between terminal and application mode
- removed unneeded imports to reduce the dumb amount of warnings from the compiler - added a bounds check in frame.rs to avoid a panic when a frame tries to render a character out of bounds, instead returning an error
This commit is contained in:
@@ -292,13 +292,7 @@ impl GameTimer {
|
||||
self.time_since_spawn += 1;
|
||||
self.time_since_move += 1;
|
||||
}
|
||||
|
||||
pub fn get_spawn_time(&self) -> u32 {
|
||||
self.time_since_spawn
|
||||
}
|
||||
pub fn reset_spawn_time(&mut self) {
|
||||
self.time_since_spawn = 0
|
||||
}
|
||||
|
||||
pub fn get_move_time(&self) -> u32 {
|
||||
self.time_since_move
|
||||
}
|
||||
|
||||
@@ -5,15 +5,12 @@ use alloc::borrow::ToOwned;
|
||||
use crate::{println, print, mknode, std};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use lazy_static::lazy_static;
|
||||
use crate::std::application::{
|
||||
Application,
|
||||
Error as ShellError
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
idx: i32,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use alloc::format;
|
||||
use alloc::string::String;
|
||||
use crate::println;
|
||||
|
||||
const PI: f64 = 3.14159265358979323846264338327950288419716939937510;
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use async_trait::async_trait;
|
||||
use alloc::{boxed::Box, format, string::String, vec::Vec};
|
||||
use log::info;
|
||||
|
||||
use crate::{std::os::OS, std::io::{Color, write, Screen}, println, std::application::{
|
||||
Application,
|
||||
Error,
|
||||
}, std};
|
||||
use crate::std::{
|
||||
os::OS,
|
||||
io::{Color, write, Screen},
|
||||
application::{Application, Error},
|
||||
};
|
||||
use crate::println;
|
||||
|
||||
const CRYSTAL_LOGO: &str =
|
||||
"\n $$$$$$\\ $$\\ $$\\ $$$$$$\\ $$$$$$\\
|
||||
|
||||
+146
-146
@@ -1,146 +1,146 @@
|
||||
use alloc::{boxed::Box, string::String, vec::Vec};
|
||||
use core::cmp::min;
|
||||
|
||||
struct Player {
|
||||
username: String,
|
||||
stats: EntityStats,
|
||||
|
||||
exp: u32,
|
||||
level: u32,
|
||||
skill_points: u32,
|
||||
skills: Vec<Box<dyn Skill>>,
|
||||
|
||||
helmet: Option<Helmet>,
|
||||
chestplate: Option<Chestplate>,
|
||||
boots: Option<Boots>,
|
||||
|
||||
inventory: Vec<Item>,
|
||||
}
|
||||
|
||||
struct EntityStats {
|
||||
health: i32,
|
||||
max_health: i32,
|
||||
mana: i32,
|
||||
max_mana: i32,
|
||||
defence: i32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
fn new(username: String) -> Self {
|
||||
Self {
|
||||
username,
|
||||
stats: EntityStats {
|
||||
health: 100,
|
||||
max_health: 100,
|
||||
mana: 100,
|
||||
max_mana: 100,
|
||||
defence: 0,
|
||||
},
|
||||
exp: 0,
|
||||
level: 0,
|
||||
skill_points: 0,
|
||||
skills: Vec::new(),
|
||||
|
||||
helmet: None,
|
||||
chestplate: None,
|
||||
boots: None,
|
||||
inventory: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn heal(&mut self, amount: i32) {
|
||||
let max_health = self.max_health();
|
||||
|
||||
self.stats.health = min(self.stats.health + amount, max_health);
|
||||
}
|
||||
|
||||
fn damage(&mut self, amount: i32) {
|
||||
let hp = self.health_points();
|
||||
}
|
||||
|
||||
fn inventory_contents_mut(&mut self) -> &mut Vec<Item> {
|
||||
&mut self.inventory
|
||||
}
|
||||
|
||||
fn max_health(&self) -> i32 {
|
||||
let mut max_health = self.stats.max_health;
|
||||
|
||||
if let Some(helmet) = &self.helmet {
|
||||
max_health += helmet.stats.health_bonus;
|
||||
}
|
||||
if let Some(chestplate) = &self.chestplate {
|
||||
max_health += chestplate.stats.health_bonus;
|
||||
}
|
||||
if let Some(boots) = &self.boots {
|
||||
max_health += boots.stats.health_bonus;
|
||||
}
|
||||
|
||||
max_health
|
||||
}
|
||||
|
||||
fn health_points(&self) -> i32 {
|
||||
let mut hp = self.stats.health;
|
||||
if let Some(helmet) = &self.helmet {
|
||||
hp += helmet.stats.health_bonus;
|
||||
}
|
||||
if let Some(chestplate) = &self.chestplate {
|
||||
hp += chestplate.stats.health_bonus;
|
||||
}
|
||||
if let Some(boots) = &self.boots {
|
||||
hp += boots.stats.health_bonus;
|
||||
}
|
||||
hp
|
||||
}
|
||||
}
|
||||
|
||||
enum Item {
|
||||
Helmet(Helmet),
|
||||
Chestplate(Chestplate),
|
||||
Boots(Boots),
|
||||
Sword,
|
||||
Potion,
|
||||
}
|
||||
|
||||
struct Helmet {
|
||||
name: &'static str,
|
||||
lore: &'static str,
|
||||
stats: ArmourStats,
|
||||
}
|
||||
|
||||
struct Chestplate {
|
||||
name: &'static str,
|
||||
lore: &'static str,
|
||||
stats: ArmourStats,
|
||||
}
|
||||
|
||||
struct Boots {
|
||||
name: &'static str,
|
||||
lore: &'static str,
|
||||
stats: ArmourStats,
|
||||
}
|
||||
|
||||
struct ArmourStats {
|
||||
durability: i32,
|
||||
max_durability: i32,
|
||||
defence: i32,
|
||||
health_bonus: i32,
|
||||
mana_bonus: i32,
|
||||
}
|
||||
|
||||
struct PlayerStats {}
|
||||
|
||||
trait Skill {
|
||||
fn skill_name(&self) -> &str; // returns the name of the skill
|
||||
fn skill_level(&self) -> &str; // returns the level of that skill
|
||||
fn description(&self) -> &str; // returns the status of that skill
|
||||
fn skillpoint_level_req(&self) -> i32;
|
||||
fn increase_level(&mut self, level: &u32, skill_points: &mut u32) -> Result<(), GameError>;
|
||||
fn decrease_level(&mut self, skill_points: &mut u32) -> Result<(), GameError>;
|
||||
fn modify_stats(&self, stats: EntityStats) -> EntityStats;
|
||||
}
|
||||
|
||||
enum GameError {
|
||||
SkillLevelMaxed,
|
||||
InsufficientSkillPoints,
|
||||
InsufficientLevel,
|
||||
}
|
||||
// use alloc::{boxed::Box, string::String, vec::Vec};
|
||||
// use core::cmp::min;
|
||||
//
|
||||
// struct Player {
|
||||
// username: String,
|
||||
// stats: EntityStats,
|
||||
//
|
||||
// exp: u32,
|
||||
// level: u32,
|
||||
// skill_points: u32,
|
||||
// skills: Vec<Box<dyn Skill>>,
|
||||
//
|
||||
// helmet: Option<Helmet>,
|
||||
// chestplate: Option<Chestplate>,
|
||||
// boots: Option<Boots>,
|
||||
//
|
||||
// inventory: Vec<Item>,
|
||||
// }
|
||||
//
|
||||
// struct EntityStats {
|
||||
// health: i32,
|
||||
// max_health: i32,
|
||||
// mana: i32,
|
||||
// max_mana: i32,
|
||||
// defence: i32,
|
||||
// }
|
||||
//
|
||||
// impl Player {
|
||||
// fn new(username: String) -> Self {
|
||||
// Self {
|
||||
// username,
|
||||
// stats: EntityStats {
|
||||
// health: 100,
|
||||
// max_health: 100,
|
||||
// mana: 100,
|
||||
// max_mana: 100,
|
||||
// defence: 0,
|
||||
// },
|
||||
// exp: 0,
|
||||
// level: 0,
|
||||
// skill_points: 0,
|
||||
// skills: Vec::new(),
|
||||
//
|
||||
// helmet: None,
|
||||
// chestplate: None,
|
||||
// boots: None,
|
||||
// inventory: Vec::new(),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fn heal(&mut self, amount: i32) {
|
||||
// let max_health = self.max_health();
|
||||
//
|
||||
// self.stats.health = min(self.stats.health + amount, max_health);
|
||||
// }
|
||||
//
|
||||
// fn damage(&mut self, amount: i32) {
|
||||
// let hp = self.health_points();
|
||||
// }
|
||||
//
|
||||
// fn inventory_contents_mut(&mut self) -> &mut Vec<Item> {
|
||||
// &mut self.inventory
|
||||
// }
|
||||
//
|
||||
// fn max_health(&self) -> i32 {
|
||||
// let mut max_health = self.stats.max_health;
|
||||
//
|
||||
// if let Some(helmet) = &self.helmet {
|
||||
// max_health += helmet.stats.health_bonus;
|
||||
// }
|
||||
// if let Some(chestplate) = &self.chestplate {
|
||||
// max_health += chestplate.stats.health_bonus;
|
||||
// }
|
||||
// if let Some(boots) = &self.boots {
|
||||
// max_health += boots.stats.health_bonus;
|
||||
// }
|
||||
//
|
||||
// max_health
|
||||
// }
|
||||
//
|
||||
// fn health_points(&self) -> i32 {
|
||||
// let mut hp = self.stats.health;
|
||||
// if let Some(helmet) = &self.helmet {
|
||||
// hp += helmet.stats.health_bonus;
|
||||
// }
|
||||
// if let Some(chestplate) = &self.chestplate {
|
||||
// hp += chestplate.stats.health_bonus;
|
||||
// }
|
||||
// if let Some(boots) = &self.boots {
|
||||
// hp += boots.stats.health_bonus;
|
||||
// }
|
||||
// hp
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// enum Item {
|
||||
// Helmet(Helmet),
|
||||
// Chestplate(Chestplate),
|
||||
// Boots(Boots),
|
||||
// Sword,
|
||||
// Potion,
|
||||
// }
|
||||
//
|
||||
// struct Helmet {
|
||||
// name: &'static str,
|
||||
// lore: &'static str,
|
||||
// stats: ArmourStats,
|
||||
// }
|
||||
//
|
||||
// struct Chestplate {
|
||||
// name: &'static str,
|
||||
// lore: &'static str,
|
||||
// stats: ArmourStats,
|
||||
// }
|
||||
//
|
||||
// struct Boots {
|
||||
// name: &'static str,
|
||||
// lore: &'static str,
|
||||
// stats: ArmourStats,
|
||||
// }
|
||||
//
|
||||
// struct ArmourStats {
|
||||
// durability: i32,
|
||||
// max_durability: i32,
|
||||
// defence: i32,
|
||||
// health_bonus: i32,
|
||||
// mana_bonus: i32,
|
||||
// }
|
||||
//
|
||||
// struct PlayerStats {}
|
||||
//
|
||||
// trait Skill {
|
||||
// fn skill_name(&self) -> &str; // returns the name of the skill
|
||||
// fn skill_level(&self) -> &str; // returns the level of that skill
|
||||
// fn description(&self) -> &str; // returns the status of that skill
|
||||
// fn skillpoint_level_req(&self) -> i32;
|
||||
// fn increase_level(&mut self, level: &u32, skill_points: &mut u32) -> Result<(), GameError>;
|
||||
// fn decrease_level(&mut self, skill_points: &mut u32) -> Result<(), GameError>;
|
||||
// fn modify_stats(&self, stats: EntityStats) -> EntityStats;
|
||||
// }
|
||||
//
|
||||
// enum GameError {
|
||||
// SkillLevelMaxed,
|
||||
// InsufficientSkillPoints,
|
||||
// InsufficientLevel,
|
||||
// }
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::string::String;
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
@@ -6,9 +5,8 @@ use alloc::boxed::Box;
|
||||
use crate::std::application::{Application, Error};
|
||||
use async_trait::async_trait;
|
||||
use crate::std::frame::{ColouredChar, Frame, Position, Dimensions, RenderError};
|
||||
use crate::std::io::{KeyStroke, Screen, Stdin, Color, ColorCode};
|
||||
use crate::std::io::{KeyStroke, Stdin, Color, ColorCode, Display};
|
||||
use crate::std::time::wait;
|
||||
use crate::user::bin::snake::Game;
|
||||
|
||||
pub struct GameOfLife {
|
||||
frame: Frame
|
||||
@@ -25,7 +23,7 @@ impl Application for GameOfLife {
|
||||
}
|
||||
async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
|
||||
// setup:
|
||||
Screen::Application.set_mode();
|
||||
let d = Display::borrow();
|
||||
|
||||
let xoffset = 38;
|
||||
let yoffset = 5;
|
||||
@@ -56,7 +54,6 @@ impl Application for GameOfLife {
|
||||
|
||||
self.mainloop()?;
|
||||
|
||||
Screen::Terminal.set_mode();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ use async_trait::async_trait;
|
||||
use alloc::{boxed::Box, string::String, vec::Vec};
|
||||
|
||||
use crate::{
|
||||
std::os::OS,
|
||||
std::io::{Color, write},
|
||||
println,
|
||||
std::application::{
|
||||
Application,
|
||||
|
||||
+4
-10
@@ -2,17 +2,11 @@ use alloc::string::{String, ToString};
|
||||
use alloc::{format, vec};
|
||||
use alloc::vec::Vec;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::fmt::format;
|
||||
use alloc::sync::Arc;
|
||||
use core::any::Any;
|
||||
use async_trait::async_trait;
|
||||
use spin::Mutex;
|
||||
use crate::{println, serial_println};
|
||||
use crate::std::io::{ColorCode};
|
||||
use crate::shell::command_handler;
|
||||
use crate::std::application::{Application, Error};
|
||||
use crate::std::frame::{self, Frame, Position, Dimensions, ColouredChar, RenderError};
|
||||
use crate::std::io::{Color, KeyStroke, Screen, Stdin};
|
||||
use crate::std::frame::{Frame, Position, Dimensions, ColouredChar, RenderError};
|
||||
use crate::std::io::{KeyStroke, Screen, Stdin};
|
||||
|
||||
use crate::user::lib::libgui::{
|
||||
cg_core::{CgComponent},
|
||||
@@ -100,7 +94,7 @@ impl Application for Grapher {
|
||||
|
||||
while let c = Stdin::keystroke().await {
|
||||
|
||||
let mut entry_widget = container.elements.get("entry_box").unwrap();
|
||||
let entry_widget = container.elements.get("entry_box").unwrap();
|
||||
let mut entry = entry_widget.fetch::<CgLineEdit>().unwrap();
|
||||
|
||||
rerender = true;
|
||||
@@ -196,7 +190,7 @@ impl Grapher {
|
||||
|
||||
let offset_x = point.x + OFFSET_X;
|
||||
let offset_y = point.y + OFFSET_Y;
|
||||
self.frame.write(Position::new(offset_x as usize, 21-offset_y as usize), ColouredChar::new('*'));
|
||||
self.frame.write(Position::new(offset_x as usize, 21-offset_y as usize), ColouredChar::new('*')).expect("Failed to write to frame - this function is broken.");
|
||||
}
|
||||
|
||||
fn reset_frame(&mut self) {
|
||||
|
||||
+56
-12
@@ -1,12 +1,15 @@
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use core::any::Any;
|
||||
use async_trait::async_trait;
|
||||
use core::fmt::Write;
|
||||
use crate::std::application::{Application, Error};
|
||||
use crate::std;
|
||||
use crate::std::frame::{ColouredChar, Dimensions, Frame, Position, RenderError};
|
||||
use crate::std::io::{Display, KeyStroke, Stdin};
|
||||
use crate::user::lib::libgui::cg_core::CgComponent;
|
||||
|
||||
struct Game {
|
||||
pub(crate) struct Game {
|
||||
ball: Ball,
|
||||
player1: Player,
|
||||
player2: Player,
|
||||
@@ -18,40 +21,81 @@ impl Application for Game {
|
||||
Game {
|
||||
ball: Ball::new(),
|
||||
player1: Player::new(1),
|
||||
player2: Player::new(2),
|
||||
player2: Player::new(78),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&mut self, _: Vec<String>) -> Result<(), Error> {
|
||||
let d = Display::borrow();
|
||||
|
||||
loop {
|
||||
self.ball.update(&self.player1, &self.player2);
|
||||
std::time::wait(0.01);
|
||||
|
||||
if let Some(key) = Stdin::try_keystroke() {
|
||||
match key {
|
||||
KeyStroke::Char('w') => {
|
||||
self.player1.pos.y -= 1;
|
||||
},
|
||||
KeyStroke::Char('s') => {
|
||||
self.player1.pos.y += 1;
|
||||
},
|
||||
KeyStroke::Up => {
|
||||
self.player2.pos.y -= 1;
|
||||
},
|
||||
KeyStroke::Down => {
|
||||
self.player2.pos.y += 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if let Ok(frame) = self.render() {
|
||||
frame.write_to_screen().unwrap();
|
||||
}
|
||||
// self.ball.update(&self.player1, &self.player2);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl CgComponent for Game {
|
||||
fn render(&self) -> Result<Frame, RenderError> {
|
||||
let mut frame = Frame::new(Dimensions::new(0, 0), Dimensions::new(80, 25))?;
|
||||
|
||||
frame.write(self.player1.pos, ColouredChar::new('@')).unwrap();
|
||||
frame.write(self.player2.pos, ColouredChar::new('@')).unwrap();
|
||||
frame.write(self.ball.pos, ColouredChar::new('@')).unwrap();
|
||||
|
||||
Ok(frame)
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
struct Player {
|
||||
x: i32,
|
||||
y: i32,
|
||||
pos: Position,
|
||||
score: i32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
fn new(y: i32) -> Self {
|
||||
Player { x: 0, y, score: 0 }
|
||||
fn new(x: usize) -> Self {
|
||||
Player { pos: Position::new(x, 12), score: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
struct Ball {
|
||||
x: i32,
|
||||
y: i32,
|
||||
pos: Position,
|
||||
vx: i32,
|
||||
vy: i32,
|
||||
}
|
||||
|
||||
impl Ball {
|
||||
fn new() -> Self {
|
||||
Ball { x: 0, y: 0 }
|
||||
Ball { pos: Position::new(40, 12), vx: 1, vy: 0 }
|
||||
}
|
||||
|
||||
fn update(&mut self, player1: &Player, player2: &Player) {
|
||||
self.x += 1;
|
||||
self.pos.x = (self.pos.x as i32 + self.vx) as usize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ const RICKROLL2: &str = "
|
||||
1a###w#*mObo*oatXkW*oo#*###p `--' `---' `----' `-----' | |-'
|
||||
`--'";
|
||||
|
||||
use crate::{println, serial_println};
|
||||
use crate::println;
|
||||
use alloc::{string::String, boxed::Box, vec::Vec};
|
||||
|
||||
|
||||
|
||||
+8
-13
@@ -1,33 +1,26 @@
|
||||
use async_trait::async_trait;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
|
||||
use alloc::{boxed::Box, format, string::{String, ToString}, vec, vec::Vec};
|
||||
use futures_util::TryFutureExt;
|
||||
use vga::writers::{GraphicsWriter, PrimitiveDrawing};
|
||||
use vga::writers::{PrimitiveDrawing};
|
||||
|
||||
use crate::{
|
||||
print,
|
||||
printerr,
|
||||
println,
|
||||
serial_println,
|
||||
};
|
||||
|
||||
use crate::std::{
|
||||
application::{Application, Error, Exit},
|
||||
time::{timer, wait},
|
||||
random::Random,
|
||||
io::{
|
||||
Color, write, Screen, Stdin, Serial, KeyStroke
|
||||
},
|
||||
frame::{Dimensions, Position, ColorCode},
|
||||
};
|
||||
|
||||
use crate::user::{
|
||||
lib::libgui::{
|
||||
cg_core::{CgComponent, CgTextEdit, CgKeyboardCapture, CgTextInput, Widget},
|
||||
cg_widgets::{CgTextBox, CgContainer, CgLabel, CgStatusBar, CgDialog},
|
||||
cg_inputs::CgLineEdit,
|
||||
cg_core::{CgComponent, CgKeyboardCapture},
|
||||
cg_widgets::CgDialog,
|
||||
},
|
||||
bin::*,
|
||||
};
|
||||
@@ -140,6 +133,9 @@ async fn exec() -> Result<(), Error> {
|
||||
let mut asteroid_game = asteroids::Game::new();
|
||||
asteroid_game.run(args).await?;
|
||||
}
|
||||
"pong" => {
|
||||
pong::Game::new().run(args).await?;
|
||||
}
|
||||
"serial" => {
|
||||
let c = Serial::reply_char('e');
|
||||
println!("{}", c);
|
||||
@@ -149,8 +145,8 @@ async fn exec() -> Result<(), Error> {
|
||||
game.run(Vec::new()).await?;
|
||||
}
|
||||
"tetris" => {
|
||||
let mut game = tetris::TetrisEngine::new();
|
||||
game.run(Vec::new()).await?;
|
||||
// let mut game = tetris::TetrisEngine::new();
|
||||
// game.run(Vec::new()).await?;
|
||||
}
|
||||
|
||||
"gigachad?" => {
|
||||
@@ -192,7 +188,6 @@ async fn exec() -> Result<(), Error> {
|
||||
};
|
||||
}
|
||||
"time" => {
|
||||
use crate::std::time::timer;
|
||||
timer();
|
||||
}
|
||||
"test_features" => {
|
||||
|
||||
+6
-16
@@ -1,19 +1,13 @@
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::string::String;
|
||||
use alloc::{format, vec, vec::Vec, boxed::Box};
|
||||
use alloc::borrow::ToOwned;
|
||||
use core::arch::x86_64::_mm_test_all_ones;
|
||||
use core::cell::RefCell;
|
||||
use async_trait::async_trait;
|
||||
use crate::std::io::{Color, KeyStroke, Screen, Stdin};
|
||||
use crate::std::io::{Color, Display, KeyStroke, Stdin};
|
||||
use crate::std::time;
|
||||
use crossbeam_queue::SegQueue;
|
||||
use lazy_static::lazy_static;
|
||||
use crate::{println, serial_println};
|
||||
use crate::std::application::{Application, Error};
|
||||
use crate::std::frame::{ColouredChar, Dimensions, Frame, RenderError, ColorCode};
|
||||
use crate::std::random::Random;
|
||||
use crate::system::std::frame;
|
||||
use super::super::lib::coords::{Line, Position, Direction};
|
||||
use super::super::lib::coords::{Position, Direction};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Gamemode {
|
||||
@@ -53,10 +47,10 @@ impl Application for Game {
|
||||
|
||||
async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
|
||||
|
||||
let mut settings = [0, 0, 0]; // ai_count, snake_len, poi_count
|
||||
let settings = [0, 0, 0]; // ai_count, snake_len, poi_count
|
||||
|
||||
if args.len() == 0 {
|
||||
self.gamemode == Gamemode::SinglePlayer;
|
||||
self.gamemode = Gamemode::SinglePlayer;
|
||||
} else {
|
||||
match args[0].as_str() {
|
||||
"easy" => {
|
||||
@@ -81,13 +75,11 @@ impl Application for Game {
|
||||
self.prepare();
|
||||
|
||||
// switch OS to application mode
|
||||
Screen::Application.set_mode();
|
||||
let d = Display::borrow();
|
||||
// render the initial state of the screen.
|
||||
self.render().map_err(|_| Error::ApplicationError(String::from("failed to render game screen")))?;
|
||||
// run the game
|
||||
self.gameloop().await?;
|
||||
// return to the terminal
|
||||
Screen::Terminal.set_mode();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -356,8 +348,6 @@ impl PathFinder {
|
||||
// serial_println!("{:?} {:?} {:?} {:?}", nearest_poi, rel_pos, head, optimal);
|
||||
return optimal;
|
||||
}
|
||||
|
||||
Direction::None
|
||||
}
|
||||
|
||||
fn optimal_move(head: &Position, rel_pos: &Position, moves: &Vec<Direction>) -> Direction {
|
||||
|
||||
@@ -3,12 +3,11 @@ use crate::std::application::{
|
||||
Application,
|
||||
Error
|
||||
};
|
||||
use crate::{print, println};
|
||||
use crate::println;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use async_trait::async_trait;
|
||||
use alloc::{
|
||||
string::ToString,
|
||||
borrow::ToOwned,
|
||||
};
|
||||
|
||||
@@ -101,13 +100,13 @@ println!("\n-------------------------------------");
|
||||
|
||||
impl Tasks {
|
||||
fn add_task(&mut self, content: String) {
|
||||
TASKS.lock().add(content);
|
||||
TASKS.lock().add(content).unwrap();
|
||||
}
|
||||
fn remove_task(&self, idx: usize) {
|
||||
TASKS.lock().remove(idx);
|
||||
TASKS.lock().remove(idx).unwrap();
|
||||
}
|
||||
fn select_task(&self, idx: i32) {
|
||||
TASKS.lock().select(idx);
|
||||
TASKS.lock().select(idx).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+112
-114
@@ -1,117 +1,115 @@
|
||||
use async_trait::async_trait;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::String;
|
||||
use alloc::vec;
|
||||
use alloc::vec::Vec;
|
||||
use crate::std::frame::{ColouredChar};
|
||||
use crate::{serial_print, serial_println};
|
||||
use crate::std::application::{Application, Error};
|
||||
use crate::std::io::Screen;
|
||||
use crate::user::lib::coords::{Direction, Position, PositionReal};
|
||||
|
||||
|
||||
pub(crate) struct TetrisEngine {
|
||||
score: u32,
|
||||
next: TetrisPiece,
|
||||
completed_frame: [[ColouredChar; 80]; 25], // this frame does not contain falling blocks, only static ones
|
||||
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Application for TetrisEngine {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
score: 0,
|
||||
next: TetrisPiece::new(PieceType::OPiece),
|
||||
completed_frame: [[ColouredChar::null(); 80]; 25],
|
||||
}
|
||||
}
|
||||
async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
|
||||
// setup:
|
||||
Screen::Application.set_mode();
|
||||
|
||||
let piece_type = PieceType::OPiece;
|
||||
let mut piece = TetrisPiece::new(piece_type);
|
||||
|
||||
serial_println!("{:?}", piece.get_positions());
|
||||
piece.rotate_right();
|
||||
serial_println!("{:?}", piece.get_positions());
|
||||
|
||||
|
||||
Screen::Terminal.set_mode();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
enum PieceType {
|
||||
OPiece,
|
||||
IPiece,
|
||||
JPiece,
|
||||
LPiece,
|
||||
SPiece,
|
||||
ZPiece,
|
||||
}
|
||||
|
||||
struct TetrisPiece {
|
||||
type_: PieceType,
|
||||
pos: Position,
|
||||
rotation: Direction,
|
||||
}
|
||||
|
||||
impl TetrisPiece {
|
||||
fn new(type_: PieceType) -> Self {
|
||||
Self {
|
||||
type_,
|
||||
pos: Position { x: 40, y: 30 },
|
||||
rotation: Direction::Degrees0,
|
||||
}
|
||||
}
|
||||
fn rotate_right(&mut self) {
|
||||
self.rotation = match self.rotation {
|
||||
Direction::Degrees90 => Direction::Degrees180,
|
||||
Direction::Degrees180 => Direction::Degrees270,
|
||||
Direction::Degrees270 => Direction::Degrees0,
|
||||
Direction::Degrees0 => Direction::Degrees90,
|
||||
Direction::None => panic!("direction should never be none in this application"),
|
||||
};
|
||||
}
|
||||
|
||||
/// function that maps the coordinates of the object.
|
||||
fn get_positions(&self) -> Vec<Position> {
|
||||
match self.type_ {
|
||||
PieceType::OPiece => {
|
||||
let positions = vec![
|
||||
PositionReal { x: -0.5, y: -0.5 },
|
||||
PositionReal { x: 0.5, y: -0.5 },
|
||||
PositionReal { x: -0.5, y: 0.5 },
|
||||
PositionReal { x: 0.5, y: 0.5 },
|
||||
];
|
||||
positions.into_iter().map(|p|
|
||||
( p.rotate(self.rotation.clone()) + self.pos.clone().real() + PositionReal { x: -0.5, y: 0.5 } ).integer()
|
||||
).collect::<Vec<Position>>()
|
||||
}
|
||||
_ => unimplemented!("E"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// use async_trait::async_trait;
|
||||
// use alloc::boxed::Box;
|
||||
// use alloc::string::String;
|
||||
// use alloc::vec;
|
||||
// use alloc::vec::Vec;
|
||||
// use crate::std::frame::{ColouredChar};
|
||||
// use crate::{serial_print, serial_println};
|
||||
// use crate::std::application::{Application, Error};
|
||||
// use crate::std::io::{Display, Screen};
|
||||
// use crate::user::lib::coords::{Direction, Position, PositionReal};
|
||||
//
|
||||
//
|
||||
// pub(crate) struct TetrisEngine {
|
||||
// score: u32,
|
||||
// next: TetrisPiece,
|
||||
// completed_frame: [[ColouredChar; 80]; 25], // this frame does not contain falling blocks, only static ones
|
||||
//
|
||||
// }
|
||||
//
|
||||
// #[async_trait]
|
||||
// impl Application for TetrisEngine {
|
||||
// fn new() -> Self {
|
||||
// Self {
|
||||
// score: 0,
|
||||
// next: TetrisPiece::new(PieceType::OPiece),
|
||||
// completed_frame: [[ColouredChar::null(); 80]; 25],
|
||||
// }
|
||||
// }
|
||||
// async fn run(&mut self, args: Vec<String>) -> Result<(), Error> {
|
||||
// // setup:
|
||||
// let d = Display::borrow();
|
||||
//
|
||||
// let piece_type = PieceType::OPiece;
|
||||
// let mut piece = TetrisPiece::new(piece_type);
|
||||
//
|
||||
// serial_println!("{:?}", piece.get_positions());
|
||||
// piece.rotate_right();
|
||||
// serial_println!("{:?}", piece.get_positions());
|
||||
//
|
||||
// Ok(())
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// enum PieceType {
|
||||
// OPiece,
|
||||
// IPiece,
|
||||
// JPiece,
|
||||
// LPiece,
|
||||
// SPiece,
|
||||
// ZPiece,
|
||||
// }
|
||||
//
|
||||
// struct TetrisPiece {
|
||||
// type_: PieceType,
|
||||
// pos: Position,
|
||||
// rotation: Direction,
|
||||
// }
|
||||
//
|
||||
// impl TetrisPiece {
|
||||
// fn new(type_: PieceType) -> Self {
|
||||
// Self {
|
||||
// type_,
|
||||
// pos: Position { x: 40, y: 30 },
|
||||
// rotation: Direction::Degrees0,
|
||||
// }
|
||||
// }
|
||||
// fn rotate_right(&mut self) {
|
||||
// self.rotation = match self.rotation {
|
||||
// Direction::Degrees90 => Direction::Degrees180,
|
||||
// Direction::Degrees180 => Direction::Degrees270,
|
||||
// Direction::Degrees270 => Direction::Degrees0,
|
||||
// Direction::Degrees0 => Direction::Degrees90,
|
||||
// Direction::None => panic!("direction should never be none in this application"),
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// /// function that maps the coordinates of the object.
|
||||
// fn get_positions(&self) -> Vec<Position> {
|
||||
// match self.type_ {
|
||||
// PieceType::OPiece => {
|
||||
// let positions = vec![
|
||||
// PositionReal { x: -0.5, y: -0.5 },
|
||||
// PositionReal { x: 0.5, y: -0.5 },
|
||||
// PositionReal { x: -0.5, y: 0.5 },
|
||||
// PositionReal { x: 0.5, y: 0.5 },
|
||||
// ];
|
||||
// positions.into_iter().map(|p|
|
||||
// ( p.rotate(self.rotation.clone()) + self.pos.clone().real() + PositionReal { x: -0.5, y: 0.5 } ).integer()
|
||||
// ).collect::<Vec<Position>>()
|
||||
// }
|
||||
// _ => unimplemented!("E"),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user