-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathController.java
170 lines (147 loc) · 5.13 KB
/
Controller.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
/*
* This is the Controller class for Test_Game.
* Controller is created by Test_Game, and it gets passed in the World object (the model).
* @author gliebchen
* @version 1.0
* @since 2016-03-16
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Controller {
private Player player;
private World worldy;
private List<Evil>evils=new ArrayList<Evil>();
private List<Bullet>bullets=new ArrayList<Bullet>();
private int score;
private int lives;
private int maxRow = 0;
private int maxCol = 0;
private int delay = 0; // if i don't put in the delay it will delete the evil as soon as it creates it
public Controller(World w){
worldy = w;
player = new Player();
score = 0;
lives = 4;
maxRow = worldy.getMaxRow();
maxCol = worldy.getMaxCol();
int[] locationAndAppearanceOfPlayer = {player.getLocation()[0],player.getLocation()[1],player.getAppearance()};
worldy.putInWorld(locationAndAppearanceOfPlayer);
}
public void userInput(char direction){
int[] location = player.suggestMove(direction);
int[] locationAndAppearanceBefore = {player.getLocation()[0],player.getLocation()[1],(int)' '};
int[] locationAndAppearanceAfter = {location[0],location[1],player.getAppearance()};
if(worldy.isFree(location)){
player.setLocation(location);
worldy.putInWorld(locationAndAppearanceBefore);
worldy.putInWorld(locationAndAppearanceAfter);
}
}
public void shoot(){
int[]location = player.getLocation();
location[1] = location[1]+1; //bullets are created in front of the player
if(worldy.isFree(location)){
Bullet newB = new Bullet(location[0], location[1]);
int[] newETempLocAndAppearance ={newB.getLocation()[0],newB.getLocation()[1],(int)newB.getAppearance()};
worldy.putInWorld(newETempLocAndAppearance);
//add to list
bullets.add(newB);
}
}
public void moveEverything(){
delay=delay+1;
//spawn evils
if(delay%10==0){
Evil newE = new Evil(randInt(2,maxRow-1), maxCol-1);
int[] newETempLocAndAppearance ={newE.getLocation()[0],newE.getLocation()[1],(int)newE.getAppearance()};
worldy.putInWorld(newETempLocAndAppearance);
//add to list
evils.add(newE);
delay=0;
}
//move evils
moveEntities();
//get rid of the dead
cleanTheDead();
}
private void moveEntities(){
int listLooper = 0;
//check each entity's position
for(listLooper=0; listLooper<evils.size(); listLooper++){
Evil e = evils.get(listLooper);
int[] eCurrentLoc = e.getLocation();
int[] eSuggestedLoc = e.suggestMove();
if(worldy.isFree(eSuggestedLoc)){
int[] eTempLocAndAppearance ={eCurrentLoc[0],eCurrentLoc[1],(int)' '};
worldy.putInWorld(eTempLocAndAppearance);
e.setLocation(eSuggestedLoc);
eTempLocAndAppearance[0]=eSuggestedLoc[0];
eTempLocAndAppearance[1]=eSuggestedLoc[1];
eTempLocAndAppearance[2]=(int)e.getAppearance();
//System.out.println(eTempLocAndAppearance[0]+" "+eTempLocAndAppearance[1]+" "+eTempLocAndAppearance[2]+" ");
worldy.putInWorld(eTempLocAndAppearance);
}else{
//here is the collision happening
if(worldy.getWhatsInLocation(eSuggestedLoc)==player.getAppearance()){
lives=lives-1;
}else if(worldy.getWhatsInLocation(eSuggestedLoc)=='.'){
score=score+1;
System.out.println(score);
}
//and tidy up after collision
int[] eTempLocAndAppearance ={eCurrentLoc[0],eCurrentLoc[1],(int)' '};
worldy.putInWorld(eTempLocAndAppearance);
e.setDead(true);
}
}
for(listLooper=0; listLooper<bullets.size(); listLooper++){
Bullet b = bullets.get(listLooper);
int[] bCurrentLoc = b.getLocation();
int[] bSuggestedLoc = b.suggestMove();
if(worldy.isFree(bSuggestedLoc)){
int[] bTempLocAndAppearance ={bCurrentLoc[0],bCurrentLoc[1],(int)' '};
worldy.putInWorld(bTempLocAndAppearance);
b.setLocation(bSuggestedLoc);
bTempLocAndAppearance[0]=bSuggestedLoc[0];
bTempLocAndAppearance[1]=bSuggestedLoc[1];
bTempLocAndAppearance[2]=(int)b.getAppearance();
//System.out.println(eTempLocAndAppearance[0]+" "+eTempLocAndAppearance[1]+" "+eTempLocAndAppearance[2]+" ");
worldy.putInWorld(bTempLocAndAppearance);
}else{
//and tidy up after the collision
int[] bTempLocAndAppearance ={bCurrentLoc[0],bCurrentLoc[1],(int)' '};
worldy.putInWorld(bTempLocAndAppearance);
b.setDead(true);
}
}
}
private void cleanTheDead(){
int listLooper = 0;
//check each entity's position
for(listLooper=evils.size()-1; listLooper>-1; listLooper--){
Evil e = evils.get(listLooper);
if(e.isDead()){
evils.remove(listLooper);
}
}
for(listLooper=bullets.size()-1; listLooper>-1; listLooper--){
Bullet b = bullets.get(listLooper);
if(b.isDead()){
bullets.remove(listLooper);
}
}
}
private int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public int[] getScoreAndLives(){
int[] scoreAndLives = {score,lives};
return scoreAndLives;
}
}