Skip to content

Commit 0060a90

Browse files
committed
added waiting state, random chance to not attack, temp sprite for attack
1 parent 57befb8 commit 0060a90

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

assets/fighters/bandit/bandit.fighter.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ spritesheet:
3030
frames: [40, 46]
3131
dying:
3232
frames: [40, 46]
33+
waiting:
34+
frames: [0, 7]
35+
repeat: false
3336
attacking:
3437
frames: [16, 23]

assets/fighters/slinger/slinger.fighter.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ spritesheet:
3030
frames: [40, 46]
3131
dying:
3232
frames: [40, 46]
33+
waiting:
34+
frames: [0, 3]
35+
repeat: false
3336
attacking:
3437
frames: [16, 20]

src/attack.rs

+48-26
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use bevy::{
66
input::Input,
77
math::Vec2,
88
prelude::{
9-
App, Commands, Component, Entity, EventReader, KeyCode, Plugin, Query, Res, Transform, With,
9+
default, App, AssetServer, Commands, Component, Entity, EventReader, KeyCode, Plugin,
10+
Query, Res, Transform, With,
1011
},
12+
sprite::SpriteBundle,
1113
transform::TransformBundle,
1214
};
1315
use bevy_rapier2d::prelude::*;
@@ -16,8 +18,8 @@ use iyes_loopless::prelude::*;
1618
use crate::{
1719
animation::Facing,
1820
collisions::BodyLayers,
19-
consts::{ATTACK_HEIGHT, ATTACK_LAYER, ATTACK_WIDTH},
20-
movement::{MoveInDirection, Target},
21+
consts::{self, ATTACK_HEIGHT, ATTACK_LAYER, ATTACK_WIDTH},
22+
movement::{MoveInDirection, Rotate, Target},
2123
state::State,
2224
ArrivedEvent, Enemy, GameState, Player,
2325
};
@@ -43,6 +45,7 @@ fn player_attack(
4345
query: Query<(&Transform, &Facing, &State), With<Player>>,
4446
mut commands: Commands,
4547
keyboard: Res<Input<KeyCode>>,
48+
asset_server: Res<AssetServer>,
4649
) {
4750
if keyboard.just_pressed(KeyCode::Return) {
4851
for (transform, facing, state) in query.iter() {
@@ -56,11 +59,24 @@ fn player_attack(
5659
}
5760

5861
commands
59-
.spawn_bundle(TransformBundle::from_transform(Transform::from_xyz(
60-
transform.translation.x,
61-
transform.translation.y,
62-
ATTACK_LAYER,
63-
)))
62+
// .spawn_bundle(TransformBundle::from_transform(Transform::from_xyz(
63+
// transform.translation.x,
64+
// transform.translation.y,
65+
// ATTACK_LAYER,
66+
// )))
67+
.spawn_bundle(SpriteBundle {
68+
texture: asset_server.load("bottled_seaweed11x31.png"),
69+
transform: Transform::from_xyz(
70+
transform.translation.x,
71+
transform.translation.y,
72+
ATTACK_LAYER,
73+
),
74+
..default()
75+
})
76+
.insert(Rotate {
77+
speed: consts::THROW_ITEM_ROTATION_SPEED,
78+
to_right: !facing.is_left(),
79+
})
6480
.insert(Collider::cuboid(ATTACK_WIDTH / 2., ATTACK_HEIGHT / 2.))
6581
.insert(Sensor(true))
6682
.insert(ActiveEvents::COLLISION_EVENTS)
@@ -86,24 +102,30 @@ pub fn enemy_attack(
86102
for event in event_reader.iter() {
87103
if let Ok(mut state) = query.get_mut(event.0) {
88104
if *state != State::Attacking {
89-
state.set(State::Attacking);
90-
let attack_entity = commands
91-
.spawn_bundle(TransformBundle::default())
92-
.insert(Collider::cuboid(ATTACK_WIDTH * 0.8, ATTACK_HEIGHT * 0.8))
93-
.insert(Sensor(true))
94-
.insert(ActiveEvents::COLLISION_EVENTS)
95-
.insert(ActiveCollisionTypes::default() | ActiveCollisionTypes::STATIC_STATIC)
96-
.insert(CollisionGroups::new(
97-
BodyLayers::ENEMY_ATTACK,
98-
BodyLayers::PLAYER,
99-
))
100-
.insert(Attack { damage: 10 })
101-
.insert(AttackTimer(Timer::new(
102-
Duration::from_secs_f32(0.48),
103-
false,
104-
)))
105-
.id();
106-
commands.entity(event.0).push_children(&[attack_entity]);
105+
if rand::random() && *state != State::Waiting {
106+
state.set(State::Waiting);
107+
} else {
108+
state.set(State::Attacking);
109+
let attack_entity = commands
110+
.spawn_bundle(TransformBundle::default())
111+
.insert(Collider::cuboid(ATTACK_WIDTH * 0.8, ATTACK_HEIGHT * 0.8))
112+
.insert(Sensor(true))
113+
.insert(ActiveEvents::COLLISION_EVENTS)
114+
.insert(
115+
ActiveCollisionTypes::default() | ActiveCollisionTypes::STATIC_STATIC,
116+
)
117+
.insert(CollisionGroups::new(
118+
BodyLayers::ENEMY_ATTACK,
119+
BodyLayers::PLAYER,
120+
))
121+
.insert(Attack { damage: 10 })
122+
.insert(AttackTimer(Timer::new(
123+
Duration::from_secs_f32(0.48),
124+
false,
125+
)))
126+
.id();
127+
commands.entity(event.0).push_children(&[attack_entity]);
128+
}
107129
}
108130
}
109131
}

src/state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub enum State {
2121
KnockedLeft,
2222
KnockedRight,
2323
// Hitstun,
24+
Waiting,
2425
Dying,
2526
}
2627

@@ -35,6 +36,7 @@ impl TryFrom<String> for State {
3536
"knocked_left" => State::KnockedLeft,
3637
"knocked_right" => State::KnockedRight,
3738
// "hitstun" => State::Hitstun,
39+
"waiting" => State::Waiting,
3840
"dying" => State::Dying,
3941
_ => {
4042
return Err("invalid value");

0 commit comments

Comments
 (0)