Skip to content

Commit

Permalink
added waiting state, random chance to not attack, temp sprite for attack
Browse files Browse the repository at this point in the history
  • Loading branch information
odecay committed Jul 6, 2022
1 parent 57befb8 commit 0060a90
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
3 changes: 3 additions & 0 deletions assets/fighters/bandit/bandit.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ spritesheet:
frames: [40, 46]
dying:
frames: [40, 46]
waiting:
frames: [0, 7]
repeat: false
attacking:
frames: [16, 23]
3 changes: 3 additions & 0 deletions assets/fighters/slinger/slinger.fighter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ spritesheet:
frames: [40, 46]
dying:
frames: [40, 46]
waiting:
frames: [0, 3]
repeat: false
attacking:
frames: [16, 20]
74 changes: 48 additions & 26 deletions src/attack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use bevy::{
input::Input,
math::Vec2,
prelude::{
App, Commands, Component, Entity, EventReader, KeyCode, Plugin, Query, Res, Transform, With,
default, App, AssetServer, Commands, Component, Entity, EventReader, KeyCode, Plugin,
Query, Res, Transform, With,
},
sprite::SpriteBundle,
transform::TransformBundle,
};
use bevy_rapier2d::prelude::*;
Expand All @@ -16,8 +18,8 @@ use iyes_loopless::prelude::*;
use crate::{
animation::Facing,
collisions::BodyLayers,
consts::{ATTACK_HEIGHT, ATTACK_LAYER, ATTACK_WIDTH},
movement::{MoveInDirection, Target},
consts::{self, ATTACK_HEIGHT, ATTACK_LAYER, ATTACK_WIDTH},
movement::{MoveInDirection, Rotate, Target},
state::State,
ArrivedEvent, Enemy, GameState, Player,
};
Expand All @@ -43,6 +45,7 @@ fn player_attack(
query: Query<(&Transform, &Facing, &State), With<Player>>,
mut commands: Commands,
keyboard: Res<Input<KeyCode>>,
asset_server: Res<AssetServer>,
) {
if keyboard.just_pressed(KeyCode::Return) {
for (transform, facing, state) in query.iter() {
Expand All @@ -56,11 +59,24 @@ fn player_attack(
}

commands
.spawn_bundle(TransformBundle::from_transform(Transform::from_xyz(
transform.translation.x,
transform.translation.y,
ATTACK_LAYER,
)))
// .spawn_bundle(TransformBundle::from_transform(Transform::from_xyz(
// transform.translation.x,
// transform.translation.y,
// ATTACK_LAYER,
// )))
.spawn_bundle(SpriteBundle {
texture: asset_server.load("bottled_seaweed11x31.png"),
transform: Transform::from_xyz(
transform.translation.x,
transform.translation.y,
ATTACK_LAYER,
),
..default()
})
.insert(Rotate {
speed: consts::THROW_ITEM_ROTATION_SPEED,
to_right: !facing.is_left(),
})
.insert(Collider::cuboid(ATTACK_WIDTH / 2., ATTACK_HEIGHT / 2.))
.insert(Sensor(true))
.insert(ActiveEvents::COLLISION_EVENTS)
Expand All @@ -86,24 +102,30 @@ pub fn enemy_attack(
for event in event_reader.iter() {
if let Ok(mut state) = query.get_mut(event.0) {
if *state != State::Attacking {
state.set(State::Attacking);
let attack_entity = commands
.spawn_bundle(TransformBundle::default())
.insert(Collider::cuboid(ATTACK_WIDTH * 0.8, ATTACK_HEIGHT * 0.8))
.insert(Sensor(true))
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(ActiveCollisionTypes::default() | ActiveCollisionTypes::STATIC_STATIC)
.insert(CollisionGroups::new(
BodyLayers::ENEMY_ATTACK,
BodyLayers::PLAYER,
))
.insert(Attack { damage: 10 })
.insert(AttackTimer(Timer::new(
Duration::from_secs_f32(0.48),
false,
)))
.id();
commands.entity(event.0).push_children(&[attack_entity]);
if rand::random() && *state != State::Waiting {
state.set(State::Waiting);
} else {
state.set(State::Attacking);
let attack_entity = commands
.spawn_bundle(TransformBundle::default())
.insert(Collider::cuboid(ATTACK_WIDTH * 0.8, ATTACK_HEIGHT * 0.8))
.insert(Sensor(true))
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(
ActiveCollisionTypes::default() | ActiveCollisionTypes::STATIC_STATIC,
)
.insert(CollisionGroups::new(
BodyLayers::ENEMY_ATTACK,
BodyLayers::PLAYER,
))
.insert(Attack { damage: 10 })
.insert(AttackTimer(Timer::new(
Duration::from_secs_f32(0.48),
false,
)))
.id();
commands.entity(event.0).push_children(&[attack_entity]);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum State {
KnockedLeft,
KnockedRight,
// Hitstun,
Waiting,
Dying,
}

Expand All @@ -35,6 +36,7 @@ impl TryFrom<String> for State {
"knocked_left" => State::KnockedLeft,
"knocked_right" => State::KnockedRight,
// "hitstun" => State::Hitstun,
"waiting" => State::Waiting,
"dying" => State::Dying,
_ => {
return Err("invalid value");
Expand Down

0 comments on commit 0060a90

Please sign in to comment.