Last active
February 20, 2023 17:59
-
-
Save David-Else/5164ab1f654b1a8b3a6a13c7f9bf2762 to your computer and use it in GitHub Desktop.
centos8 ultimate setup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crossterm::style::StyledContent; | |
use crossterm::style::Stylize; | |
#[derive(Debug, Clone, Copy)] | |
pub struct Position { | |
pub x: u16, | |
pub y: u16, | |
} | |
impl PartialEq for Position { | |
fn eq(&self, other: &Self) -> bool { | |
self.x == other.x && self.y == other.y | |
} | |
} | |
// Point2D is the same but we want the distinction | |
#[derive(Debug)] | |
struct WidthHeight { | |
width: u16, | |
height: u16, | |
} | |
// ------- | |
// ENUMS | |
// ------- | |
#[derive(Clone, Copy)] | |
pub enum Direction { | |
Up, | |
Down, | |
Left, | |
Right, | |
None, | |
} | |
#[derive(Clone, Copy)] | |
pub enum GameEvent { | |
KeyPress(Direction), | |
Attack(Position), | |
} | |
#[derive(Debug)] | |
pub enum DrawingMethod { | |
Point, | |
Rectangle, | |
} | |
#[derive(Debug)] | |
enum EntityType { | |
Zombie(Zombie), | |
Rectangle(Rectangle), | |
} | |
// -------- | |
// ENTITY | |
// -------- | |
#[derive(Debug)] | |
pub struct Entity { | |
entity_type: EntityType, | |
position: Position, | |
width_height: WidthHeight, | |
} | |
impl Entity { | |
fn new(entity_type: EntityType, position: Position, width_height: WidthHeight) -> Self { | |
Entity { | |
entity_type, | |
position, | |
width_height, | |
} | |
} | |
fn update(&mut self, event: GameEvent) { | |
match &mut self.entity_type { | |
EntityType::Zombie(zombie) => zombie.update(event), | |
EntityType::Rectangle(rectangle) => rectangle.update(event), | |
} | |
} | |
fn is_collision(&self, entity: &Entity) -> bool { | |
self.position.x < entity.position.x + entity.width_height.width | |
&& self.position.x + self.width_height.width > entity.position.x | |
&& self.position.y < entity.position.y + entity.width_height.height | |
&& self.width_height.height + self.position.y > entity.position.y | |
} | |
fn get_drawing_method(&self) -> &DrawingMethod { | |
match &self.entity_type { | |
EntityType::Zombie(zombie) => &zombie.drawing_method, | |
EntityType::Rectangle(rectangle) => &rectangle.drawing_method, | |
} | |
} | |
fn get_image(&self) -> &StyledContent<String> { | |
match &self.entity_type { | |
EntityType::Zombie(zombie) => &zombie.display_char, | |
EntityType::Rectangle(rectangle) => &rectangle.styled_content, | |
} | |
} | |
fn attack(&mut self, target: Position) { | |
match &self.entity_type { | |
EntityType::Zombie(zombie) => { | |
if self.position.x > target.x { | |
self.move_direction(Direction::Up) | |
} | |
if self.position.x < target.x { | |
self.move_direction(Direction::Down) | |
} | |
if self.position.y > target.y { | |
self.move_direction(Direction::Left) | |
} | |
if self.position.y < target.y { | |
self.move_direction(Direction::Right) | |
} | |
} | |
EntityType::Rectangle(_) => todo!(), // EntityType::Rectangle(rectangle) => &rectangle.styled_content, | |
} | |
} | |
fn move_direction(&mut self, movement: Direction) { | |
match movement { | |
Direction::Up => self.position.x -= 1, | |
Direction::Down => self.position.x += 1, | |
Direction::Right => self.position.y += 1, | |
Direction::Left => self.position.y -= 1, | |
Direction::None => {} | |
} | |
} | |
// fn is_left_or_right(&self, row: u16, column: u16) -> bool { | |
// (row == 0 || self.width_height.width - 1 == row) | |
// || (column == 0 || self.width_height.height - 1 == column) | |
// } | |
} | |
// ----------- | |
// RECTANGLE | |
// ----------- | |
#[derive(Debug)] | |
pub struct Rectangle { | |
pub drawing_method: DrawingMethod, | |
pub styled_content: StyledContent<String>, | |
} | |
impl Rectangle { | |
fn update(&mut self, _event: GameEvent) { | |
println!("I am a rectangle updating!"); | |
} | |
} | |
// -------- | |
// ZOMBIE | |
// -------- | |
#[derive(Debug)] | |
pub struct Zombie { | |
pub drawing_method: DrawingMethod, | |
pub display_char: StyledContent<String>, | |
} | |
impl Zombie { | |
fn update(&mut self, _event: GameEvent) { | |
println!("I am a zombie updating!"); | |
} | |
} | |
fn main() { | |
let mut zombie = Entity::new( | |
EntityType::Zombie(Zombie { | |
drawing_method: DrawingMethod::Point, | |
display_char: String::from("z").green(), | |
}), | |
Position { x: 10, y: 10 }, | |
WidthHeight { | |
width: 1, | |
height: 1, | |
}, | |
); | |
let rectangle = Entity::new( | |
EntityType::Rectangle(Rectangle { | |
drawing_method: DrawingMethod::Rectangle, | |
styled_content: String::from("█").white(), | |
}), | |
Position { x: 5, y: 5 }, | |
WidthHeight { | |
width: 2, | |
height: 3, | |
}, | |
); | |
zombie.update(GameEvent::KeyPress(Direction::Left)); | |
zombie.attack(Position { x: 100, y: 100 }); | |
println!("{:?}", zombie.get_image()); | |
println!("{:?}", rectangle.get_image()); | |
println!("{:?}", rectangle.position); | |
println!("{:?}", rectangle.width_height); | |
} | |
// use super::*; | |
#[test] | |
fn test_entity_update() { | |
let mut zombie = Entity::new( | |
EntityType::Zombie(Zombie { | |
drawing_method: DrawingMethod::Point, | |
display_char: String::from("z").green(), | |
}), | |
Position { x: 10, y: 10 }, | |
WidthHeight { | |
width: 1, | |
height: 1, | |
}, | |
); | |
zombie.update(GameEvent::KeyPress(Direction::Left)); | |
assert_eq!(zombie.position, Position { x: 9, y: 10 }); | |
} | |
#[test] | |
fn test_entity_attack() { | |
let mut zombie = Entity::new( | |
EntityType::Zombie(Zombie { | |
drawing_method: DrawingMethod::Point, | |
display_char: String::from("z").green(), | |
}), | |
Position { x: 10, y: 10 }, | |
WidthHeight { | |
width: 1, | |
height: 1, | |
}, | |
); | |
zombie.attack(Position { x: 1, y: 10 }); | |
assert_eq!(zombie.position, Position { x: 9, y: 10 }); | |
} | |
#[test] | |
fn test_entity_collision() { | |
let entity1 = Entity::new( | |
EntityType::Zombie(Zombie { | |
drawing_method: DrawingMethod::Point, | |
display_char: String::from("z").green(), | |
}), | |
Position { x: 10, y: 10 }, | |
WidthHeight { | |
width: 1, | |
height: 1, | |
}, | |
); | |
let entity2 = Entity::new( | |
EntityType::Rectangle(Rectangle { | |
drawing_method: DrawingMethod::Rectangle, | |
styled_content: String::from("█").white(), | |
}), | |
Position { x: 9, y: 10 }, | |
WidthHeight { | |
width: 2, | |
height: 2, | |
}, | |
); | |
let result = entity1.is_collision(&entity2); | |
assert_eq!(result, true); | |
} | |
// #[test] | |
// fn test_entity_drawing_method() { | |
// let zombie = Entity::new( | |
// EntityType::Zombie(Zombie { | |
// drawing_method: DrawingMethod::Point, | |
// display_char: String::from("z").green(), | |
// }), | |
// Position { x: 10, y: 10 }, | |
// WidthHeight { | |
// width: 1, | |
// height: 1, | |
// }, | |
// ); | |
// let rectangle = Entity::new( | |
// EntityType::Rectangle(Rectangle { | |
// drawing_method: DrawingMethod::Rectangle, | |
// styled_content: String::from("█").white(), | |
// }), | |
// Position { x: 9, y: 10 }, | |
// WidthHeight { | |
// width: 2, | |
// height: 2, | |
// }, | |
// ); | |
// let result1 = zombie.get_drawing_method(); | |
// let result2 = rectangle.get_drawing_method(); | |
// assert_eq!(result1, &DrawingMethod::Point); | |
// assert_eq!(result2, &DrawingMethod::Rectangle); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment