Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Particles #41

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ pub fn add_actors(
if let Some(atom) = chunk_manager.get_mut_atom(pos) {
if atom.state == State::Void {
*atom = Atom::object();
} else if atom.state == State::Liquid {
let rand_angle = fastrand::f32() - 0.5;
let vel = actor.vel * -4. * vec2(rand_angle.cos(), rand_angle.sin());
//Water splashes
atom.velocity = (
(vel.x).clamp(-126.0, 126.) as i8,
(vel.y).clamp(-126.0, 126.) as i8,
);
atom.automata_mode = false;
}
}
update_dirty_rects_3x3(&mut dirty_rects.current, pos);
Expand Down
50 changes: 23 additions & 27 deletions src/atom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rand::Rng;
use std::{collections::HashSet, f32::consts::PI};
use std::collections::HashSet;
use std::f32::consts::PI;

use crate::prelude::*;

Expand All @@ -10,21 +11,19 @@ pub struct Atom {
pub state: State,

#[serde(skip)]
pub updated_at: u8,
#[serde(skip)]
pub automata_mode: bool,
// Used when thrown up, etc
#[serde(skip)]
pub velocity: (i8, i8),
pub speed: (i8, i8),
// Frames idle
#[serde(skip)]
pub f_idle: u8,
#[serde(skip)]
pub updated_at: u8,
}

impl Atom {
pub fn object() -> Self {
Atom {
state: State::Object,
color: [255, 255, 255, 255],
..Default::default()
}
}
Expand All @@ -50,14 +49,14 @@ pub fn update_powder(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashS

let mut cur_pos = pos;

// Get fall speed
let mut fall_speed = get_fspeed(chunks, cur_pos);
if fall_speed < TERM_VEL {
fall_speed += GRAVITY;
set_fspeed(chunks, cur_pos, fall_speed);
// Get atom speed
let mut speed = get_speed(chunks, cur_pos);
if speed < TERM_VEL {
speed += GRAVITY;
set_speed(chunks, cur_pos, speed);
}

for _ in 0..fall_speed {
for _ in 0..speed {
let neigh = down_neigh(chunks, cur_pos, &[(State::Liquid, 0.2)], dt);
let mut swapped = false;
for neigh in neigh {
Expand All @@ -73,18 +72,16 @@ pub fn update_powder(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashS
}

if !swapped {
let new_vel = Vec2::new(0.0, -(fall_speed as f32));
let vel = Vec2::new(0.0, -(speed as f32));

set_vel(
chunks,
cur_pos,
Vec2::from_angle(rand::thread_rng().gen_range(-PI / 2.0..PI / 2.))
.rotate(new_vel * 0.3)
.rotate(vel * 0.3)
.as_ivec2(),
);

set_fspeed(chunks, cur_pos, 0);

break;
}
}
Expand All @@ -98,14 +95,14 @@ pub fn update_liquid(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashS
let mut cur_pos = pos;

// Get fall speed
let mut fall_speed = get_fspeed(chunks, pos);
if fall_speed < TERM_VEL {
fall_speed += GRAVITY;
set_fspeed(chunks, pos, fall_speed);
let mut speed = get_speed(chunks, pos);
if speed < TERM_VEL {
speed += GRAVITY;
set_speed(chunks, pos, speed);
}

let mut swapped = false;
for _ in 0..fall_speed {
for _ in 0..speed {
let neigh = down_neigh(chunks, cur_pos, &[], dt);
for neigh in neigh {
if neigh.0 {
Expand All @@ -121,7 +118,7 @@ pub fn update_liquid(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashS
}

if !swapped {
set_fspeed(chunks, cur_pos, 0);
set_speed(chunks, cur_pos, 0);
let neigh = side_neigh(chunks, cur_pos, &[], dt);
let side = if neigh[0].0 {
Some(neigh[0].1.x)
Expand Down Expand Up @@ -149,13 +146,13 @@ pub fn update_liquid(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashS
awakened
}

/// Updates particle and returns atoms awakened
pub fn update_particle(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashSet<IVec2> {
/// This updates the atom with a vector based velocity, not a automata like one
pub fn update_atom(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> HashSet<IVec2> {
let mut awakened = HashSet::new();
let mut cur_pos = pos;

// Add gravity
let mut vel = get_vel(chunks, cur_pos).unwrap_or(IVec2::ZERO);
let mut vel = get_vel(chunks, cur_pos);
if vel.y < TERM_VEL as i32 {
vel += GRAVITY as i32 * IVec2::Y;
set_vel(chunks, cur_pos, vel);
Expand Down Expand Up @@ -185,7 +182,6 @@ pub fn update_particle(chunks: &mut UpdateChunksType, pos: IVec2, dt: u8) -> Has
);
} else if !swapable(chunks, cur_pos + IVec2::Y, &[], state, dt) {
set_vel(chunks, cur_pos, IVec2::ZERO);
set_mode(chunks, cur_pos, true);
}
break;
}
Expand Down
50 changes: 23 additions & 27 deletions src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use smallvec::SmallVec;
use crate::prelude::*;

/// Updates and do the chunks logic
#[derive(Default, Resource)]
#[derive(Default, Resource, Clone)]
pub struct ChunkManager {
pub chunks: HashMap<IVec2, Chunk>,
pub pos: IVec2,
Expand Down Expand Up @@ -199,7 +199,11 @@ pub fn manager_setup(
.spawn((
Name::new("Chunks textures"),
VisibilityBundle::default(),
TransformBundle::default(),
TransformBundle::from_transform(Transform::from_translation(vec3(
0.,
0.,
AUTOMATA_LAYER,
))),
ChunkTextures,
))
.push_children(&images_vec);
Expand Down Expand Up @@ -397,43 +401,39 @@ pub fn update_chunks(chunks: &mut UpdateChunksType, dt: u8, dirty_rect: &URect)

let mut awake_self = false;
let state;
let automata_mode;
let speed;
{
let atom = &mut chunks.group[local_pos];
state = atom.state;

if atom.velocity == (0, 0) {
atom.automata_mode = true;
}
automata_mode = atom.automata_mode;
if automata_mode {
atom.velocity = (0, atom.velocity.1.abs());
}
speed = atom.speed;

if atom.f_idle < FRAMES_SLEEP && state != State::Void && state != State::Solid {
atom.f_idle += 1;
awake_self = true;
}
}

let mut awakened = if automata_mode {
match state {
State::Powder => update_powder(chunks, pos, dt),
State::Liquid => update_liquid(chunks, pos, dt),
_ => HashSet::new(),
}
let (vector, mut awakened) = if speed.0 == 0 && speed.1 >= 0 {
(
false,
match state {
State::Powder => update_powder(chunks, pos, dt),
State::Liquid => update_liquid(chunks, pos, dt),
_ => HashSet::new(),
},
)
} else {
update_particle(chunks, pos, dt)
(true, update_atom(chunks, pos, dt))
};

let mut self_awakened = HashSet::new();
if awakened.contains(&pos) {
let atom = &mut chunks.group[local_pos];
atom.f_idle = 0;
} else if !automata_mode {
awakened.insert(pos);
} else if vector {
let atom = &mut chunks.group[local_pos];
atom.f_idle = 0;
awakened.insert(pos);
} else if awake_self {
awakened.insert(pos);
self_awakened.insert(pos);
Expand Down Expand Up @@ -463,8 +463,8 @@ pub fn add_chunk(
index: IVec2,
) -> Entity {
let pos = Vec2::new(
index.x as f32 * SIDE_LENGHT,
(-index.y as f32) * SIDE_LENGHT,
index.x as f32 * CHUNK_LENGHT as f32,
(-index.y as f32) * CHUNK_LENGHT as f32,
);

//Add texture
Expand All @@ -484,11 +484,7 @@ pub fn add_chunk(
anchor: Anchor::TopLeft,
..Default::default()
},
transform: Transform::from_xyz(pos.x, pos.y, 0.).with_scale(vec3(
ATOM_SIZE as f32,
ATOM_SIZE as f32,
1.,
)),
transform: Transform::from_xyz(pos.x, pos.y, 0.),
..Default::default()
})
.id()
Expand Down
15 changes: 11 additions & 4 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ 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;

pub const SIDE_LENGHT: f32 = (CHUNK_LENGHT * ATOM_SIZE) as f32;

// Actor consts

pub const UP_WALK_HEIGHT: usize = 3;
Expand All @@ -22,13 +20,17 @@ 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 RUN_SPEED: f32 = 3.5;

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

// Engine consts
pub const ATOM_SIZE: usize = 3;

//This was a "scale" const for the atoms, but we can just zoom in, so it was removed
//Made the code less verbose and simpler, we can readd if it makes sense
//pub const ATOM_SIZE: usize = 3;

pub const GRAVITY: u8 = 1;
pub const TERM_VEL: u8 = 10;
pub const FRAMES_SLEEP: u8 = 4;
Expand All @@ -37,3 +39,8 @@ pub const LOAD_WIDTH: i32 = 20;
pub const LOAD_HEIGHT: i32 = 12;

pub const _CAMERA_SPEED: f32 = 10.;

//Layers
pub const PLAYER_LAYER: f32 = 1.;
pub const PARTICLE_LAYER: f32 = 10.;
pub const AUTOMATA_LAYER: f32 = 100.;
Loading