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

Load attack properties from assets #221

Merged
merged 4 commits into from
Aug 11, 2022
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
12 changes: 11 additions & 1 deletion assets/beach/beach.level.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,20 @@ enemies:
location: [450, 20, 0]
trip_point_x: 300
- fighter: /fighters/big_bass/big_bass.fighter.yaml
location: [1000, 20, 0]
location: [600, 20, 0]
trip_point_x: 700
boss: true

- fighter: &brute /fighters/brute/brute.fighter.yaml
location: [400, -30, 0]
trip_point_x: -1
- fighter: *brute
location: [450, 20, 0]
trip_point_x: 300
- fighter: *brute
location: [1000, 20, 0]
trip_point_x: 700

stop_points: [500, 1000]

items:
Expand Down
9 changes: 9 additions & 0 deletions assets/fighters/bandit/bandit.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ spritesheet:
attacking:
frames: [14, 17]

attack:
name: "punch"
frames:
startup: 1
active: 2
recovery: 3
hitbox: [12, 12]
hitbox_offset: [24, 0]

audio:
effects:
attacking:
Expand Down
9 changes: 9 additions & 0 deletions assets/fighters/big_bass/big_bass.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ spritesheet:
attacking:
frames: [30, 44]

attack:
name: "ground_slam"
frames:
startup: 5
active: 9
recovery: 14
hitbox: [48, 48]
hitbox_offset: [0, 0]

audio:
effects:
attacking:
Expand Down
9 changes: 9 additions & 0 deletions assets/fighters/brute/brute.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,14 @@ spritesheet:
attacking:
frames: [16, 23]

attack:
name: "punch"
frames:
startup: 2
active: 3
recovery: 7
hitbox: [12, 12]
hitbox_offset: [24, 0]

audio:
effects: {}
10 changes: 10 additions & 0 deletions assets/fighters/fishy/fishy.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ spritesheet:
frames: [71, 76]
dying:
frames: [71, 76]
#spritesheet does not contain unique attack animation
attacking:
frames: [85, 90]

attack:
name: "punch"
frames:
startup: 1
active: 2
recovery: 4
hitbox: [16, 16]
hitbox_offset: [24, 0]

audio:
effects:
attacking:
Expand Down
10 changes: 10 additions & 0 deletions assets/fighters/sharky/sharky.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ spritesheet:
frames: [71, 76]
dying:
frames: [71, 76]
#spritesheet does not contain unique attack animation
attacking:
frames: [85, 90]

attack:
name: "punch"
frames:
startup: 1
active: 2
recovery: 4
hitbox: [16, 16]
hitbox_offset: [24, 0]

audio:
effects:
attacking:
Expand Down
9 changes: 9 additions & 0 deletions assets/fighters/slinger/slinger.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ spritesheet:
repeat: false
attacking:
frames: [14, 18]
# this should be changed to a ranged attack
attack:
name: "punch"
frames:
startup: 1
active: 2
recovery: 3
hitbox: [12, 12]
hitbox_offset: [24, 0]

audio:
effects: {}
23 changes: 14 additions & 9 deletions src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use bevy::{hierarchy::DespawnRecursiveExt, math::Vec2, prelude::*, reflect::Refl
use bevy_rapier2d::prelude::*;
use iyes_loopless::prelude::*;

use serde::Deserialize;

use crate::{
animation::Animation,
consts::{ATTACK_HEIGHT, ATTACK_WIDTH},
damage::{DamageEvent, Damageable, Health},
metadata::FighterMeta,
GameState,
};

Expand Down Expand Up @@ -42,28 +44,31 @@ pub struct Attack {
///
/// Must be added to an entity that is a child of an entity with an [`Animation`] and an [`Attack`]
/// and will be used to spawn a collider for that attack during the `active` frames.
#[derive(Component)]
/// Each field is an index refering to an animation frame
#[derive(Component, Debug, Clone, Copy, Deserialize)]
pub struct AttackFrames {
pub startup: usize,
pub active: usize,
pub recovery: usize,
}

/// Activate collisions for entities with [`AttackFrames`]
fn activate_hitbox(
attack_query: Query<(Entity, &AttackFrames, &Parent), Without<Collider>>,
animated_query: Query<&Animation>,
fighter_query: Query<(&Animation, &Handle<FighterMeta>)>,
mut commands: Commands,
fighter_assets: Res<Assets<FighterMeta>>,
) {
for (entity, attack_frames, parent) in attack_query.iter() {
if let Ok(animation) = animated_query.get(**parent) {
if let Ok((animation, fighter_meta)) = fighter_query.get(**parent) {
if animation.current_frame >= attack_frames.startup
&& animation.current_frame <= attack_frames.active
{
//TODO: insert Collider based on size and transform offset in attack asset
commands
.entity(entity)
.insert(Collider::cuboid(ATTACK_WIDTH * 0.8, ATTACK_HEIGHT * 0.8));
if let Some(fighter_data) = fighter_assets.get(fighter_meta) {
commands.entity(entity).insert(Collider::cuboid(
fighter_data.attack.hitbox.x,
fighter_data.attack.hitbox.y,
));
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub const CAMERA_SPEED: f32 = 0.8;
pub const MAX_Y: f32 = (GROUND_HEIGHT / 2.) + GROUND_Y;
pub const MIN_Y: f32 = -(GROUND_HEIGHT / 2.) + GROUND_Y;

pub const ATTACK_WIDTH: f32 = 16.;
pub const ATTACK_HEIGHT: f32 = 16.;
//TODO: remove in favor of loading attack velocity from YAML
pub const ATTACK_VELOCITY: f32 = 250.0;

pub const ITEM_LAYER: f32 = 100.;
Expand Down
6 changes: 3 additions & 3 deletions src/enemy_ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
animation::Facing,
consts::{self, ENEMY_MAX_ATTACK_DISTANCE, ENEMY_MIN_ATTACK_DISTANCE, ENEMY_TARGET_MAX_OFFSET},
enemy::{Enemy, TripPointX},
fighter_state::{Attacking, Idling, Moving, StateTransition, StateTransitionIntents},
fighter_state::{Idling, Moving, Punching, StateTransition, StateTransitionIntents},
player::Player,
Stats,
};
Expand Down Expand Up @@ -135,8 +135,8 @@ pub fn emit_enemy_intents(

// And attack!
intents.push_back(StateTransition::new(
Attacking::default(),
Attacking::PRIORITY,
Punching::default(),
Punching::PRIORITY,
false,
));

Expand Down
Loading