-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntity.java
More file actions
197 lines (161 loc) · 4.85 KB
/
Entity.java
File metadata and controls
197 lines (161 loc) · 4.85 KB
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
package org.helllabs.java.asciiquarium;
import java.util.List;
public class Entity implements Comparable<Entity> {
String name;
int type = -1;
String[][] shape;
String[][] mask;
float fx, fy;
int height;
int width;
int depth;
float[] callbackArguments = { 0.0F, 0.0F, 0.0F, 0.0F };
EntityCallback callback = null;
EntityCallback deathCallback = null;
EntityCallback collHandler = null;
List<Entity> collisions = null;
boolean autoTrans = false;
boolean dieOffscreen = false;
boolean physical = false;
long dieTime = -1;
int dieFrame = -1;
float fframe = 0.0F;
int frame = 0;
int frameCount = 0;
Animation animation;
char defaultColor = 'w';
char transparent = '?';
boolean dead = false;
abstract interface EntityCallback {
public void run(Entity entity);
}
public Entity(String name, String[] shape, String[] mask, int x, int y, int depth) {
this.shape = new String[1][];
this.shape[0] = shape;
this.mask = new String[1][];
this.mask[0] = mask;
setVars(name, x, y, depth);
}
public Entity(String name, String shape, int x, int y, int depth) {
this.shape = new String[1][0];
this.shape[0] = new String[] { shape };
this.mask = null;
setVars(name, x, y, depth);
}
public Entity(String name, String[][] shape, String[][] mask, int x, int y, int depth) {
this.shape = shape;
this.mask = mask;
setVars(name, x, y, depth);
}
public Entity(String name, String[][] shape, String[] mask, int x, int y, int depth) {
this.shape = shape;
this.mask = new String[shape.length][];
for (int i = 0; i < shape.length; i++)
this.mask[i] = mask;
setVars(name, x, y, depth);
}
public Entity(String name, String[][] shape, int x, int y, int depth) {
this.shape = shape;
this.mask = null;
setVars(name, x, y, depth);
}
private void setVars(String name, int x, int y, int depth) {
this.name = name;
this.fx = x;
this.fy = y;
this.depth = depth;
int width = 0;
for (String s : shape[0]) { // we assume all frames have the same size
if (s.length() > width)
width = s.length();
}
this.height = shape[0].length;
this.width = width;
this.callback = moveCallback;
//Log.i("Asciiquarium", "Entity constructor: " + name + " (" + this.width + "x" + this.height + ")");
}
public void draw(Screen screen) {
draw(screen, 0);
}
public void draw(Screen screen, int frame) {
//Log.d("Asciiquarium", "Entity: draw " + name + " " + frame + "/" + (shape.length - 1));
int x = (int)fx;
int y = (int)fy;
if (frame >= shape.length || x >= screen.columns || y >= screen.rows)
return;
for (int lineNum = 0; lineNum < height; lineNum++) {
int yy = y + lineNum;
if (yy < 0)
continue;
if (yy >= screen.rows)
break;
final int length = shape[frame][lineNum].length();
/* Log.d("Asciiquarium", "Entity: draw start=" + start + " end=" + end +
" line=" + yy + " col=" + x); */
final String line = shape[frame][lineNum];
String attr = "";
int aLength = 0;
if (mask != null) {
attr = mask[frame][lineNum];
aLength = attr.length();
}
int offset = yy * screen.columns + x;
boolean flag = false;
char c, a; // character and attribute
for (int srcPos = 0; srcPos < length; srcPos++, offset++) {
c = line.charAt(srcPos);
if (c != ' ')
flag = true;
if (x + srcPos < 0 || x + srcPos >= screen.columns)
continue;
if (flag && c != transparent) {
screen.text[offset] = c;
if (srcPos < aLength) {
a = attr.charAt(srcPos);
if (a == ' ')
a = defaultColor;
screen.color[offset] = a;
} else {
screen.color[offset] = defaultColor;
}
}
}
}
//Log.d("Asciiquarium", "Entity: draw complete");
}
// The default callback. You can also call this from your own
// callback to do the work of moving and animating the entity
// after you have done whatever other processing you want to do.
EntityCallback moveCallback = new EntityCallback() {
public void run(Entity entity) {
entity.move();
}
};
public void move() {
if (callbackArguments[3] > 0) {
fframe += callbackArguments[3];
if (fframe >= shape.length)
fframe -= shape.length;
frame = (int)fframe;
}
frameCount++;
fx += callbackArguments[0];
fy += callbackArguments[1];
//Log.d("Asciiquarium", name + " frame = " + frame);
}
// Remove this entity from the animation. This is equivalent to:
//
// animation.delEntity(entity);
//
// This does not destroy the object, so you can still
// read it later (or put it in a different animation) as long
// as you have a reference to it.
public void kill() {
animation.delEntity(this);
}
// Comparable
@Override
public int compareTo(Entity e) {
return this.depth < e.depth ? 1 : this.depth > e.depth ? -1 : 0;
}
}