fixed stuff

no progress 💀
This commit is contained in:
FantasyPvP
2023-11-27 00:21:34 +00:00
parent b16ce4a6f2
commit 417833fc41
15 changed files with 40 additions and 423 deletions
+23
View File
@@ -0,0 +1,23 @@
use crate::system::std::application::Application;
use async_trait::async_trait;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use crate::std::application::Error;
pub struct Player {
pub health: u32,
pub score: u32
}
impl Player {
pub fn new() -> Player {
Player {
health: 5,
score: 0
}
}
}
pub struct Game {
pub player: Player
}
+2
View File
@@ -0,0 +1,2 @@
mod render;
mod game;
+2
View File
@@ -0,0 +1,2 @@
use crate::println;
-53
View File
@@ -1,53 +0,0 @@
use super::entity::Enemy;
use alloc::vec::Vec;
pub enum Event {
PlayerKilled,
EntityKilled(Enemy),
}
pub enum Choice<A, B> {
A(A),
B(B),
}
impl core::fmt::Display for Event {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Event::PlayerKilled => write!(f, "Player killed!"),
Event::EntityKilled(x) => write!(f, "Entity killed! {}", x),
}
}
}
impl<A, B> core::fmt::Display for Choice<A, B> where
A: core::fmt::Display,
B: core::fmt::Display
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Choice::A(a) => write!(f, "{}", a),
Choice::B(b) => write!(f, "{}", b),
}
}
}
pub fn eventcheck<A>(e: (A, Option<Vec<Event>>)) -> Choice<A, Event> {
match e.1 {
Some(events) => {
for event in events {
match event {
Event::PlayerKilled => {
return Choice::B(event)
}
Event::EntityKilled(entity) => {
return Choice::B(event)
}
}
}
},
None => (),
};
Choice::A(e.0)
}
-102
View File
@@ -1,102 +0,0 @@
use super::player::Player;
use super::engine::Event;
use alloc::{string::String, vec::Vec, vec};
use crate::std::random;
pub trait Entity {
fn attack_entity(&mut self, _: &mut EntityObject) -> (AttackResult, Option<Vec<Event>>) {
(AttackResult::Miss, None)
}
}
pub enum EntityObject<'a> {
Player(&'a mut Player),
Enemy(&'a mut Enemy),
}
#[derive(Debug, Clone, Copy)]
pub enum AttackResult {
Miss,
GlancingBlow(f64),
Hit(f64),
CriticalHit(f64),
FriendlyFire,
}
impl core::fmt::Display for AttackResult {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
AttackResult::Miss => write!(f, "Missed!"),
AttackResult::GlancingBlow(damage) => write!(f, "Glancing Blow: {}", damage),
AttackResult::Hit(damage) => write!(f, "Hit: {}", damage),
AttackResult::CriticalHit(damage) => write!(f, "Critical Hit: {}", damage),
AttackResult::FriendlyFire => write!(f, "Friendly Fire (no damage dealt)!"),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Enemy {
pub health_points: f64,
pub max_health_points: f64,
pub base_attack_damage: f64,
pub speed: f64,
}
impl Enemy {
pub fn new() -> Self {
Self {
health_points: 200.0,
max_health_points: 200.0,
base_attack_damage: 5.0,
speed: 100.0,
}
}
}
impl core::fmt::Display for Enemy {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Enemy: {}/{}", self.health_points, self.max_health_points)
}
}
impl Entity for Enemy {
fn attack_entity(&mut self, target: &mut EntityObject) -> (AttackResult, Option<Vec<Event>>) {
let mut entity = if let EntityObject::Player(player) = target {
player
} else {
return (AttackResult::FriendlyFire, None);
};
// combat implementation
let dmg: f64;
let r = random::Random::int(0, 125) as f64;
let rs = self.speed / entity.speed * 100 as f64;
let attack = if r < rs * 0.2 {
dmg = self.base_attack_damage * 1.5;
entity.health_points -= dmg;
AttackResult::CriticalHit(dmg)
} else if r < rs * 0.8 {
dmg = self.base_attack_damage;
entity.health_points -= dmg;
AttackResult::Hit(dmg)
} else if r < rs {
dmg = self.base_attack_damage * 0.5;
entity.health_points -= dmg;
AttackResult::GlancingBlow(dmg)
} else {
AttackResult::Miss
};
if entity.health_points <= 0.0 {
return (attack, Some(vec![Event::PlayerKilled]));
} else {
return (attack, None)
}
}
}
-129
View File
@@ -1,129 +0,0 @@
use async_trait::async_trait;
use super::{
engine::{Choice, Event, eventcheck},
entity::{Enemy, Entity, EntityObject},
player::Player,
};
use alloc::{borrow::ToOwned, format, string::{String, ToString}, vec::Vec, boxed::Box};
use crate::{
std::{
io::{self, println, serial_println},
random,
},
std::application::{
Application,
Error,
},
};
use crate::std::io::{KeyStroke, Stdin};
pub struct GameLoop;
#[async_trait]
impl Application for GameLoop {
fn new() -> Self {
Self {}
}
async fn run(&mut self, _args: Vec<String>) -> Result<(), Error> {
let mut username: String = io::Stdin::readline().await;
username = username.trim().to_string();
let mut player = Player::new(username);
let mut enemy = Enemy::new();
for _ in 0..30 {
match (eventcheck(player.attack_entity(&mut EntityObject::Enemy(&mut enemy)))) {
Choice::A(result) => {
println!("{}", result);
},
Choice::B(event) => {
println!("{}", event);
match event {
Event::PlayerKilled => {
println!(" [!] {} was slain by Enemy\n\n[ You lost! ]", player.username);
break;
}
Event::EntityKilled(entity) => {
println!("\n [!] Enemy was slain by {}\n\n [ You won! ]", player.username);
break;
}
}
}
}
println!("{}", eventcheck(enemy.attack_entity(&mut EntityObject::Player(&mut player))));
println!("[{}\n[{}", player, enemy);
}
// FRAMEGEN.lock().render_frame();
let string = String::from(format!(
"┌────────────────────────────┐
{}
{} / {}
└────────────────────────────┘"
, player.username, player.health_points, player.max_health_points));
// let mut healthbar = Element::from_str(string);
// healthbar.render((1, 1));
//
// let new2 = String::from("[an element]");
// let mut new = Element::from_str(new2);
//
//
// new.render((10, 10));
// new.render((10, 15));
// new.render((5, 20));
// new.render((34, 16));
//
//
// FRAMEGEN.lock().render_frame();
//
// let fr = FRAMEGEN.lock().get_frame().to_owned();
// serial_println!("{}", {
// let mut string = String::new();
// for row in fr {
// let mut r = String::new();
// for col in row {
// r.push(col.character as char);
// }
// string.push_str(&r);
// string.push('\n')
// };
// string
// });
loop {
if let KeyStroke::Char(c) = Stdin::keystroke().await {
println!("{}", c)
}
}
Ok(())
}
}
fn random() -> u64 {
let r = random::Random::int(0, 125) as u64;
r
}
-19
View File
@@ -1,19 +0,0 @@
#[derive(Debug, Clone, Copy)]
pub struct Armour;
#[derive(Debug, Clone, Copy)]
pub struct Weapon;
#[derive(Debug, Clone, Copy)]
pub struct Charm;
#[derive(Debug, Clone, Copy)]
pub struct OtherItem;
#[derive(Debug, Clone, Copy)]
pub enum Item {
Armour(Armour),
Weapon(Weapon),
Charm(Charm),
Other(OtherItem),
}
-6
View File
@@ -1,6 +0,0 @@
pub mod player;
pub mod items;
pub mod entity;
pub mod engine;
pub mod renderer;
pub mod init;
-82
View File
@@ -1,82 +0,0 @@
use super::{
items::{Item, Armour},
entity::{Entity, EntityObject, AttackResult},
engine::Event,
};
use alloc::{string::String, vec::Vec, vec};
use crate::std::random;
pub struct Player {
pub username: String,
pub health_points: f64,
pub max_health_points: f64,
pub base_attack_damage: f64,
pub speed: f64,
pub inventory: [ Item ; 15 ],
pub equipped: [ Item ; 7 ], // helmet, chestplate, leggings, boots, mainhand, offhand, charm
}
impl Player {
pub fn new(username: String) -> Self {
Self {
username,
health_points: 100.0,
max_health_points: 100.0,
base_attack_damage: 10.0,
speed: 100.0,
inventory: [ Item::Armour(Armour {}); 15 ],
equipped: [ Item::Armour(Armour {}); 7 ],
}
}
}
impl core::fmt::Display for Player {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}: {}/{}", self.username, self.health_points, self.max_health_points)
}
}
impl Entity for Player {
fn attack_entity(&mut self, target: &mut EntityObject) -> (AttackResult, Option<Vec<Event>>) {
let mut entity = if let EntityObject::Enemy(enemy) = target {
enemy
} else {
return (AttackResult::FriendlyFire, None);
};
// combat implementation
let dmg: f64;
let r = random::Random::int(0, 125) as f64;
let rs = self.speed / entity.speed * 100 as f64;
let attack = if r < rs * 0.2 {
dmg = self.base_attack_damage * 1.5;
entity.health_points -= dmg;
AttackResult::CriticalHit(dmg)
} else if r < rs * 0.8 {
dmg = self.base_attack_damage;
entity.health_points -= dmg;
AttackResult::Hit(dmg)
} else if r < rs {
dmg = self.base_attack_damage * 0.5;
entity.health_points -= dmg;
AttackResult::GlancingBlow(dmg)
} else {
AttackResult::Miss
};
if entity.health_points <= 0.0 {
return (attack, Some(vec![Event::EntityKilled(entity.clone())]));
} else {
return (attack, None)
}
}
}
+4 -3
View File
@@ -13,10 +13,11 @@ use crate::std::frame::{self, Frame, Position, Dimensions, ColouredChar, RenderE
use crate::std::io::{Color, KeyStroke, Screen, Stdin}; use crate::std::io::{Color, KeyStroke, Screen, Stdin};
use crate::user::lib::libgui::{ use crate::user::lib::libgui::{
cg_core::{CgComponent, CgTextInput, CgInputHandler, CgContainer}, cg_core::{CgComponent},
cg_widgets::CgContainerWidget, cg_widgets::CgContainer,
cg_inputs::CgLineEdit, cg_inputs::CgLineEdit,
}; };
use crate::user::lib::libgui::cg_core::CgTextEdit;
use super::calc; use super::calc;
@@ -79,7 +80,7 @@ impl Application for Grapher {
let mut commandresult = String::new(); let mut commandresult = String::new();
while let c = Stdin::keystroke().await { while let c = Stdin::keystroke().await {
let mut container = CgContainerWidget::new( let mut container = CgContainer::new(
Position::new(0, 0), Position::new(0, 0),
Dimensions::new(80, 25), Dimensions::new(80, 25),
true, true,
+1 -1
View File
@@ -1,5 +1,4 @@
pub mod calc; pub mod calc;
pub mod crystal_rpg;
pub mod crystalfetch; pub mod crystalfetch;
pub mod rickroll; pub mod rickroll;
pub mod shell; pub mod shell;
@@ -11,3 +10,4 @@ mod snake;
mod grapher; mod grapher;
mod gameoflife; mod gameoflife;
mod tetris; mod tetris;
mod asteroids;
+4 -12
View File
@@ -3,23 +3,19 @@ use lazy_static::lazy_static;
use spin::Mutex; use spin::Mutex;
use alloc::{boxed::Box, string::{String, ToString}, vec, vec::Vec}; use alloc::{boxed::Box, string::{String, ToString}, vec, vec::Vec};
use core::future::Future;
use vga::writers::{GraphicsWriter, PrimitiveDrawing}; use vga::writers::{GraphicsWriter, PrimitiveDrawing};
use crate::{print, printerr, println, serial_println, std, std::application::{Application, Error}, user::bin::*}; use crate::{print, printerr, println, serial_println, std, std::application::{Application, Error}, user::bin::*};
use crate::kernel::render::ColorCode; use crate::std::frame::{Dimensions, Position, ColorCode};
use crate::std::frame::{Dimensions, Position};
use crate::std::io::{Color, write, Screen, Stdin, Serial, KeyStroke}; use crate::std::io::{Color, write, Screen, Stdin, Serial, KeyStroke};
use crate::std::random::Random; use crate::std::random::Random;
use crate::user::bin::gigachad_detector::GigachadDetector; use crate::user::bin::gigachad_detector::GigachadDetector;
use crate::user::bin::grapher::Grapher; use crate::user::bin::grapher::Grapher;
use crate::user::lib::libgui::{ use crate::user::lib::libgui::{
cg_core::{CgComponent}, cg_core::{CgComponent, CgTextEdit},
cg_widgets::{CgTextBox, CgContainer}, cg_widgets::{CgTextBox, CgContainer, CgIndicatorBar, CgIndicatorWidget, CgLabel, CgStatusBar},
cg_inputs::CgLineEdit,
}; };
use crate::user::lib::libgui::cg_core::CgTextEdit;
use crate::user::lib::libgui::cg_inputs::CgLineEdit;
use crate::user::lib::libgui::cg_widgets::{CgIndicatorBar, CgIndicatorWidget, CgLabel, CgStatusBar};
lazy_static! { lazy_static! {
pub static ref CMD: Mutex<CommandHandler> = Mutex::new(CommandHandler::new()); pub static ref CMD: Mutex<CommandHandler> = Mutex::new(CommandHandler::new());
@@ -110,10 +106,6 @@ async fn exec() -> Result<(), Error> {
let mut cmd = tasks::Tasks::new(); let mut cmd = tasks::Tasks::new();
cmd.run(args).await?; cmd.run(args).await?;
} }
"play" => {
let mut gameloop = crystal_rpg::init::GameLoop::new();
gameloop.run(args).await?;
}
"VGA" => { "VGA" => {
use vga::colors::Color16; use vga::colors::Color16;
use vga::writers::{GraphicsWriter, Graphics640x480x16}; use vga::writers::{GraphicsWriter, Graphics640x480x16};
+3 -14
View File
@@ -3,9 +3,10 @@ use alloc::string::String;
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::slice::from_mut; use core::slice::from_mut;
use crate::kernel::render::{ColorCode, RenderError, ScreenChar};
use crate::{printerr, serial_println}; use crate::{printerr, serial_println};
use crate::std::frame::{ColouredChar, Dimensions, Position, special_char, Frame}; use crate::std::frame::{ColouredChar, Dimensions, Position, special_char, Frame, RenderError, ColorCode};
use crate::user::lib::libgui::cg_inputs::CgLineEdit;
use crate::user::lib::libgui::cg_widgets::{CgContainer, CgTextBox, CgIndicatorBar, CgIndicatorWidget, CgLabel, CgStatusBar};
/// implement this trait if you require the widget to be able to have an outline /// implement this trait if you require the widget to be able to have an outline
pub trait CgOutline: CgComponent { pub trait CgOutline: CgComponent {
@@ -25,15 +26,3 @@ pub trait CgTextEdit: CgComponent {
fn move_cursor(&mut self, direction: bool); // true = right, false = left fn move_cursor(&mut self, direction: bool); // true = right, false = left
fn clear(&mut self); fn clear(&mut self);
} }
+1 -2
View File
@@ -2,12 +2,11 @@ use alloc::{boxed::Box, format, string::String, vec, vec::Vec};
use alloc::fmt::format; use alloc::fmt::format;
use alloc::string::ToString; use alloc::string::ToString;
use core::cmp::{max, min}; use core::cmp::{max, min};
use crate::kernel::render::{ColorCode, RenderError};
use crate::serial_println; use crate::serial_println;
use super::cg_core::{ use super::cg_core::{
CgComponent, CgOutline CgComponent, CgOutline
}; };
use crate::std::frame::{ColouredChar, Dimensions, Position, Frame}; use crate::std::frame::{ColouredChar, Dimensions, Position, Frame, RenderError, ColorCode};
use crate::std::io::Color; use crate::std::io::Color;
pub struct CgContainer<'a> { pub struct CgContainer<'a> {