-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
147 lines (116 loc) · 4.94 KB
/
Copy pathWorld.java
File metadata and controls
147 lines (116 loc) · 4.94 KB
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
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.util.Random;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class World {//================================================================================================================================================
// Variables ================================================================================================================================================
public Frog frog;
int height;
int width;
ObjectHolder<Segment> segments;
String biome;
ArrayList<Projectile> projectiles;
ArrayList<Enemy> enemies;
public static boolean enemiesAllowed;
ObjectHolder<Life> lives;
//================================================================================================================================================
// Constructor ================================================================================================
public World(int initWidth, int initHeight){//This is kind of like a level
width = initWidth;
height = initHeight;
frog = new Frog();
segments = new ObjectHolder<Segment>();//Create the segment datastructure
for (int i=0; i<15; i++){//Fill the data structure
if (i==0 || i==7 || i==14)//Add safeAreas
segments.append(new SafeArea(Frogger.HEIGHT-(50+i*50)));
else segments.append (new Road(Frogger.HEIGHT-(50+i*50)));//Add the roads
}
lives = new ObjectHolder<Life>();//These 3 lines are used for adding lives. For now, they're disabled, but we're leaving this in just in case somebody wants them later
/*for (int i=0; i<2; i++)
lives.append(new Life());*/
biome = createBiome();
projectiles = new ArrayList<Projectile>();//Create projectiles data structure and enemies on the line below.
enemies = new ArrayList<Enemy>();
if (enemiesAllowed){//Create three enemies if they havn't been disabled
for (int i=0; i<3; i++)
enemies.add(new Enemy());
}
} //Constructor ================================================================================================
// Methods =============================================================================================================================================================================
public String createBiome(){
Random rand = new Random();
int numBiomes=4;
String[] biomes = new String []{"Basic", "Tundra", "Marsh", "Desert"};
return biomes[rand.nextInt(numBiomes)];
}//createBiome
public void updateSegments(){
segments.update();
}//updateSegments
public void updateProjectiles(){
for(Projectile p: projectiles){
p.update();
}
}//updateProjectiles
public boolean updateEnemies(){//UPDATES AND ALSO RETURNS BOOLEAN VALUE OF WHETHER THEY CAUGHT THE FROG
for (Enemy e:enemies){
e.update(frog.getX(), frog.getY());
}
for (Enemy e:enemies){
if (
frog.getX()+50>e.x
&& frog.getX()<e.x+e.size
&&frog.getY()+50>e.y
&& frog.getY()<e.y+e.size
&& !e.shot//If the enemy is dead, it can't eat a frog
)
return true;
}
return false;
}//updateEnemies
public void checkEnemiesShot(){//This checks to see if the projectiles have entered the hurt box for the enemies. If so, it sets the enemies to be dead and also created a new enemy for each one killed
int numDead=0;
try{
for (Enemy e : enemies){
for (Projectile p : projectiles){
if ( // If the projectiles hit the bird
p.x+p.size >= e.x-e.size
&& p.x-p.size<=e.x+e.size*2
&& p.y+p.size >= e.y-e.size
&& p.y-p.size<=e.y+e.size*2
)
{
e.health-=p.power;
if (e.health==0){
e.shot=true;
numDead++;
}
}
}
}
}
catch (Exception e){ //This is our bandaid for a weird nullPointerException we recieved ONCE, and neither we or Prof. Alfeld could figure out why
System.out.println("That was really weird");
System.out.println(e);
}
Random rand= new Random();
for (int i=0; i<numDead; i++){
enemies.add(new Enemy(rand.nextInt(Frogger.WIDTH), rand.nextInt(50)));
}
}//checkEnemiesShot
public void addMoreEnemies(){//This is activated from Frogger with a key command
for (int i=0; i<3; i++)
enemies.add(new Enemy());
}//addMoreEnemies
public void drawSegments(Graphics g, String biome){
segments.draw(g, biome);
}//drawSegments
public void drawLives (Graphics g, String s){
lives.draw(g, s);
}//drawLives
//================================================================================================================================================================================================
}//World==============================================================================================================================================================================================