Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
markrzasa committed Nov 12, 2023
1 parent beba91c commit 54e28ea
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 177 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 9 additions & 16 deletions src/black_hole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::time::{Duration, SystemTime};
use sdl2::rect::Rect;
use crate::drawable::Drawable;
use crate::game_context::GameContext;
use crate::game_sprite::GameSprite;
use crate::updateable::Updateable;

const ROTATION_UPDATE_MILLIS: Duration = Duration::from_millis(250);
Expand All @@ -19,34 +20,26 @@ pub enum BlackHoleState {
}

pub struct BlackHole {
degrees: f64,
height: u32,
last_update: SystemTime,
sprite: GameSprite,
state: BlackHoleState,
width: u32,
x: f64,
y: f64
}

impl BlackHole {
pub fn new(r: &Rect) -> Self {
Self {
degrees: 0.0,
height: r.height(),
last_update: SystemTime::now(),
state: BlackHoleState::Open,
width: r.width(),
x: r.x() as f64,
y: r.y() as f64
sprite: GameSprite::from_rect(r),
state: BlackHoleState::Open
}
}

pub fn covered(&mut self) {
self.state = BlackHoleState::Covered;
}

pub fn get_rect(&self) -> Rect {
Rect::new(self.x as i32, self.y as i32, self.width, self.height)
pub fn get_sprite(&self) -> GameSprite {
self.sprite
}

pub fn get_state(&self) -> BlackHoleState {
Expand Down Expand Up @@ -83,8 +76,8 @@ impl BlackHoles {
impl Drawable for BlackHoles {
fn draw(&mut self, ctx: Context, gl: &mut GlGraphics) {
for black_hole in self.black_holes.iter() {
self.sprite.set_position(black_hole.x, black_hole.y);
self.sprite.set_rotation(black_hole.degrees);
self.sprite.set_position(black_hole.sprite.x, black_hole.sprite.y);
self.sprite.set_rotation(black_hole.sprite.degrees);
self.sprite.draw(ctx.transform, gl);
}
}
Expand All @@ -94,7 +87,7 @@ impl Updateable for BlackHoles {
fn update<'a>(&'a mut self, context: &'a GameContext) -> &GameContext {
for black_hole in self.black_holes.iter_mut() {
if black_hole.last_update.elapsed().unwrap() > ROTATION_UPDATE_MILLIS {
black_hole.degrees = (black_hole.degrees + 10.0).rem_euclid(360.0);
black_hole.sprite.degrees = (black_hole.sprite.degrees + 10.0).rem_euclid(360.0);
black_hole.last_update = SystemTime::now();
}
}
Expand Down
68 changes: 20 additions & 48 deletions src/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sprite::Sprite;
use uuid::Uuid;
use crate::drawable::Drawable;
use crate::game_context::GameContext;
use crate::game_sprite::GameSprite;
use crate::updateable::Updateable;

const ENEMY_MOVE_INCREMENT: f64 = 0.25;
Expand All @@ -27,11 +28,7 @@ pub enum EnemyState {
}

pub struct Enemy {
id: Uuid,
x: f64,
y: f64,
height: u32,
width: u32,
sprite: GameSprite,
sprite_index: u32,
state: EnemyState,
frames: u32,
Expand All @@ -41,11 +38,7 @@ pub struct Enemy {
impl Enemy {
pub fn new(x: f64, y: f64, width: u32, height: u32, frames: u32) -> Self {
Self {
id: Uuid::new_v4(),
x,
y,
height,
width,
sprite: GameSprite::new(x, y, width as f64, height as f64),
sprite_index: 0,
state: EnemyState::Alive,
frames,
Expand All @@ -57,12 +50,8 @@ impl Enemy {
self.state = EnemyState::Dying;
}

pub fn get_id(&self) -> Uuid {
return self.id;
}

pub fn get_rect(&self) -> Rect {
Rect::new(self.x as i32, self.y as i32, self.width, self.height)
pub fn get_sprite(&self) -> GameSprite {
self.sprite
}

pub fn get_sprite_index(&self) -> u32 {
Expand All @@ -80,24 +69,24 @@ impl Enemy {
self.sprite_index = (self.sprite_index + 1).rem_euclid(self.frames);
self.last_frame_change = SystemTime::now();
}
if (player.x as f64) < self.x {
self.x = self.x - ENEMY_MOVE_INCREMENT;
if (player.x as f64) < self.sprite.x {
self.sprite.x = self.sprite.x - ENEMY_MOVE_INCREMENT;
} else {
self.x = self.x + ENEMY_MOVE_INCREMENT;
self.sprite.x = self.sprite.x + ENEMY_MOVE_INCREMENT;
}

if (player.y as f64) < self.y {
self.y = self.y - ENEMY_MOVE_INCREMENT;
if (player.y as f64) < self.sprite.y {
self.sprite.y = self.sprite.y - ENEMY_MOVE_INCREMENT;
} else {
self.y = self.y + ENEMY_MOVE_INCREMENT;
self.sprite.y = self.sprite.y + ENEMY_MOVE_INCREMENT;
}
}
EnemyState::Dying => {
self.x = self.x - ENEMY_DIE_INCREMENT as f64;
self.y = self.y - ENEMY_DIE_INCREMENT as f64;
self.width = self.width + (ENEMY_DIE_INCREMENT * 2);
self.height = self.height + (ENEMY_DIE_INCREMENT * 2);
if (self.x <= 0.0) && (self.y <= 0.0) && ((self.x + self.width as f64) >= screen_width) && ((self.y + self.height as f64) >= screen_height) {
self.sprite.x = self.sprite.x - ENEMY_DIE_INCREMENT as f64;
self.sprite.y = self.sprite.y - ENEMY_DIE_INCREMENT as f64;
self.sprite.width = self.sprite.width + (ENEMY_DIE_INCREMENT * 2) as f64;
self.sprite.height = self.sprite.height + (ENEMY_DIE_INCREMENT * 2) as f64;
if (self.sprite.x <= 0.0) && (self.sprite.y <= 0.0) && ((self.sprite.x + self.sprite.width) >= screen_width) && ((self.sprite.y + self.sprite.height) >= screen_height) {
self.state = EnemyState::Dead;
}
}
Expand Down Expand Up @@ -152,7 +141,7 @@ impl Enemies {

impl Drawable for Enemies {
fn draw(&mut self, ctx: Context, gl: &mut GlGraphics) {
for enemy in self.enemies.values() {
for enemy in self.enemies.values_mut() {
match enemy.get_state() {
EnemyState::Alive => {
self.sprite.set_src_rect([
Expand All @@ -161,27 +150,10 @@ impl Drawable for Enemies {
self.sprite_width as f64,
self.sprite_height as f64
]);
self.sprite.set_position(enemy.x, enemy.y);
self.sprite.draw(ctx.transform, gl);
enemy.sprite.draw(&mut self.sprite, ctx, gl);
}
EnemyState::Dying => {
let width = self.sprite_width as f64 / 2.0;
let height = self.sprite_height as f64 / 2.0;
self.sprite.set_src_rect([0.0, 0.0, width, height]);
self.sprite.set_position(enemy.x, enemy.y);
self.sprite.draw(ctx.transform, gl);

self.sprite.set_src_rect([width, 0.0, width, height]);
self.sprite.set_position(enemy.x + enemy.width as f64 - width, enemy.y);
self.sprite.draw(ctx.transform, gl);

self.sprite.set_src_rect([0.0, height, width, height]);
self.sprite.set_position(enemy.x + enemy.width as f64 - width, enemy.y + enemy.height as f64 - height);
self.sprite.draw(ctx.transform, gl);

self.sprite.set_src_rect([width, height, width, height]);
self.sprite.set_position(enemy.x, enemy.y + enemy.height as f64 - height);
self.sprite.draw(ctx.transform, gl);
enemy.sprite.shatter(&mut self.sprite, self.sprite_width, self.sprite_height, ctx, gl);
}
_ => {}
}
Expand All @@ -205,7 +177,7 @@ impl Updateable for Enemies {
p.x as f64, p.y as f64,
self.sprite_width, self.sprite_height, self.sprite_frames
);
self.enemies.insert(enemy.get_id(), enemy);
self.enemies.insert(enemy.get_sprite().get_id(), enemy);
self.last_enemy = SystemTime::now();
}
}
Expand Down
76 changes: 76 additions & 0 deletions src/game_sprite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use graphics::Context;
use opengl_graphics::{GlGraphics, Texture};
use sdl2::rect::Rect;
use sprite::Sprite;
use uuid::Uuid;

#[derive(Clone, Copy, PartialEq)]
pub struct GameSprite {
pub degrees: f64,
id: Uuid,
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64
}

impl GameSprite {
pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
Self {
degrees: 0.0,
id: Uuid::new_v4(),
x, y, width, height
}
}

pub fn from_rect(r: &Rect) -> Self {
Self {
degrees: 0.0,
id: Uuid::new_v4(),
x: r.x as f64,
y: r.y as f64,
width: r.w as f64,
height: r.h as f64
}
}

pub fn get_id(&self) -> Uuid {
self.id
}

pub fn get_position(&self) -> Rect {
Rect::new(self.x as i32, self.y as i32, self.width as u32, self.height as u32)
}

pub fn set_position(&mut self, x: f64, y: f64) -> &mut Self {
self.x = x;
self.y = y;
self
}

pub fn draw(&mut self, sprite: &mut Sprite<Texture>, ctx: Context, gl: &mut GlGraphics) {
sprite.set_position(self.x, self.y);
sprite.set_rotation(self.degrees);
sprite.draw(ctx.transform, gl);
}

pub fn shatter(&mut self, sprite: &mut Sprite<Texture>, sprite_width: u32, sprite_height: u32, ctx: Context, gl: &mut GlGraphics) {
let width = sprite_width as f64 / 2.0;
let height = sprite_height as f64 / 2.0;
sprite.set_src_rect([0.0, 0.0, width, height]);
sprite.set_position(self.x, self.y);
sprite.draw(ctx.transform, gl);

sprite.set_src_rect([width, 0.0, width, height]);
sprite.set_position(self.x + self.width - width, self.y);
sprite.draw(ctx.transform, gl);

sprite.set_src_rect([0.0, height, width, height]);
sprite.set_position(self.x + self.width - width, self.y + self.height - height);
sprite.draw(ctx.transform, gl);

sprite.set_src_rect([width, height, width, height]);
sprite.set_position(self.x, self.y + self.height - height);
sprite.draw(ctx.transform, gl);
}
}
Loading

0 comments on commit 54e28ea

Please sign in to comment.