@@ -6,8 +6,10 @@ use bevy::{
6
6
input:: Input ,
7
7
math:: Vec2 ,
8
8
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 ,
10
11
} ,
12
+ sprite:: SpriteBundle ,
11
13
transform:: TransformBundle ,
12
14
} ;
13
15
use bevy_rapier2d:: prelude:: * ;
@@ -16,8 +18,8 @@ use iyes_loopless::prelude::*;
16
18
use crate :: {
17
19
animation:: Facing ,
18
20
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 } ,
21
23
state:: State ,
22
24
ArrivedEvent , Enemy , GameState , Player ,
23
25
} ;
@@ -43,6 +45,7 @@ fn player_attack(
43
45
query : Query < ( & Transform , & Facing , & State ) , With < Player > > ,
44
46
mut commands : Commands ,
45
47
keyboard : Res < Input < KeyCode > > ,
48
+ asset_server : Res < AssetServer > ,
46
49
) {
47
50
if keyboard. just_pressed ( KeyCode :: Return ) {
48
51
for ( transform, facing, state) in query. iter ( ) {
@@ -56,11 +59,24 @@ fn player_attack(
56
59
}
57
60
58
61
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
+ } )
64
80
. insert ( Collider :: cuboid ( ATTACK_WIDTH / 2. , ATTACK_HEIGHT / 2. ) )
65
81
. insert ( Sensor ( true ) )
66
82
. insert ( ActiveEvents :: COLLISION_EVENTS )
@@ -86,24 +102,30 @@ pub fn enemy_attack(
86
102
for event in event_reader. iter ( ) {
87
103
if let Ok ( mut state) = query. get_mut ( event. 0 ) {
88
104
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
+ }
107
129
}
108
130
}
109
131
}
0 commit comments