-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.java
executable file
·1563 lines (1087 loc) · 49.3 KB
/
Sprite.java
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.BitSet;
import java.util.ArrayList;
import javax.swing.JPanel;
import java.awt.Font;
import java.lang.Math;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.PixelGrabber;
import java.awt.image.RGBImageFilter;
import java.util.Random;
/*
Author: Jethro Holcroft.
Start Date: 16/01/2016.
The purpose of this class is to draw animated sprites that are easy to use in a game type environment, these sprites should be able to function as projectiles, pickups
animated caracters that are controled by the player with keys or mouse, animations in the background or effects such as explosions, buttons,
scrollable text boxes or even as a splash screen.
This class will contain functions that allow detection of collisions with other objects of Sprite, returns the angle between this Sprite and other objects of type sprite
Objects of this class will contain an array of images, these will be frames for animation, integers to store height width and positions.
Each object will be initialised with a name, this is so the object can be assigned a unique ID for various reasons, it will also help identify this object in debug messages.
Comments will reference changes of state or conditions, a sub class called stateData manages these,
a state is the frames for an animation to loop between, the position to draw the next iteration, the width and height dimensions if they need to change,
in this case a state will these details and addState() will contain arguments that will add a new state to sprite
if no states are added, then only the default state will be used, the width and height of the object will be default and based on the size of the frames.
A condition is an angle speed or velocity with data on which frames to loop through in case the sprites condition matches one saved in the array list 'conditions',
'addAngle/speed/velocityCondition()' are three seperate functions that will add conditions, pollConditions() will do the checking to see if a condition has changed
it is recomended that objects of this class will be stored in array lists or singularly in a wrapper class that is likley to be used in the main application
loop using an MVC style functionality, however a lone
object right at the start of a program can be used as a splash screen, a timer class and a start stop boolean will be included to aid extra functionality.
Emitters are particle fountains, the instructions for setting up and using are near line 1000 and the comments more or less explain how they are used,
further explantions also in documentation soon to be included
an additional button and menu class will also be incuded in the sprite class along with a special constructor, this is to take advantage of some of the functionality
of the sprite class to add a little
gui support, therefore default font variables and font loading support will also be included.
There will be a lot of debug messages and error handling, this is to help make this class easy to use.
comments will aim to explain variables and functions as though the reader is learning how to use the code, and functions and
variables will be written to best describe their purpose and allow as readable code
as possible, however a lot of Javas paint methods and graphics functionality/overides are best loosley referenced to.
Gravity and Thrust functions have been added, if yo uchoose gravity mode a gravity angle and strength has to be set, default is zero,
adding thrust acceleration will temporarily cancel gravity effects
and can be used for jumping or for lander style physics, setting gravity angle to be always at the center of another sprite using point to function will create a gravity well
TO-DO:
add subclasses that will use this sprite class to create a menu button array, text buttons, consider having an inventory style menu!
add sound functionality, call in state and condition change, have sound loaded in state data
*/
public class Sprite{
//this stores a contact sheet image, this will contain all frames of animation in one image
private BufferedImage contactSheet;
//this contains each frame of animation in the form of an array.
private ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
//stores various states for the sprite
private ArrayList<stateData> states = new ArrayList<stateData>();
//stores conditions of the sprite, calling 'pollState()' on each object in this array list
//and inputing angle, speed or velocity of in args will return true or false if that condition has been stored,
//and this sprites angle, speed or velocity matches
public ArrayList<stateData> conditions = new ArrayList<stateData>();
//this array will contain an array of different partical Emitters, particles or emiiters themselves can be accessed by their names with 'getPrticle()' or 'getEmitter()'
//it is very important that particles and emitters use a strict naming convention when created and used in order to keep track of what is going on
public ArrayList< Emitter> emitters = new ArrayList< Emitter>();
//these store dimensions of the sprite, frame stores the present frameNum the sprite will draw, frameStart and frameEnd are the start and end loops the
//animation will cycle through, default state has start at 0 and end at the number of frames, deltaX and Y are for 'deltaMove()' this function draws the sprite as normal but with added delta values
//this can be used for moving grids of sprites in relation to something
private int posX, posY, width, height, frameNum, frameStart, frameEnd, deltaX, deltaY;
//maxVelocity, velocity, angle and acceleration are for smooth movement functions of the sprite, acceleration is to add a spot of easing to the movement functions
//to create a natural movement maxVelocity is the set speed velocity will build up or down to based on acceleration
private float maxVelocity, velocity, acceleration, gravityAcceleration;
//angle variable, must be a double for easy conversion to degrees
private double angle, gravityAngle, thrustAngle, gravity, thrustAcceleration;
//these are to check if the sprite is colliding with another sprite/colliding has been set to true, if the mouse is over the spite and if the sprite
//has been selected (mouse over sprite and a mouse click has been detected selected is toggled to true and false), gravity mode applies top down gravity
//vector each time moveSPrite is called if it is set to true
private boolean colliding, collideAbove, collideBelow, collideLeft, collideRight, mouseOver, constantSpeed, gravityMode;
private int boundsLeft, boundsRight, boundsTop, boundsBottom;
public boolean selected;
//this stores the present state of the object, its set to default. name identifies object
private String state = "default", name;
//default font is 'serif'
private Font font = new Font("serif", Font.PLAIN, 30);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// CONSTRUCTORS; //
// //
// Several different types of constructor for several different types of sprite, the first is a standard animated sprite, //
// the second takes an argument that determines. What kind of sprite to produce based on the argument containting 'functionality' //
// //
// The second two are for plain images one takes a path to an image the other an image passed into it, this is for dynamic initialisation //
// //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This constructor is for a standard animated sprite,
//constructor requires the image name in the form of a string, a path to load the image from, and the rows and columns the contact sheet is split up into.
//this constructor can throw an exception if it is not initialised properly.
public Sprite( String _name, String _path, int _rows, int _cols) throws Exception{
try{
//assign name
name = _name;
//temporary load image function
contactSheet = ImageIO.read( new File( _path));
//init frames array
initFrames( _rows, _cols);
//set default state of sprite, if an uneven table this will need to be over written
//to create an alternative default state that will not draw the blank frames
addState( "default", 0, ( _rows * _cols) - 1, height, width, 0, 0, 0, 0);
//activate the default state
activateState("default");
}catch(Exception e){
System.out.println("Initialisation of " + _name + " failed: " + e.toString());
}
}
public Sprite(String _functionality, String _name, String _path, int _rows, int _cols) throws Exception{
try{
//assign name
name = _name;
//temporary load image function
contactSheet = ImageIO.read(new File(_path));
//init frames array
initFrames(_rows, _cols);
//set default state of sprite
addState( "default", 0, ( _rows * _cols) - 1, height, width, 0, 0, 0, 0);
//activate the default state
activateState("default");
}catch(Exception e){
System.out.println("Initialisation of " + _name + " failed: " + e.toString());
}
}
//this constructor is for a non animated sprite, just a rectangular image, this constructor
//takes a buffered image as an arg
public Sprite(String _name, BufferedImage _img) throws Exception{
try{
//assign name
name = _name;
//initialises straight from args
contactSheet = _img;
//set default state of sprite
addState( "default", 0, 0, height, width, 0, 0, 0, 0);
//init frames array
initFrames( 1, 1);
}catch(Exception e){
System.out.println("Initialisation of " + _name + " failed: " + e.toString());
}
}
//takes one image from path, no animation
public Sprite(String _name, String _path) throws Exception{
try{
//assign name
name = _name;
//temporary load image function
contactSheet = ImageIO.read(new File(_path));
//set default state of sprite
addState( "default", 0, 0, height, width, 0, 0, 0, 0);
//init frames array
initFrames( 0, 0);
}catch(Exception e){
System.out.println("Initialisation of " + _name + " failed: " + e.toString());
}
}
//this function initialises the array list frames with images cut from the contactSheet and will be called in the constructor,
//rows and cols along with the dimensions of contactSheet are used to work out the size of each frame to cut out, this only works if the
//contactSheet contains images divided evenly into rows and columns and also in the same relative position within it.
//the function returns a boolean if it was successful or not.
private void initFrames(int _rows, int _cols){
//this initialises the width and heigh of each sub image for use cutting up the contact sheet, it does not have to be the width and height
//of the sprite itself however it will remain so unless any states are added.
width = contactSheet.getWidth() / _cols;
height = contactSheet.getHeight() / _rows;
for(int r = 0; r < _rows; r++ ){
for(int c = 0; c < _cols; c++){
//this function uses the available variables to slice up the contact sheet in even parts
frames.add( contactSheet.getSubimage( c * width, r * height, width, height));
}
}
}
//this function adds a state to the sprite, the first state is always the default state, other states are added by the user, if you want
//a value to stay the same when you add it use the state in args for example in height use sprite.getHeight() in your code.
//this will also overwrite a state with the same name
public void addState( String _stateName, int _fStart, int _fEnd, int _height, int _width, float _mvel, float _vel, float _acc, double _ang){
//loops through states search for statename allready
for(int x = 0; x < states.size() -1; x++){
//if state name allrady exists
if(states.get(x).dName == _stateName){
//delete that state and break out the loop
states.remove(x);
//message alerts to name and state that is over written
System.out.println(name + " state " + _stateName + " over written with new perameters!");
break;
}
}
//add state to
states.add(new stateData( _stateName, _fStart, _fEnd, _height, _width, _mvel, _vel, _acc, _ang));
}
//this function activates the state with the same string passed in args, if it is not found, debug error message is returned
//and default state is set instead
public void activateState(String _setStateTo){
//if found is never set to true, state was never found and default state is used
boolean found = false;
//loops for each state in states array
for(stateData sd: states){
//if state with same name in args is found then
if(sd.dName == _setStateTo){
//change frames and size of sprite
frameStart = sd.dStart;
frameEnd = sd.dEnd;
width = sd.dWidth;
height = sd.dHeight;
maxVelocity = sd.dmaxVelocity;
velocity = sd.dvelocity;
acceleration = sd.dacceleration;
angle = sd.dangle;
//set to animate frames from new states animation loop
frameNum = frameStart + 1;
found = true;
break;
}
}
//if not found then set to default state
if( !found){
frameStart = 0;
maxVelocity = 0;
velocity = 0;
angle = 0;
acceleration = 0;
frameEnd = states.get(0).dEnd;
width = states.get(0).dWidth;
height = states.get(0).dHeight;
}
}
//as above function however if the state is allready active then the function is not called at all
public void continue_activateState(String _setStateTo){
if( !(getState().equals(_setStateTo))){
activateState(_setStateTo);
}
}
//this function also activates state almost the same as above however it does not change
//velocity, maxVelocity, angle or speed, this is to allow for just changing the frames
//if the conditions change, ideal for moving sprites with keys as keys are held down
public void semi_activateState( String _setStateTo){
//if found is never set to true, state was never found and default state is used
boolean found = false;
//set temp variables
float _maxVelocity = getmaxVelocity();
float _velocity = getVelocity();
float _acceleration = getAcceleration();
double _angle = getAngle();
//call activate state as normal
activateState(_setStateTo);
//set member variables with tems as they were
setmaxVelocity(_maxVelocity);
setVelocity(_velocity);
setAcceleration(_acceleration);
setAngle(_angle);
}
//adds angle condition to conditions array
public void addAngleCondition(double _startAngle, double _endAngle, int _frameStart, int _frameEnd){
conditions.add(new stateData(_startAngle, _endAngle, _frameStart, _frameEnd));
}
//adds speed condition to the conditions array
public void addSpeedCondition(int _speedStart, int _speedEnd, int _frameStart, int _frameEnd){
conditions.add(new stateData(_speedStart, _speedEnd, _frameStart, _frameEnd));
}
//adds velocity conditions to the conditions array
public void addVelocityCondition(boolean isIncreasing, int _velocity, int _frameStart, int _frameEnd){
conditions.add(new stateData( isIncreasing, _velocity, _frameStart, _frameEnd));
}
//adds velocity conditions to the conditions array
public void addJumpCondition( double _thrustAccelMin, double _thrustAngleMin, double _thrustAccelMax, double _thrustAngleMax, int _frameStart, int _frameEnd){
conditions.add(new stateData( _thrustAccelMin, _thrustAngleMin, _thrustAccelMax, _thrustAngleMax, _frameStart, _frameEnd));
}
//loops the conditions array to check if sprite matches any one of the saved conditions, if it does frame start and
//frame end are updated
public void pollConditions( String _condition){
//for each connection
for(int x = 0; x < conditions.size(); x++){
//call each condition based on input
conditions.get( x).checkCondition( _condition);
}
}
//this function returns the next frame in the animation loop
public BufferedImage nextFrame(){
//if the frame is not at the end of the loop
if(frameNum < frameEnd-1){
//iterate present frame by one and return it
frameNum++;
return frames.get(frameNum);
}else{
//reset frame to first frame in loop and return it
frameNum = frameStart;
return frames.get(frameNum);
}
}
//gets present frame
public BufferedImage getFrame(){
return frames.get(frameNum);
}
//overloaded gets frame in args
public BufferedImage getFrame(int _frame){
if(_frame < frames.size()){
return frames.get(_frame);
}
System.out.println( "Frame not found");
return frames.get(0);
}
//iterates next frame in loop but does not return it
public void nextFrame_noReturn(){
//if the frame is not at the end of the loop
if(frameNum > 0){
//iterate present frame by one
frameNum++;
}else{
//reset frame to first frame in loop
frameNum = frameStart;
}
}
//this function returns the previous frame in the animation loop
public BufferedImage previousFrame(){
//if the frame is not at the end of the loop
if(frameNum > 0){
//iterate present frame by one and return it
frameNum--;
return frames.get(frameNum);
}else{
//reset frame to first frame in loop and return it
frameNum = frames.size();
return frames.get(frameNum);
}
}
//adds a new frame to frame array
public void addFrame( BufferedImage _img){
frames.add( _img);
}
//replaces a frame, first arg is frame second arg is position in array if frame to replace
public void replaceFrame( BufferedImage _img, int _index){
frames.set(_index, _img);
}
//this function detects a collision with the sprite that is passed in args, if there is a collision
//colliding is set to true else false
boolean checkCollision( Sprite _spr){
//this is a standard collision dectetion algorithm using the sprites dimensions
if(getPosX() + getWidth() > _spr.getPosX() && getPosX() < _spr.getPosX() + _spr.getWidth() &&
getPosY() + getHeight() > _spr.getPosY() && getPosY() < _spr.getPosY() + _spr.getHeight()){
colliding = true;
return true;
}else{
colliding = false;
return false;
}
}
//this function checks if a collision was just to he left of the sprite
boolean checkCollisionLeft( Sprite _spr){
//creates the dimenstions for a box to the right of the sprite
int xbox = getPosX() -1;
int ybox = getPosY();
int wbox = 3;
int hbox = getHeight();
//checks if sprite passed in arguments is colliding and returns true or false respecivley
if( xbox > _spr.getPosX() && xbox + wbox > _spr.getPosX() && ybox + hbox < _spr.getPosY() && ybox > _spr.getPosY()){
return true;
}else{
return false;
}
}
//this function checks if the collision was just to he left of the sprite
boolean checkCollisionRight( Sprite _spr){
//creates the dimenstions for a box to the right of the sprite
int xbox = getPosX() +1;
int ybox = getPosY();
int wbox = 3;
int hbox = getHeight();
//checks if sprite passed in arguments is colliding and returns true or false respecivley
if( xbox > _spr.getPosX() && xbox + wbox < _spr.getPosX() && ybox + hbox < _spr.getPosY() && ybox > _spr.getPosY()){
return true;
}else{
return false;
}
}
//this function checks if the collision was just to he left of the sprite
boolean checkCollisionAbove( Sprite _spr){
//creates the dimenstions for a box to the right of the sprite
int xbox = getPosX();
int ybox = getPosY() -1;
int wbox = getWidth();
int hbox = 3;
//checks if sprite passed in arguments is colliding and returns true or false respecivley
if( xbox + wbox < _spr.getPosX() && xbox > _spr.getPosX() && ybox + hbox < _spr.getPosY() && ybox > _spr.getPosY()){
return true;
}else{
return false;
}
}
//this function checks if the collision was just to he left of the sprite
boolean checkCollisionBelow( Sprite _spr){
//creates the dimenstions for a box to the right of the sprite
int xbox = getPosX();
int ybox = getPosY() +1;
int wbox = getWidth();
int hbox = 3;
//checks if sprite passed in arguments is colliding and returns true or false respecivley
if( xbox + wbox < _spr.getPosX() && xbox > _spr.getPosX() && ybox + hbox < _spr.getPosY() && ybox > _spr.getPosY()){
return true;
}else{
return false;
}
}
//the following collisions detect if the player has left bounds that can be set in the constructor after the sprite
//object is created, this is meant for detecting if the sprite, is leaving left the bounds of the screen
public boolean leftBound(){
if( getPosX() <= boundsLeft){
return true;
}else{
return false;
}
}
public boolean rightBound(){
if( getPosX() >= boundsRight){
return true;
}else{
return false;
}
}
public boolean bottomBound(){
if( getPosY() <= boundsBottom){
return true;
}else{
return false;
}
}
public boolean topBound(){
if( getPosY() >= boundsTop){
return true;
}else{
return false;
}
}
//this function detects a circular collision with the centre of the sprite that is passed in args, the second argument is the diameter of
//the circle to collid with
boolean circularCollision(Sprite _spr, int _radius){
int squarex = deltaX + getPosX() + (getWidth()/2) - _spr.getPosX() + (getWidth()/2);
int squarey = deltaY + getPosY() + (getHeight()/2) - _spr.getPosY() + (getHeight()/2);
//this is a standard circular collision detetion algorithm using the sprites dimensions, the circular collision will collid with the center of
//this sprite from a circle eminating from the center of the sprite to check
if(Math.sqrt( (squarex * squarex) + (squarey * squarey)) < _radius){
colliding = false;
return true;
}else{
colliding = true;
return false;
}
}
//this function will set the angle to be pointing towards the sprite in args
public void pointTo( Sprite _sprite){
double angleTo = Math.toDegrees(Math.abs( Math.atan2( getPosX() - _sprite.getPosX(), getPosY() - _sprite.getPosY())));
//inverse the angle as it is relative to
setAngle( angleTo);
}
//this function will set the angle to be pointing towards the sprite in args
public float pointToTwo( Sprite _sprite){
float angle = (float)Math.toDegrees(Math.atan2((double)( getPosY() - _sprite.getPosY()),(double)(getPosX() - _sprite.getPosX())));
angle = (angle + 180.0f) % 360.0f;
setAngle( angle);
return angle;
}
//this function will return an angle to the sprite in args
public double getAngleTo( Sprite _sprite){
return Math.toDegrees( Math.atan2( _sprite.getPosY() - getPosY(), _sprite.getPosX() - getPosX()));
}
public double getDistance( Sprite _sprite){
return Math.sqrt(( ( _sprite.getPosX() - getPosX())^2) + ((_sprite.getPosY() - getPosY())^2));
}
//this function moves the sprite one unit towards its present vector each time it is called each time it is called
public void moveSprite(){
//set maxVelocity based on acceleration
accelerate();
//if gravity mode is not active
if( gravityMode){
//adds gravitational downward acceleration if sprite is not colliding with anything
gravityAcceleration += gravity;
if( colliding){
gravityAcceleration = 0;
}
if( thrustAcceleration > 0){
thrustAcceleration -= gravity;
//zeros thrust if it goes under zero
if(thrustAcceleration < 0){
thrustAcceleration = 0;
}
}
/// IF THIS WORKS CREATE VECTOR ADDING FUNCTION
//adding thrust forces to movement vector
setXY( posX += thrustAcceleration * Math.cos( Math.toRadians( thrustAngle)), posY += thrustAcceleration * Math.sin( Math.toRadians( thrustAngle)));
//adding gravity to movement vector
setXY( posX += gravityAcceleration * Math.cos( Math.toRadians( gravityAngle)), posY += gravityAcceleration* Math.sin( Math.toRadians( gravityAngle)));
//increment positionX and Y using trig, I always found this link exceptionally usefull http://www.helixsoft.nl/articles/circle/sincos.htm
//when first learning to use this
setXY( posX += velocity * Math.cos(Math.toRadians(angle)), posY += velocity * Math.sin(Math.toRadians(angle)));
}else{
//increment positionX and Y using trig, I always found this link exceptionally usefull http://www.helixsoft.nl/articles/circle/sincos.htm
//when first learning to use this
setXY( posX += velocity * Math.cos(Math.toRadians(angle)), posY += velocity * Math.sin(Math.toRadians(angle)));
}
}
//superficially moves the sprite with its present position plus a delta value
public void deltaMove(){
//adds delta value to existing value
setXY( posX + deltaX, posY + deltaY);
}
//manages acceleration easing if speed has not been set to constant
public void accelerate(){
//is this less overhead than a single if statment with this block nested in?
if(velocity < maxVelocity && !constantSpeed){
velocity += acceleration;
}else if(velocity > maxVelocity && !constantSpeed){
velocity -= acceleration;
}
}
//will display text at an x and y distance from the top left corner from the sprite
//function takes a graphics object to draw to, the text to display and the the x and y
//offsets, remember to call after the sprite has been drawn to not draw the sprite over the text
public void drawString(Graphics g2, String _text, int _x, int _y){
g2.drawString( _text, getPosX() + _x, getPosY() + _y);
}
//////////////////////////////////
// Getters and Setters //
//////////////////////////////////
public int getFrameNumber(){
return frameNum;
}
public void setFrameNumber(int num){
frameNum = num;
}
//setting sprites bounds collisions
public void setAllBounds(int _left, int _right, int _top, int _bottom){
boundsLeft = _left;
boundsRight = _right;
boundsTop = _top;
boundsBottom = _bottom;
}
//the object can just be set to collide for other collision situations
public void setCollision(boolean _collide){
colliding = _collide;
}
//this functin returns true of the object is colliding and false if it is not
public boolean getCollision(){
return colliding;
}
//returns width
public int getWidth(){
return width;
}
//returns height
public int getHeight(){
return height;
}
//returns position X
public int getPosX(){
return posX;
}
//returns position Y
public int getPosY(){
return posY;
}
//returns angle
public double getAngle(){
return angle;
}
//returns maxVelocity
public float getmaxVelocity(){
return maxVelocity;
}
//returns maxVelocity
public float getVelocity(){
return velocity;
}
//returns acceleration
public float getAcceleration(){
return acceleration;
}
//returns name
public String getName(){
return name;
}
//sets name
public void setName(String _name){
name = _name;
}
//sets angle
public void setAngle(double _angle){
angle = _angle;
}
//set width
public void setWidth(int _width){
width = _width;
}
//set height
public void setHeight(int _height){
height = _height;
}
//this re-sets the X and y positions
public void setWH( int _w, int _h ){
width = _w;
height = _h;
}
//sets the present speed for the sprite
public void setVelocity(float _Velocity){
velocity = _Velocity;
}
//sets the max speed for the sprite
public void setmaxVelocity(float _maxVelocity){
maxVelocity = _maxVelocity;
}
//sets acceleration, there is a danger of dividing by zero if movement functions are not robustly made
//please be observant
public void setAcceleration(float _acceleration){
acceleration = _acceleration;
}
//this function returns the present state of the object
public String getState(){
return state;
}
//set speed to be constant or eased with velocity
public void toConstantSpeed(boolean _cspeed){
constantSpeed = _cspeed;
}
//this re-sets the X and y positions
public void setXY( int _x, int _y ){
posX = _x;
posY = _y;
}
//sets an offset for a sprite or a range of sprites
public void setDelta( int _dx, int _dy){
deltaX = _dx;
deltaY = _dy;
}
public boolean getGravityMode(){
return gravityMode;
}
public void setGravityMode( boolean _gm){
gravityMode = _gm;
}
public void setGravity( double _gravity){
gravity = _gravity;
}
public void setGravityAngle( double _gA){
gravityAngle = _gA;
}
public double getGravity(){
return gravity;
}
public double getGravityAngle(){
return gravityAngle;
}
public void setThrustAngle( double _tA){
thrustAngle = _tA;
}
public double getThrustAngle(){
return thrustAngle;
}
public void setThrustAcceleration( double _tA){
thrustAcceleration = _tA;
}
public double getThrustAcceleration(){
return thrustAcceleration;
}
/*******************************************************************************************************************************
Emitters are a sub class that manage an array of sprites to create a particle fountain or projectile launch effect
Following functions will be related to following sub class 'Emitter'
AddEmitter( String, String, double, int, int, double, int, boolean, int, int) is a function in sprite that should be called when the sprite is created,
'fireEmitter("emitters name")'' or 'activateFountain(" emitters name")' will acivate preset behaviour that will be contained in the arguments of 'addEmitter()', 'drawEitter( "NAME OF EMITTER")'' will
draw each of that emitters particals and 'detectParticleCollision( "NAME_OF_EMITTER", Sprite _sprite)' will check the emitter with the name
in args one for collisions with each particle it owns against the sprite passed in args two.
particals are still destroyed after a certain amount of distance from the emitter is reached, this is because even though java does do garbage collection
java would still try and draw each and every particle unless it was removed at some point, it is also possible to give them a life time with a get ticks object.
**********************************************************************************************************************************/
//adds a new emitter to the sprite
public void addEmitter( String _name, String _path, int _error, int _burst, int _burstrate, double _angle, int _vel, boolean _grav){
emitters.add( new Emitter( _name, _path, _error, _burst, _burstrate, _angle, _vel, _grav));
}
//fires one particle from the emitter
public void fireEmitter( String _name){
for( int e = 0; e < emitters.size() ; e++){
if( emitters.get( e).getEmitterName() == _name){
emitters.get( e).fire();
}
}
}
//draws particles in Emitter
public void drawEmitter( String _name, Graphics _gr2){
for( int e = 0; e < emitters.size(); e++){
if( emitters.get( e).getEmitterName() == _name){
emitters.get( e).draw( _gr2);
}
}
}