-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisometric_tilemap.pde
84 lines (68 loc) · 2.26 KB
/
isometric_tilemap.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
// isometric tile map in P3 by vvixi
PVector x = new PVector(0.5, 0.25);
PVector y = new PVector(-0.5, 0.25);
PImage t0, t1, t2, t3, t4, t5, t6, t7;
int gridSz = 10;
float tileH = 142, time;
float tileW = 116, y_off = 10;
PImage[] tiles = new PImage[19];
isoMap map;
void setup() {
surface.setTitle("Isometric Tilemap");
// load tiles from 10 on
for (int i = 18; i < tiles.length; i++) { tiles[i] = loadImage("assets/platformerTile_"+i+".png"); }
size(800, 800);
frameRate(32);
stroke(200);
map = new isoMap();
}
class isoMap {
float a, b, c, d, cap;
PVector p = new PVector(mouseX * tileH, (mouseY + tileH / 2) * tileW);
float tileArea = tileH * tileW;
float x2tileArea = 2 * tileH * tileW;
float constX = (-height / 2) * tileW + (width/2 * tileH);
float constY = (-height / 2) * tileW - (width/2 * tileH);
void make_isoMap() {
background(40);
translate(width/2-tileW/2, 50);
for (int i = 0; i < gridSz; i++) {
for (int j = 0; j < gridSz; j++) {
time++;
y_off+=1;
a = i * x.x;
b = j * y.x;
c = i * x.y;
d = j * y.y;
// formula for converting tilemap locations into isometric locations
PVector newV = new PVector((a) + (b), (c) + (d));
// add a nice wavy effect and draw tiles
image(tiles[18], (newV.x*tileW)+sin(y_off*.5), ((newV.y)*tileH)+sin(y_off*.5));
}
}
}
public PVector screen2map() {
// formula for capturing the mouse or entity location
// tiles anchored by center, not bottom
p = new PVector(mouseX * tileH, (mouseY + tileH / 2) * tileW);
PVector point = new PVector(floor((p.y - p.x + constX) / tileArea), floor((p.y + p.x + constY) / tileArea));
if (frameCount % 120 == 0) {
println(point);
}
return point;
}
public PVector map2screen() {
PVector screenCen = new PVector(int(width/2), int(height/2));
PVector screenPoint = new PVector(floor((p.y - p.x) * tileW / 2), floor((p.y + p.x) * tileH /2));
PVector result = screenCen.add(screenPoint);
result = new PVector(floor(result.x), floor(result.y));
if (frameCount % 120 == 0) {
println(result);
}
return result;
}
}
void draw() {
map.make_isoMap();
map.screen2map();
}