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