Skip to content

Commit

Permalink
Merge pull request #13 from Zac8668/clean-code
Browse files Browse the repository at this point in the history
fix: Clean code
  • Loading branch information
Zac8668 authored Dec 30, 2023
2 parents 19e2b23 + 90c4f56 commit bd730cc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/actors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::atom::State;
use crate::prelude::*;

#[derive(Component, Clone, Copy)]
Expand Down
3 changes: 2 additions & 1 deletion src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Chunk {
Ordering::Less => {}
_ => {
for atom in &mut atoms {
atom.state = crate::prelude::State::Powder;
atom.state = State::Powder;
atom.color = [
(230 + rand::thread_rng().gen_range(-20_i16..20_i16)) as u8,
(197 + rand::thread_rng().gen_range(-20_i16..20_i16)) as u8,
Expand Down Expand Up @@ -50,6 +50,7 @@ impl Chunk {
)
}

//This uses the CPU and is not used in-game anymore
pub fn update_image_positions(&self, image: &mut Image, positions: &HashSet<IVec2>) {
for pos in positions {
let pixel_index = (pos.y as usize * CHUNK_LENGHT + pos.x as usize) * 4;
Expand Down
2 changes: 1 addition & 1 deletion src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct ChunkGroup<'a> {
pub center: &'a mut [Atom; CHUNK_LEN],
pub corners: ChunkCorners<'a>,
pub sides: ChunkSides<'a>,
// The pos of the center chunk on the chunk manager vec
/// Position of the center chunk.
pub center_pos: IVec2,
}

Expand Down
21 changes: 20 additions & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Chunk Lenght consts
// Chunk lenght MUST be divisible by 4
pub const CHUNK_LENGHT: usize = 64;
pub const HALF_CHUNK_LENGHT: usize = CHUNK_LENGHT / 2;
Expand All @@ -6,12 +7,30 @@ pub const CHUNK_LEN: usize = CHUNK_LENGHT * CHUNK_LENGHT;
pub const HALF_CHUNK_LEN: usize = CHUNK_LEN / 2;
pub const QUARTER_CHUNK_LEN: usize = CHUNK_LEN / 4;

// Actor consts

pub const UP_WALK_HEIGHT: usize = 3;
pub const DOWN_WALK_HEIGHT: usize = 6;

// Player consts
pub const FUEL_MAX: f32 = 50.;
pub const FUEL_REGEN: f32 = 1.;
pub const FUEL_COMSUMPTON: f32 = 0.48;
pub const JETPACK_FORCE: f32 = 1.5;
pub const JETPACK_MAX: f32 = 3.;

pub const JUMP_MAG: f32 = 13.;
pub const RUN_SPEED: f32 = 5.;

pub const TOOL_DISTANCE: f32 = 32.;
pub const TOOL_RANGE: f32 = 12.;

// Engine consts
pub const ATOM_SIZE: usize = 3;
pub const _CAMERA_SPEED: f32 = 10.;
pub const GRAVITY: u8 = 1;
pub const TERM_VEL: u8 = 10;
pub const FRAMES_SLEEP: u8 = 4;
pub const CHUNKS_WIDTH: usize = 8;
pub const CHUNKS_HEIGHT: usize = 6;

pub const _CAMERA_SPEED: f32 = 10.;
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ mod geom_tools;
mod manager_api;
mod player;
mod prelude {
pub use crate::atom::State;
pub use crate::{
actors::*, animation::*, atom::State, atom::*, chunk::*, chunk_group::*, chunk_manager::*,
consts::*, debug::*, geom_tools::*, manager_api::*, player::*,
actors::*, animation::*, atom::*, chunk::*, chunk_group::*, chunk_manager::*, consts::*,
debug::*, geom_tools::*, manager_api::*, player::*,
};
pub use bevy::math::{ivec2, ivec3, uvec2, uvec3, vec2, vec3};
pub use bevy::prelude::*;
Expand Down
24 changes: 5 additions & 19 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,14 @@ pub struct Tool {
atoms: Vec<Atom>,
}

// Player consts
const FUEL_MAX: f32 = 50.;
const FUEL_REGEN: f32 = 1.;
const FUEL_COMSUMPTON: f32 = 0.48;
const JUMP_MAG: f32 = 13.;
const JETPACK_FORCE: f32 = 1.5;
const JETPACK_MAX: f32 = 3.;
const RUN_SPEED: f32 = 5.;

pub fn player_setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut chunk_manager: Query<&mut ChunkManager>,
) {
let mut chunk_manager = chunk_manager.single_mut();

let player_actor = Actor {
height: 17,
width: 10,
Expand Down Expand Up @@ -101,12 +93,8 @@ pub fn update_player(
let (mut actor, mut player, mut textatlas_sprite, mut anim_idxs) = player.single_mut();
let (mut tool_transform, tool_gtransform, mut tool_sprite, mut tool) = tool.single_mut();
let (mouse, keys) = input;

let mut chunk_manager = chunk_manager.single_mut();

let on_ground = on_ground(&chunk_manager, &actor);
let mut just_jumped = false;

// Gravity
if actor.vel.y < TERM_VEL as f32 {
actor.vel.y += 1.;
Expand All @@ -117,11 +105,13 @@ pub fn update_player(
actor.vel.x = x * RUN_SPEED;

// Refuel
let on_ground = on_ground(&chunk_manager, &actor);
if on_ground {
player.fuel = (player.fuel + FUEL_REGEN).clamp(0., Player::default().fuel);
}

// Jump and Jetpack
let mut just_jumped = false;
if keys.just_pressed(KeyCode::Space) {
if on_ground {
actor.vel.y -= JUMP_MAG;
Expand Down Expand Up @@ -179,20 +169,16 @@ pub fn update_player(
tool_transform.translation.x =
tool_transform.translation.x.abs() * (flip_bool as i8 * 2 - 1) as f32;

const TOOL_DISTANCE: f32 = 32.;
const TOOL_RANGE: f32 = 12.;

//Tool shooting and sucking atoms
let mut center_vec_y_flipped = center_vec;
center_vec_y_flipped.y *= -1.;
center_vec_y_flipped /= ATOM_SIZE as f32;

let tool_slope = Vec2::new(angle.cos(), -angle.sin());
let tool_front = center_vec_y_flipped + tool_slope * 8.;

let bound_slope = Vec2::new((angle + std::f32::consts::FRAC_PI_2).cos(), -(angle).cos());
let tool_front = center_vec_y_flipped + tool_slope * 8.;

let mut pos_to_update = vec![];

if mouse.pressed(MouseButton::Right) {
let new_tool_front = tool_front + tool_slope * 6.;
for i in 0..3 {
Expand Down

0 comments on commit bd730cc

Please sign in to comment.