-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
262 additions
and
482 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
use avian2d::prelude::*; | ||
use bevy::prelude::*; | ||
|
||
use crate::screens::ingame::playing::{MovementSpeed, Obstacle}; | ||
use crate::{ | ||
assets::Characters, | ||
physics::Gravity, | ||
screens::{Screen, ingame::playing}, | ||
}; | ||
|
||
#[derive(Component, Debug, Default, Eq, PartialEq)] | ||
pub enum CharacterState { | ||
#[default] | ||
Falling, | ||
Walking, | ||
} | ||
|
||
#[derive(Component, Debug)] | ||
#[require(CharacterState)] | ||
pub struct Yup; | ||
|
||
pub fn plugin(app: &mut App) { | ||
app.add_systems(PostProcessCollisions, collision_handler); | ||
// TODO: this .after is a bit cursed, come up with better spawning logic. | ||
app.add_systems(OnEnter(Screen::InGame), init.after(playing::init)); | ||
} | ||
|
||
fn collision_handler( | ||
mut collisions: ResMut<Collisions>, | ||
obstacles: Query<Entity, With<Obstacle>>, | ||
mut yups: Query<(Entity, &mut MovementSpeed), With<Yup>>, | ||
) { | ||
// For now, we'll ignore all collisions between yups. | ||
// TODO: sometimes blockers will need to be collided with! | ||
collisions.retain(|c| yups.get(c.entity1).is_err() || yups.get(c.entity2).is_err()); | ||
|
||
for (yup, mut speed) in &mut yups { | ||
for obstacle in &obstacles { | ||
if collisions.contains(yup, obstacle) { | ||
*speed = MovementSpeed(-speed.0); | ||
} | ||
} | ||
} | ||
fn init(mut commands: Commands, characters: Res<Characters>) { | ||
commands.spawn(( | ||
Name::new("Yup"), | ||
Yup, | ||
Gravity, | ||
Sprite { | ||
image: characters.yup.clone(), | ||
..default() | ||
}, | ||
// TODO: should all yups be spawned on specific Z-value for easy handling? | ||
Transform::from_xyz(500., 200., 1.), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::{ | ||
game::yup::{CharacterState, Yup}, | ||
screens::ingame::playing::Level, | ||
}; | ||
|
||
pub fn plugin(app: &mut App) { | ||
app.add_systems(FixedUpdate, (gravity, collision).chain()); | ||
} | ||
|
||
#[derive(Component, Debug)] | ||
pub struct Gravity; | ||
|
||
fn gravity(mut has_gravity: Query<(&CharacterState, &mut Transform), With<Gravity>>) { | ||
for (state, mut t) in &mut has_gravity { | ||
if *state == CharacterState::Falling { | ||
t.translation.y -= 3.; | ||
} | ||
} | ||
} | ||
|
||
fn collision( | ||
camera: Single<(&Camera, &GlobalTransform), With<Camera2d>>, | ||
images: Res<Assets<Image>>, | ||
level: Query<&MeshMaterial2d<ColorMaterial>, With<Level>>, | ||
materials: Res<Assets<ColorMaterial>>, | ||
mut yups: Query<(&mut CharacterState, &Transform), With<Yup>>, | ||
) { | ||
let Ok(l) = level.get_single() else { | ||
return; | ||
}; | ||
|
||
let Some(level_material) = materials.get(&l.0) else { | ||
return; | ||
}; | ||
|
||
let Some(texture) = &level_material.texture else { | ||
return; | ||
}; | ||
|
||
let Some(img) = images.get(texture) else { | ||
return; | ||
}; | ||
|
||
let (camera, camera_transform) = camera.into_inner(); | ||
for (mut state, t) in &mut yups { | ||
// TODO: better way to determine this? | ||
let feet = t.translation + Vec3::new(0., -18., 0.); | ||
if let Ok(collision_point) = camera.world_to_viewport(camera_transform, feet) { | ||
let Ok(check) = img.get_color_at(collision_point.x as u32, collision_point.y as u32) | ||
else { | ||
return; | ||
}; | ||
if check.alpha() > 0. { | ||
// Hey, we collided with something! | ||
*state = CharacterState::Walking; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.