Skip to content

Commit

Permalink
Merge pull request #47 from bevy-pirates-15/ryn-changes
Browse files Browse the repository at this point in the history
Ryn changes
  • Loading branch information
Rynvosis authored Jul 31, 2024
2 parents ee02a1a + a6de84c commit 78cce2d
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 25 deletions.
Binary file modified assets/images/forest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/images/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions assets/light_frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#version 450

layout(location = 0) in vec2 frag_uv;
layout(location = 1) in vec3 world_position;

layout(location = 0) out vec4 out_color;

layout(set = 2, binding = 0) uniform Material {
vec4 material_color;
};

layout(set = 2, binding = 1) uniform LightCount {
uint light_count;
};

layout(set = 2, binding = 2) buffer Lights {
vec3 lights[];
};

void main() {
bool discard_fragment = false;

for (uint i = 0u; i < light_count; i++) {
vec2 light_pos = lights[i].xy;
float light_radius = lights[i].z;
float lr_squared = light_radius * light_radius;
vec2 diff = world_position.xy - light_pos;
float distancesquared = dot(diff, diff);
if (distancesquared < lr_squared) {
discard_fragment = true;
break;
}
}

if (discard_fragment) {
discard;
}

out_color = material_color;
}
39 changes: 39 additions & 0 deletions assets/light_shader.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#import bevy_sprite::{
mesh2d_vertex_output::VertexOutput,
}

@group(2) @binding(0) var<uniform> material_color: vec4<f32>;
@group(2) @binding(1) var<uniform> light_count: u32;
@group(2) @binding(2) var<uniform> lights: array<vec3<f32>, 64>;


@fragment
fn fragment(
in: VertexOutput
) -> @location(0) vec4<f32> {

var world_pos = in.world_position;
var discard_fragment = false;

for (var i = 0u; i < light_count; i = i + 1u) {
let light_pos = lights[i].xy;
let light_radius = lights[i].z;
let lr_squared = light_radius * light_radius;
let diff = world_pos.xy - light_pos;
let distancesquared = dot(diff, diff);
if (distancesquared < lr_squared) {
discard_fragment = true;
break;
}
}

if (discard_fragment) {
discard;
}

return material_color;
}




18 changes: 18 additions & 0 deletions assets/light_vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 450

layout(location = 0) in vec3 in_position;
layout(location = 1) in vec2 in_uv;

layout(location = 0) out vec2 frag_uv;
layout(location = 1) out vec3 world_position;

layout(set = 0, binding = 0) uniform CameraData {
mat4 view_proj;
mat4 model;
};

