forked from Pomax/Pjs-2D-Game-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.pde
executable file
·151 lines (131 loc) · 4.13 KB
/
State.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
/**
* A sprite state is mostly a thin wrapper
*/
class State {
String name;
Sprite sprite;
Actor actor;
int duration = -1,
served = 0;
// this point is considered the "center point" when
// swapping between states. If a state has an anchor
// at (3,3), mapping to world coordinate (240,388)
// and it swaps for a state that has (10,10) as anchor,
// the associated actor is moved (-7,-7) to effect
// the anchor being in the same place before and after.
float ax, ay;
// shortcut constructor
State(String name, String spritesheet) {
this(name, spritesheet, 1, 1);
}
// bigger shortcut constructor
State(String _name, String spritesheet, int rows, int cols) {
name = _name;
sprite = new Sprite(spritesheet, rows, cols);
sprite.setState(this);
}
/**
* add path points to a state
*/
void addPathPoint(float x, float y, int duration) { sprite.addPathPoint(x, y, 1,1,0, duration); }
/**
* add path points to a state (explicit scale and rotations)
*/
void addPathPoint(float x, float y, float sx, float sy, float r, int duration) { sprite.addPathPoint(x, y, sx, sy, r, duration); }
/**
* add a linear path to the state
*/
void addPathLine(float x1, float y1, float x2, float y2, float duration) {
sprite.addPathLine(x1,y1,1,1,0, x2,y2,1,1,0, duration); }
/**
* add a linear path to the state (explicit scale and rotations)
*/
void addPathLine(float x1, float y1, float sx1, float sy1, float r1,
float x2, float y2, float sx2, float sy2, float r2,
float duration) {
sprite.addPathLine(x1,y1,sx1,sy1,r1, x2,y2,sx2,sy2,r2, duration); }
/**
* add a curved path to the state
*/
void addPathCurve(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, float duration, float slowdown_ratio) {
sprite.addPathCurve(x1,y1,1,1,0, cx1,cy1,cx2,cy2, x2,y2,1,1,0, duration, slowdown_ratio); }
/**
* add a curved path to the state (explicit scale and rotations)
*/
void addPathCurve(float x1, float y1, float sx1, float sy1, float r1,
float cx1, float cy1,
float cx2, float cy2,
float x2, float y2, float sx2, float sy2, float r2,
float duration,
float slowdown_ratio) {
sprite.addPathCurve(x1,y1,sx1,sy1,r1, cx1,cy1,cx2,cy2, x2,y2,sx2,sy2,r2, duration, slowdown_ratio); }
/**
* incidate whether or not this is a looping path
*/
void setLooping(boolean l) {
sprite.setLooping(l);
}
/**
* Make this state last X frames.
*/
void setDuration(float _duration) {
setLooping(false);
duration = (int) _duration;
}
/**
* bind this state to an actor
*/
void setActor(Actor _actor) {
actor = _actor;
actor.width = sprite.width;
actor.height = sprite.height;
}
/**
* when the sprite is moved by its path,
* let the actor know of its updated position.
*/
void setActorOffsets(float x, float y) {
actor.setTranslation(x, y);
}
void setActorDimensions(float w, float h, float xa, float ya) {
actor.width = w;
actor.height = h;
actor.halign = xa;
actor.valign = ya;
}
// reset a sprite (used when swapping states)
void reset() {
sprite.reset();
served = 0;
}
// signal to the actor that the sprite is done running its path
void finished() {
if(actor!=null) {
actor.handleStateFinished(this); }}
// if the sprite has a non-looping path: is it done running that path?
boolean mayChange() {
if (duration != -1) {
return false;
}
return sprite.mayChange();
}
// drawing the state means draw the sprite
void draw(boolean disabled) {
// if disabled, only draw every other frame
if(disabled && frameCount%2==0) {}
//otherwise, draw all frames
else { sprite.draw(0,0); }
served++;
if(served == duration) {
finished();
}
}
// check if coordinate is in sprite
boolean over(float _x, float _y) {
return sprite.over(_x,_y);
}
// set sprite's animation
void setAnimationSpeed(float factor) {
sprite.setAnimationSpeed(factor);
}
}