-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZSpriteRepeater.java
147 lines (122 loc) · 3.96 KB
/
ZSpriteRepeater.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
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
public class ZSpriteRepeater implements ScenePrimitive, JSONSerializable{
//transparent ZDraw
int repx, repy;
ZSprite sprite;
public ZSpriteRepeater(ZSprite sprite){
this.sprite = sprite;
repx = 1;
repy = 1;
}
public ZSpriteRepeater(ZSprite sprite, int repeatx, int repeaty){
repx = repeatx;
repy = repeaty;
this.sprite = sprite;
}
//O(1) shared copy (should never be modified)
public ZSpriteRepeater fastClone(){
ZSpriteRepeater res = new ZSpriteRepeater(sprite.fastClone(), repx, repy);
return res;
}
public void drawTo(ZDraw zbuffer, int xo, int yo){
// System.out.println("print to: " +x+"/"+y+" size: "+w+":"+h);
for (int rx = 0; rx < repx; rx++ )
for (int ry = 0; ry < repy; ry++ )
sprite.drawTo(zbuffer, xo + rx*sprite.w, yo + ry * sprite.h);
}
public void drawTo(ZDraw zbuffer, ZDraw colorMap, int xo, int yo){
// System.out.println("print to: " +x+"/"+y+" size: "+w+":"+h);
for (int rx = 0; rx < repx; rx++ )
for (int ry = 0; ry < repy; ry++ )
sprite.drawTo(zbuffer, colorMap, xo + rx*sprite.w, yo + ry * sprite.h);
}
public Vector3i centerI(){
return new Vector3i(sprite.x+sprite.w*(repx)/2, sprite.y+sprite.h*(repy)/2, sprite.z);
}
public void move(int x, int y, int z){
sprite.move(x, y, z);
}
public void moveTo(Vector3i to){
sprite.x=to.x-sprite.w*repx/2;
sprite.y=to.y-sprite.h*repy/2;
sprite.z=to.z;
}
public void moveTo(int x, int y, int z){
sprite.x=x-sprite.w*repx/2;
sprite.y=y-sprite.h*repy/2;
sprite.z=z;
}
public Vector3i cornerLTF(){
return sprite.cornerLTF();
}
public Vector3i cornerRBN(){
return new Vector3i(sprite.x + sprite.w * repx, sprite.y + sprite.h * repy, ZDraw.MAXZ);
}
public boolean inBounds(int x, int y)
{
return !((x < 0) || (x >= sprite.w * repx) || (y < 0) || (y >= sprite.h*repy));
}
public int zAt(int wx, int wy){
if (!inBounds(wx-sprite.x, wy-sprite.y)) return -1;
return sprite.zAt((wx - sprite.x) % sprite.w + sprite.x, (wy - sprite.y) % sprite.h + sprite.y);
}
public boolean intersect(ZSprite sprite, int dx, int dy){
/*//calculate intersection rect in local coords
int ox = sprite.x - x + dx;
int il = Math.max(0, ox);
int ir = Math.min(w, ox + sprite.w);
if (il >= ir)
return false;
int oy = sprite.y - y + dy;
int it = Math.max(0, oy);
int ib = Math.min(h, sprite.h + oy);
if (it >= ib)
return false;*/
for (int rx = 0; rx < repx; rx++ )
for (int ry = 0; ry < repy; ry++)
if (this.sprite.intersect(sprite, dx - rx*sprite.w, dy - ry * sprite.h, false))
return true;
return false;
}
public boolean intersectReversed(ZSprite sprite, int dx, int dy, boolean removeIntersection){
/*//calculate intersection rect in local coords
int ox = sprite.x - x + dx;
int il = Math.max(0, ox);
int ir = Math.min(w, ox + sprite.w);
if (il >= ir)
return false;
int oy = sprite.y - y + dy;
int it = Math.max(0, oy);
int ib = Math.min(h, sprite.h + oy);
if (it >= ib)
return false;*/
boolean result = false;
for (int rx = 0; rx < repx; rx++ )
for (int ry = 0; ry < repy; ry++)
if (sprite.intersect(this.sprite, dx + rx*sprite.w, dy + ry * sprite.h, removeIntersection)) {
result = true;
if (!removeIntersection) break;
}
return result;
}
public Object jsonSerialize(){
TreeMap<String,Object> tm = new TreeMap<String,Object>();
tm.put("type", "ZSpriteRepeater");
tm.put("position", new int[]{sprite.x, sprite.y, sprite.z});
tm.put("repeat", new int[]{repx, repy});
return tm;
}
public void jsonDeserialize(Object obj){
Map<String, Object> map = ((Map<String, Object>)obj);
assert ("ZSpriteRepeater".equals( map.get("type")));
ArrayList<Number> p = (ArrayList<Number>) map.get("position");
sprite.x=p.get(0).intValue();
sprite.y=p.get(1).intValue();
sprite.z=p.get(2).intValue();
ArrayList<Number> r = (ArrayList<Number>) map.get("repeat");
repx = r.get(0).intValue();
repy = r.get(1).intValue();
}
}