-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
170 lines (151 loc) · 5.46 KB
/
Cell.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ATSSG;
import ATSSG.Entities.Entity;
import ATSSG.Enums.TerrainType;
import ATSSG.GUI.GooeyJButton;
import ATSSG.Player.Player;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class Cell implements Comparable<Cell> {
//Variables
protected TerrainType terrainType;
protected ResourceItem occupyingResource; //Was listed in design doc as just Resource; I assumed ResourceItem, could also be ResourceType
protected Collection<Entity> occupyingEntities;
protected GameMap containingMap;
protected int x, y; //location, may want to turn into an inner class or something
protected GooeyJButton view;
protected Image[] terrIcons;
protected int terrainFlavor;
//Constructors
public Cell(TerrainType t, ResourceItem r, GameMap m, int x_in, int y_in){
this.x = x_in;
this.y = y_in;
this.terrainType = t;
this.terrainFlavor = terrainType.getTerrainFlavor();
this.terrIcons = terrainType.getImages(terrainFlavor);
this.occupyingResource = r;
this.containingMap = m;
this.occupyingEntities = new ArrayList<Entity>(8);
this.view = new GooeyJButton(null, this);
}
//Methods
public void updateView(int iconW, int iconH) {
this.terrIcons = terrainType.getImages(terrainFlavor);
Icon icon;
Collection<Entity> occs = getOccupyingEntities();
if (occs == null || occs.isEmpty()) {
icon = new ImageIcon(terrIcons[0]);
this.view.setToolTipText(null);
} else if (occs.size() == 1) {
Entity ent = occs.iterator().next();
ent.updateHealthyImage();
Image background = terrIcons[2];
background.getGraphics().drawImage(ent.getHealthyImage(), 0, 0, null);
icon = new ImageIcon(background.getScaledInstance(iconW, iconH, Image.SCALE_SMOOTH));
this.view.setToolTipText(ent.toString());
} else {
Image[] entities = new Image[occs.size()];
Iterator<Entity> itr = occs.iterator();
for (int i = 0; i < entities.length; i++) {
entities[i] = itr.next().getImage();
}
Image background = terrIcons[2];
double iW = background.getWidth(null);
double iH = background.getHeight(null);
for (int j = 0; j < entities.length; j++) {
background.getGraphics().drawImage(entities[j].getScaledInstance((int)(iW/Math.pow(1.3, j)), (int)(iH/Math.pow(1.3, j)), Image.SCALE_SMOOTH),
(int)(iW-iW/Math.pow(1.3, j)), (int)(iH-iH/Math.pow(1.3, j)), null);
}
icon = new ImageIcon(background.getScaledInstance(iconW, iconH, Image.SCALE_SMOOTH));
this.view.setToolTipText("Deathball: Magnitude " + occs.size());
}
this.view.setIcon(icon);
}
public void setActionListener(ActionListener actlis) {
for (ActionListener al : view.getActionListeners()) {
view.removeActionListener(al);
}
view.addActionListener(actlis);
}
public GooeyJButton getView() {
return view;
}
//Note that this makes Cell inconsistent with equals.
public int compareTo(Cell target) {
int number = (10000 * target.getY() + target.getX())-(10000 * this.getY() + this.getX());
return number / Math.abs(number);
}
public Collection<Cell> getAdjacent(){
ArrayList<Cell> cell_list = new ArrayList<Cell>(8);
GameMap m = this.containingMap;
Cell top_left = m.getCell(x - 1, y - 1);
if(top_left != null){
cell_list.add(top_left);
}
Cell top_center = m.getCell(x, y - 1);
if(top_center != null){
cell_list.add(top_center);
}
Cell top_right = m.getCell(x + 1, y - 1);
if(top_right != null){
cell_list.add(top_right);
}
Cell center_left = m.getCell(x - 1, y);
if(center_left != null){
cell_list.add(center_left);
}
Cell center_right = m.getCell(x + 1, y);
if(center_right != null){
cell_list.add(center_right);
}
Cell bottom_left = m.getCell(x - 1, y + 1);
if(bottom_left != null){
cell_list.add(bottom_left);
}
Cell bottom_center = m.getCell(x, y + 1);
if(bottom_center != null){
cell_list.add(bottom_center);
}
Cell bottom_right = m.getCell(x + 1, y + 1);
if(bottom_right != null){
cell_list.add(bottom_right);
}
return cell_list;
}
public TerrainType getTerrainType() {return terrainType;}
public Icon getTerrainDIcon() {return new ImageIcon(terrIcons[1]);}
public Collection<Entity> getOccupyingEntities() { return occupyingEntities;}
public GameMap getGameMap(){ return containingMap;}
public ResourceItem getResourceItem(){return occupyingResource;}
public boolean addOccupyingEntity(Entity toAdd) {
occupyingEntities.add(toAdd);
return true; //May want to change later to ensure no more than one building/unit can be in the same cell.
}
public void removeEntity(Entity toRemove){
occupyingEntities.remove(toRemove);
}
public Entity getEnemy(Player p){
if(occupyingEntities.isEmpty()){
return null;
}
if(occupyingEntities.iterator().next().getOwner().equals(p)){
return null;
}
return occupyingEntities.iterator().next();
}
public static int distance(Cell a, Cell b){
//Returns distance between two given cells. Diagonals count as 1
if (a==null || b== null) {
System.out.println("The two cells are "+a+" "+b);
}
int xDif = a.getX()-b.getX();
int yDif = a.getY()-b.getY();
return Math.max(Math.abs(xDif), Math.abs(yDif));
}
public int getX() {return x;}
public int getY() {return y;}
}