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
|
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
public class WorldScene extends Scene {
private ArrayList<Actor> actors = new ArrayList<Actor>();
private Map map;
private int tileSize;
private int panX;
private int panY;
public WorldScene(Dimension gridSize, int tileSize) {
this.tileSize = tileSize;
map = new Map(gridSize);
this.panX = 0;
this.panY = 0;
// TODO remove hardcoded stuff
Actor player = new Actor("pipo", 100, Actor.Type.PLAYER, gridSize);
player.equipWeapon(new Weapon(1000, 10, 1, 0));
Actor enemy = new Actor("marco", 100, Actor.Type.ENEMY, gridSize);
enemy.move(5, 5);
this.actors.add(player);
this.actors.add(enemy);
}
private Composite makeAlpha(Graphics2D g2d, float alpha) {
Composite originalComposite = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
return originalComposite;
}
@Override
public void zoom(int value) {
if (this.tileSize > 5 || value > 0) {
this.tileSize = this.tileSize + value;
}
}
@Override
public void pan(int x, int y) {
this.panX = this.panX + x;
this.panY = this.panY + y;
}
@Override
public void render(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// draw tiles
for (Tile tile : this.map.grid) {
switch (tile.type) {
case GRASS:
g2d.setColor(Palette.GREEN);
break;
case WATER:
g2d.setColor(Palette.BLUE);
break;
}
g2d.fillRect(
this.tileSize * tile.x + this.panX,
this.tileSize * tile.y + this.panY,
this.tileSize, this.tileSize
);
Composite originalComposite = makeAlpha(g2d, .5f);
if (tile.selected) {
g2d.setColor(Palette.RED);
g2d.fillRect(
this.tileSize * tile.x + this.panX,
this.tileSize * tile.y + this.panY,
this.tileSize, this.tileSize
);
}
// draw grid (with composite)
g2d.setPaint(Palette.BLACK);
g2d.drawRect(
this.tileSize * tile.x + this.panX,
this.tileSize * tile.y + this.panY,
this.tileSize, this.tileSize
);
g2d.setComposite(originalComposite);
}
// draw actors
for (Actor actor : this.actors) {
switch (actor.type) {
case PLAYER:
g2d.setColor(Palette.ORANGE);
break;
case ENEMY:
g2d.setColor(Palette.RED);
break;
}
int gap = this.tileSize / 10;
g2d.fillRect(
(this.tileSize * actor.getX()) + gap + this.panX,
(this.tileSize * actor.getY()) + gap + this.panY,
this.tileSize - gap * 2, this.tileSize - gap * 2
);
if (!actor.isAlive()) {
g2d.setColor(Palette.BLUE);
g2d.fillRect(
(this.tileSize * actor.getX()) + gap + this.panX,
(this.tileSize * actor.getY()) + this.tileSize/4 + this.panY,
this.tileSize - gap * 2, this.tileSize/4
);
}
}
}
@Override
public void mouseClicked(int x, int y) {
//TODO fix mouse tracking with pan
this.updateTiles();
Tile tile = map.getTile(
(int) ((x+this.panX)/(double)this.tileSize) + this.sceneXOffset,
(int) ((y+this.panY)/(double)this.tileSize) + this.sceneYOffset
);
//TODO remove test stuff
if (tile.getActor() != null) {
Actor actor = tile.getActor();
Actor player = this.actors.get(0);
player.shoot(actor, 1);
}
}
public void updateTiles() {
for (Tile tile : this.map.grid) {
tile.clearActor();
}
for (Actor actor : this.actors) {
Tile tile = map.getTile(actor.getX(), actor.getY());
tile.setActor(actor);
}
}
}
|