Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a85cf1cab |
+23
-26
@@ -24,7 +24,6 @@ async fn main() -> color_eyre::Result<()> {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
enum Gamestate {
|
enum Gamestate {
|
||||||
#[default]
|
#[default]
|
||||||
@@ -34,27 +33,15 @@ enum Gamestate {
|
|||||||
Lost,
|
Lost,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Player {
|
pub struct Paddle {
|
||||||
x: f64,
|
x: f64,
|
||||||
y: f64,
|
y: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Player {
|
impl Paddle {
|
||||||
fn default() -> Self {
|
pub fn new(x: f64, y: f64) -> Self {
|
||||||
Player { x: 0.0, y: 5.0 }
|
Paddle { x: x, y: y }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Enemy {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Enemy {
|
|
||||||
fn default() -> Self {
|
|
||||||
Enemy { x: 49.0, y: 5.0 }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,19 +81,27 @@ impl Ball {
|
|||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct App {
|
pub struct App {
|
||||||
running: bool,
|
running: bool,
|
||||||
paused: bool,
|
|
||||||
event_stream: EventStream,
|
event_stream: EventStream,
|
||||||
player: Player,
|
player: Paddle,
|
||||||
player_score: i32,
|
player_score: i32,
|
||||||
enemy_score: i32,
|
enemy_score: i32,
|
||||||
enemy: Enemy,
|
enemy: Paddle,
|
||||||
ball: Ball,
|
ball: Ball,
|
||||||
state: Gamestate,
|
state: Gamestate,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
App {
|
||||||
|
running: true,
|
||||||
|
event_stream: EventStream::new(),
|
||||||
|
player: Paddle::new(0.0, 5.0),
|
||||||
|
enemy: Paddle::new(49.0, 5.0),
|
||||||
|
ball: Ball::default(),
|
||||||
|
state: Gamestate::Playing,
|
||||||
|
player_score: 0,
|
||||||
|
enemy_score: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn restart(&mut self) {
|
pub fn restart(&mut self) {
|
||||||
@@ -114,18 +109,21 @@ impl App {
|
|||||||
self.enemy_score = 0;
|
self.enemy_score = 0;
|
||||||
self.state = Gamestate::Playing;
|
self.state = Gamestate::Playing;
|
||||||
|
|
||||||
self.player = Player::default();
|
self.player = Paddle::new(0.0, 5.0);
|
||||||
self.enemy = Enemy::default();
|
self.enemy = Paddle::new(49.0, 5.0);
|
||||||
self.ball = Ball::default();
|
self.ball = Ball::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
if self.paused != true {
|
match self.state {
|
||||||
|
Gamestate::Playing => {
|
||||||
self.ball.update();
|
self.ball.update();
|
||||||
self.update_enemy();
|
self.update_enemy();
|
||||||
self.check_collisions();
|
self.check_collisions();
|
||||||
self.check_state();
|
self.check_state();
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_state(&mut self) {
|
pub fn check_state(&mut self) {
|
||||||
@@ -323,4 +321,3 @@ impl App {
|
|||||||
self.running = false;
|
self.running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in new issue
Block a user