void main() {
frag_uv = in_uv;
world_position = (model * vec4(in_position, 1.0)).xyz;
gl_Position = view_proj * vec4(in_position, 1.0);
}
5 changes: 4 additions & 1 deletion src/game/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ fn clear_dead_enemies(

// let rng = rand::thread_rng();
for (health, pos, xp, enemy) in enemy_query.iter() {
let mut exp_pos = *pos;
exp_pos.translation.z += 30.;

if health.health <= 0.0 {
commands.entity(enemy).despawn_recursive();
commands.spawn((
Expand All @@ -460,7 +463,7 @@ fn clear_dead_enemies(
ItemDrop,
SpriteBundle {
texture: images[&ImageAsset::Exp].clone_weak(),
transform: *pos,
transform: exp_pos,
..default()
},
ExpireTimer {
Expand Down
53 changes: 53 additions & 0 deletions src/game/lighting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use bevy::app::Update;
use bevy::asset::{Asset, Assets};
use bevy::color::LinearRgba;
use bevy::math::Vec3;
use bevy::prelude::{Component, GlobalTransform, Query, ResMut, TypePath};
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use bevy::sprite::{Material2d, Material2dPlugin};

pub(super) fn plugin(app: &mut bevy::app::App) {
app.add_plugins(Material2dPlugin::<LightMaterial>::default());
app.add_systems(Update, update_light_material);
}

#[derive(Component)]
pub struct GameLight {
pub radius: f32,
#[allow(dead_code)]
pub priority: u32,
}

#[derive(AsBindGroup, TypePath, Debug, Clone, Asset)]
pub struct LightMaterial {
#[uniform(0)]
pub color: LinearRgba,
#[uniform(1)]
pub light_count: u32,
#[uniform(2)]
pub lights: [Vec3; 64],
}

impl Material2d for LightMaterial {
fn fragment_shader() -> ShaderRef {
"light_shader.wgsl".into()
}
}

fn update_light_material(
mut materials: ResMut<Assets<LightMaterial>>,
light_sources: Query<(&GameLight, &GlobalTransform)>,
) {
let mut lights = [Vec3::ZERO; 64];
let mut light_count = 0;

for (light, transform) in light_sources.iter() {
lights[light_count] = transform.translation().truncate().extend(light.radius);
light_count += 1;
}

for (_, material) in materials.iter_mut() {
material.light_count = light_count as u32;
material.lights.copy_from_slice(&lights);
}
}
2 changes: 2 additions & 0 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod enemy;
pub mod enemy_casting;
pub mod input;
pub mod levelling;
pub mod lighting;
pub mod physics;
pub mod player_mods;
pub mod projectiles;
Expand All @@ -32,6 +33,7 @@ pub(super) fn plugin(app: &mut App) {
projectiles::plugin,
physics::plugin,
player_mods::plugin,
lighting::plugin,
));

app.register_type::<Damageable>();
Expand Down
3 changes: 1 addition & 2 deletions src/game/spawn/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
use bevy::{prelude::*, utils::HashSet};
use bevy_ecs_tilemap::prelude::*;

use super::{borders::SpawnBorders, player::SpawnPlayer, prompt::SpawnPrompt, wand::SpawnWand};
use crate::{
game::assets::{ImageAsset, ImageAssets},
screen::Screen,
};

use super::{borders::SpawnBorders, player::SpawnPlayer, prompt::SpawnPrompt, wand::SpawnWand};

pub(super) fn plugin(app: &mut App) {
app.observe(spawn_level);
app.insert_resource(ChunkManager::default());
Expand Down
2 changes: 2 additions & 0 deletions src/game/spawn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod map;
pub mod player;
pub mod prompt;
pub mod wand;
// pub mod lighting;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((
Expand All @@ -17,5 +18,6 @@ pub(super) fn plugin(app: &mut App) {
wand::plugin,
borders::plugin,
prompt::plugin,
// lighting::plugin,
));
}
30 changes: 27 additions & 3 deletions src/game/spawn/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
use std::time::Duration;

use avian2d::prelude::*;
use bevy::prelude::*;

use crate::game::lighting::{GameLight, LightMaterial};
use crate::game::physics::GameLayer;
use crate::game::player_mods::damage::player_hit_by_projectile;
use crate::game::player_mods::movement::{Movement, PlayerMovement};
Expand All @@ -19,6 +17,8 @@ use crate::{
},
screen::Screen,
};
use avian2d::prelude::*;
use bevy::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.observe(spawn_player);
Expand All @@ -37,6 +37,8 @@ fn spawn_player(
mut commands: Commands,
images: Res<ImageAssets>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
#[allow(dead_code)] _light_materials: ResMut<Assets<LightMaterial>>,
#[allow(dead_code)] _meshes: ResMut<Assets<Mesh>>,
) {
// A texture atlas is a way to split one image with a grid into multiple sprites.
// By attaching it to a [`SpriteBundle`] and providing an index, we can specify which section of the image we want to see.
Expand Down Expand Up @@ -87,5 +89,27 @@ fn spawn_player(
StateScoped(Screen::Playing),
));

p.insert(GameLight {
radius: 100.0,
priority: 1,
});

p.observe(player_hit_by_projectile);

// p.with_children(|parent| {
// parent.spawn((
// Name::new("Darkness"),
// MaterialMesh2dBundle {
// mesh: meshes.add(Rectangle::new(660.,380.)).into(),
// material: light_materials.add(LightMaterial {
// color: LinearRgba::new(0.0,0.0,0.0,0.9),
// light_count: 2,
// lights: [Vec3::ZERO; 64],
// }),
// transform: Transform::from_translation(Vec3::new(0., 0., 20.)),
// ..Default::default()
// },
// StateScoped(Screen::Playing),
// ));
// });
}
12 changes: 10 additions & 2 deletions src/game/spell_system/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Duration;

use crate::game::assets::particles::{ParticleAsset, ParticleAssets};
use crate::game::assets::spell_gfx::{SpellGFXAsset, SpellGFXAssets};
use crate::game::lighting::GameLight;
use crate::game::projectiles::{ProjectileDamage, ProjectileLifetime, ProjectileTeam};
use crate::game::spell_system::casting::SpellCastContext;
use crate::screen::Screen;
Expand Down Expand Up @@ -76,7 +77,7 @@ pub fn spawn_spell_projectile(
Sensor,
SpatialBundle {
transform: Transform::from_translation(
caster_transform.translation + Vec3::new(0.0, 0.0, 0.1),
caster_transform.translation + Vec3::new(0.0, 0.0, 30.),
)
.with_rotation(rotation)
.with_scale(Vec3::splat(1.0)),
Expand All @@ -88,7 +89,7 @@ pub fn spawn_spell_projectile(
ProjectileDamage {
damage: stats.damage,
hits_remaining: stats.num_hits,
team,
team: team.clone(),
knockback_force: stats.knockback_force,
},
ProjectileLifetime {
Expand All @@ -98,6 +99,13 @@ pub fn spawn_spell_projectile(
))
.id();

if team == ProjectileTeam::Player {
world.entity_mut(spell).insert(GameLight {
radius: 20.0,
priority: 10,
});
}

match spell_model {
SpellModel::None => {}
SpellModel::StaticSprite(gfx) => {
Expand Down
8 changes: 4 additions & 4 deletions src/game/spell_system/spells/cores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl SpellEffect for ZapSpell {
damage: self.base_damage,
num_hits: 1,
lifetime: Duration::from_secs_f32(2.0),
knockback_force: 200.0,
knockback_force: 100.0,
},
) else {
warn!("Failed to spawn zap spell entity");
Expand Down Expand Up @@ -159,7 +159,7 @@ impl SpellEffect for BangSpell {
damage: self.base_damage,
num_hits: 1000,
lifetime: Duration::from_secs_f32(0.05),
knockback_force: 100.0,
knockback_force: 50.0,
}, // self.radius,
// 0.0,
// self.base_damage,
Expand Down Expand Up @@ -227,7 +227,7 @@ impl SpellEffect for ArcaneArrowSpell {
damage: self.base_damage,
num_hits: self.num_hits,
lifetime: Duration::from_secs_f32(2.0),
knockback_force: 100.0,
knockback_force: 50.0,
},
) else {
warn!("Failed to spawn arcane arrow spell entity");
Expand Down Expand Up @@ -292,7 +292,7 @@ impl SpellEffect for SplitterBoltsSpell {
damage: self.base_damage,
num_hits: 1,
lifetime: Duration::from_secs_f32(2.0),
knockback_force: 50.0,
knockback_force: 25.0,
},
) else {
warn!("Failed to spawn splitter bolts spell entity");
Expand Down
5 changes: 3 additions & 2 deletions src/game/spell_system/spells/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(super) fn get_spells() -> Vec<(SpellComponent, i32)> {
}),
icon_id: 17,
},
100,
50,
),
(
SpellComponent {
Expand All @@ -58,7 +58,7 @@ pub(super) fn get_spells() -> Vec<(SpellComponent, i32)> {
}),
icon_id: 18,
},
20,
100,
),
]
}
Expand Down Expand Up @@ -221,6 +221,7 @@ impl SpellEffect for Duplicate {
// get ProjectileDamage component
if let Some(mut projectile_damage) = mod_world.get_mut::<ProjectileDamage>(e) {
projectile_damage.damage *= damage_decrease;
projectile_damage.knockback_force *= damage_decrease;
};
});

Expand Down
Loading

0 comments on commit 78cce2d

Please sign in to comment.