made a game

made a snake game and rewrote some rendering stuff again
This commit is contained in:
FantasyPvP
2023-10-02 00:56:18 +01:00
parent f0ee584c87
commit bf9c9be88d
19 changed files with 532 additions and 241 deletions
+117 -51
View File
@@ -4,11 +4,7 @@ use lazy_static::lazy_static;
use spin::Mutex;
use x86_64::instructions::interrupts;
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use alloc::{boxed::Box, string::{String, ToString}, vec, vec::Vec};
use crate::{
kernel::tasks::{executor::Executor, Task},
@@ -16,6 +12,8 @@ use crate::{
std::io::{print, println, Stdin, Screen},
user::bin::*,
};
use crate::std::io::{Color, write};
use crate::user::bin::gigachad_detector::GigachadDetector;
use super::*;
@@ -34,59 +32,127 @@ use super::*;
/// starts the shell
/// this function should be directly called by main.rs or by an init system
fn new_function() {
}
pub fn userspace() -> Result<(), String> {
let mut executor = Executor::new();
//
// executor.spawn(Task::new(new_function()));
// loop {
// executor.try_run()
// }
fn run_task(task_name: String, args: Vec<String>) -> Result<(), String> {
Ok(())
}
// struct Shell {}
//
// impl Application for Shell {
// fn new() -> Shell {
// Shell {}
// }
// async fn run(&mut self, _: Vec<String>) -> Result<(), Error> {
// Ok(())
// }
// }
pub async fn userspace() -> Result<(), String> {
let mut executor = Executor::new();
let mut shell = Shell::new();
shell.run(vec![]).await.unwrap();
fn parse_args(command: String) -> Result<(String, Vec<String>), String> {
let mut args: Vec<String> = Vec::new();
for arg in command.split(" ").collect::<Vec<&str>>() {
match arg {
"" => {}
x => args.push(x.to_string()),
}
}
let cmd: String;
if args.len() > 0 {
cmd = args[0].clone();
args.remove(0);
} else {
return Err("command was empty.".to_string());
};
Ok((cmd, args))
Ok(())
}
struct Shell {
history: Vec<String>,
}
#[async_trait]
impl Application for Shell {
fn new() -> Shell {
Shell {
history: Vec::new(),
}
}
async fn run(&mut self, _: Vec<String>) -> Result<(), Error> {
loop {
self.prompt();
let input = Stdin::readline().await;
let (cmd, args) = self.parse_args(input).unwrap();
self.run_cmd(cmd, args).await.unwrap();
}
}
}
impl Shell {
fn prompt(&mut self) {
write(format_args!("\n Crystal> "), (Color::Cyan, Color::Black));
}
// fn exec<R, T: Fn() -> R>(command: T) -> Result<R, Error> { // this command runs when a shell command is executed
// Ok(command())
// }
async fn run_cmd(&mut self, cmd: String, args: Vec<String>) -> Result<(), Error> {
match cmd.as_str() {
"calculate" | "calc" | "solve" => {
let mut cmd = calc::Calculator::new();
cmd.run(args).await?;
}
"rickroll" => {
let mut cmd = rickroll::Rickroll::new();
cmd.run(args).await?;
}
"crystalfetch" => {
let mut cmd = crystalfetch::CrystalFetch::new();
cmd.run(args).await?;
}
"tasks" => {
let mut cmd = tasks::Tasks::new();
cmd.run(args).await?;
}
"play" => {
let mut gameloop = crystal_rpg::init::GameLoop::new();
gameloop.run(args).await?;
}
"echo" => {
println!(
"Crystal: '{}'",
" ".join(args)
)
}
"clear" => {
Screen::clear();
}
"print" => {
use crate::std::os::OS;
let x: String = OS.lock().version.clone();
println!("{}", x);
}
"snake" => {
let mut game = snake::Game::new();
game.run(Vec::new()).await?;
}
"gigachad?" => {
let mut gigachad_detector = GigachadDetector::new();
gigachad_detector.run(args).await?;
}
"test_features" => {
use crate::std::random::Random;
println!("{}", Random::int(0, 10));
}
_ => {
return Err(Error::UnknownCommand(
"command not yet implemented".to_string(),
))
}
}
Ok(())
}
fn parse_args(&self, command: String) -> Result<(String, Vec<String>), String> {
let mut args: Vec<String> = Vec::new();
for arg in command.split(" ").collect::<Vec<&str>>() {
match arg {
"" => {}
x => args.push(x.to_string()),
}
}
let cmd: String;
if args.len() > 0 {
cmd = args[0].clone();
args.remove(0);
}
else {
return Err("command was empty.".to_string());
};
Ok((cmd, args))
}
}
//fn run_binary(binary: dyn Application) -> Result<Vec<String>, String> {
// binary.run();
// Ok(Vec::<String::new()>)
//}