-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehaviours.pde
369 lines (320 loc) · 9.76 KB
/
Behaviours.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import java.util.Map;
import java.util.function.Predicate;
enum BehaviourType {
Destroyable, Obstacle, Movable, Projectile, Warrior, Targetable, AudioSource
}
/** Base class for all behaviours */
class Behaviour {
Object m_object; ///< parent object that has this behaviour
BehaviourType type; ///< type of the behaviour
Behaviour( BehaviourType t, Object obj ) {
type = t;
m_object = obj;
}
/**
* Derived classes can implement
* Called each tick
* Default - empty
*/
void update() {
}
}
/**
* Behaviour of hitable and destroyable objects
* Can process hits by bullets
*/
class Destroyable extends Behaviour {
float max_health;
float health; ///< current health
Hitbox hitbox; ///< hitbox of the object
Destroyable( Object obj ) {
super( BehaviourType.Destroyable, obj );
}
/**
* Checks if bullet @param b hits the hitbox
* If it does, calls hit method
*/
boolean onDamage( Bullet b ) {
if ( hitbox.contains( b.coor ) ) {
hit( b.damage );
return true;
}
return false;
}
/**
* A response for a hit
* Called when hit by a bullet
* Can be called in other situation
*/
void hit( float damage ) {
health -= damage;
}
}
/** Behaviour for obstacles-walls on the map */
class Obstacle extends Behaviour {
/**
* A set that contains passingTypes of bullets
* If an object`s type contains in this list, it will pass through
*/
private ArrayList<MovableType> passingTypes = new ArrayList<MovableType>();
Hitbox hitbox; ///< hitbox of a wall on the map
Obstacle( Object obj ) {
super( BehaviourType.Obstacle, obj );
}
/** Inserts a type @param t in a passingTypes */
void setPass( MovableType t ) {
if ( !passingTypes.contains( t ) ) {
passingTypes.add(t);
}
}
/** Removes a type @param t from a passingTypes */
void unsetPass( MovableType t ) {
if ( passingTypes.contains(t) ) {
passingTypes.remove(t);
}
}
/** Checks, if this type @param t can pass through */
boolean canPass( MovableType t ) {
return passingTypes.contains( t );
}
PVector isHit( PVector from, PVector to ) {
return hitbox.isHit( from, to );
}
}
enum MovableType {
Character, Projectile
}
/** Behaviour of pointy fast movables (like bullets) */
class Movable extends Behaviour {
MovableType type;
boolean canBounce = true;
//private int lastMove = -1;
Movable( Object obj, MovableType t ) {
super( BehaviourType.Movable, obj );
type = t;
}
/**
* Moves an object with coordinates @param coor by the vector @param vel
* and writes the result coordinates and velosity into @param coor and @param vel.
* Parameters shouldn`t be copied, so method could modify them.
*
* The method finds all obstacles on the map and modifies coors and velosity to bounce.
* If bounced, returns intersection coors, null otherwise.
*/
PVector move( PVector coor, PVector vel ) {
/*if ( lastMove == -1 ) {
lastMove = millis();
}*/
//float dt = millis()-lastMove;
//PVector move = vel.copy().mult( dt );
//println( "Movable.move( ", coor, vel, " )" );
ArrayList<Behaviour> obstacles = m_object.m_scene.getBehaviours( BehaviourType.Obstacle );
for ( int i = 0; i < obstacles.size(); i++ ) {
//Object obj = obstacles.get( i );
Obstacle beh = (Obstacle)obstacles.get(i);
if ( beh.canPass( type ) ) continue;
PVector bounced = beh.isHit( coor, PVector.add( coor, vel ) );
if ( bounced != null ) {
//println( "Bounced!" );
//println( "Prev velosity:", vel, "New velosity:", bounced );
/*move.x = bounced.x;
move.y = bounced.y;
vel.x = bounced.x / dt;
vel.y = bounced.y / dt;*/
if ( canBounce ) {
vel.x = bounced.x;
vel.y = bounced.y;
} else {
vel.x = 0;
vel.y = 0;
}
//vel = PVector.sub( bounced, PVector.add( coor, vel ) );
//vel.mult( -1 );
return bounced;
}
}
/*coor.add( move );
lastMove = millis();*/
coor.add( vel );
return null;
}
}
/**
* Uses algorithm for bouncing round movables.
* Can be used for modeling relativly big and slow movables (like characters).
*/
class BigRoundMovable extends Movable {
BigRoundMovable( Object obj, MovableType t ) {
super( obj, t );
type = t;
}
/**
* Moves an object with coordinates @param coor by the vector @param vel
* and writes the result coordinates and velosity into @param coor and @param vel.
* Parameters shouldn`t be copied, so method could modify them.
*
* The method finds all obstacles on the map and modifies coors and velosity to bounce.
*
* This method calculates taking account of size.
* If bounced, returns intersection coors, null otherwise.
*/
PVector move( PVector coor, PVector vel, float r ) {
ArrayList<Behaviour> obstacles = m_object.m_scene.getBehaviours( BehaviourType.Obstacle );
boolean bounced = false;
PVector d = null;
for ( int i = 0; i < obstacles.size(); i++ ) {
Obstacle beh = (Obstacle)obstacles.get(i);
if ( beh.canPass( type ) ) continue;
d = beh.hitbox.distVector( coor );
if ( d.mag() < r ) {
/** Big round movables bounce always. */
//verbose( "Bounced! " + d );
float a = PVector.angleBetween( vel, d );
if ( vel.copy().rotate( a ).dot( d ) < vel.copy().rotate( -a ).dot( d ) ) {
vel.rotate( PI - 2*a );
} else {
vel.rotate( PI + 2*a );
}
coor.add( d.mult( -(r-d.mag())/d.mag() ) );
bounced = true;
}
}
coor.add( vel );
if ( bounced ) {
vel.mult( 0.8 );
return PVector.add( coor, d );
}
return null;
}
}
/**
* Behaviour of an object in a team.
* Other objects in other teams will target an object with this behaviour.
*/
class Warrior extends Behaviour {
int teamNum;
Warrior( Object obj, int teamNum ) {
super( BehaviourType.Warrior, obj );
this.teamNum = teamNum;
}
int getTeamNum() {
return teamNum;
}
void changeTeam( int teamNum ) {
this.teamNum = teamNum;
}
}
/**
* Targeting and scanning module.
* Scans the map for enemies and targets them.
*/
class Targetable extends Behaviour {
Targetable( Object obj ) {
super( BehaviourType.Targetable, obj );
}
/** Returns if this team @param teamNum has enemies (Warriors) on the map. */
boolean hasEnemies( int teamNum ) {
ArrayList<Behaviour> allWarriors = m_object.m_scene.getBehaviours( BehaviourType.Warrior );
for ( Behaviour b : allWarriors ) {
if ( ((Warrior)b).teamNum != teamNum ) return true;
}
return false;
}
/** Finds the closest warrior object hostile to teamnum */
Object findByRange( PVector origin, int teamNum ) {
ArrayList<Object> objects = m_object.m_scene.getObjByBehaviour( BehaviourType.Warrior );
return findClosestFrom( origin, teamNum, objects );
}
Object findClosestFrom( PVector origin, int teamNum, ArrayList<Object> objects ) {
//checking hostile warriors
ArrayList<Warrior> hostiles = new ArrayList<Warrior>();
for ( int i = 0; i < objects.size(); i++ ) {
Object obj = objects.get(i);
if ( obj.hasBehaviour( BehaviourType.Warrior ) ) {
Warrior w = (Warrior)obj.getBehaviour( BehaviourType.Warrior );
if ( teamNum != w.teamNum ) {
hostiles.add( w );
}
}
}
//finding closest target
Object closest = null;
float dist = 10000;
for ( int i = 0; i < hostiles.size(); i++ ) {
Object obj = hostiles.get(i).m_object;
if ( origin.dist( obj.coor ) < dist ) {
closest = obj;
dist = origin.dist( obj.coor );
}
}
return closest;
}
}
/** Behaviour of noizemaking objects */
class AudioSource extends Behaviour {
private AudioRepository m_rep;
private HashMap<String, Thread> m_threads = new HashMap<String, Thread>();
AudioSource( Object obj, AudioRepository r ) {
super( BehaviourType.AudioSource, obj );
m_rep = r;
}
/**
* Plays sound only if previous sound with the same name already played @param duration.
* Returns false if previous sound haven't played that much.
* Duration in milliseconds.
*/
boolean playSoundWithDelay(final String name, final long duration) {
if ( m_threads.containsKey( name ) ) {
return false;
}
Thread th = m_rep.getSoundThread(name, duration);
if ( th == null ) return true;
th.start();
m_threads.put( name, th );
return true;
}
/**
* Plays random sound from a given folder @param name.
*/
boolean playRandomSoundWithDelay(final String categoryName, final long duration) {
if ( m_threads.containsKey( categoryName ) ) return false;
Thread th = m_rep.getRandomSoundThread(categoryName, duration);
if ( th == null ) return true;
th.start();
m_threads.put( categoryName, th );
return true;
}
/**
* Plays sound on the background
*/
void playSound(final String name) {
Thread th = m_rep.getSoundThreadOrRandomSoundThread(name);
th.start();
m_threads.put(name, th);
}
void playRandomSound(final String categoryName) {
Thread th = m_rep.getRandomSoundThread(categoryName);
th.start();
m_threads.put(categoryName, th);
}
/**
* Called each tick.
* Checks, if threads are alive, deletes dead threads.
*/
void update() {
HashMap<String, Thread> updated_threads = new HashMap<String, Thread>();
for ( HashMap.Entry<String, Thread> e : m_threads.entrySet() ) {
Thread th = e.getValue();
// println(th.getState());
if ( th.isAlive() ) {
updated_threads.put( e.getKey(), th );
}
}
m_threads = updated_threads;
}
}
/*class Shootable extends Behaviour {
Shootable( Object obj ) {
super( BehaviourType.Shootable, obj );
}
}*/