-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyWorld.java
More file actions
120 lines (106 loc) · 3.98 KB
/
Copy pathMyWorld.java
File metadata and controls
120 lines (106 loc) · 3.98 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (giant and adiani)
* @version (0.1 alpha)
*/
public class MyWorld extends World
{
private int frame = 0;
public int randomSpawnPosition;
public static Guardian guardian;
public static Portal portal;
public static int score;
public static GreenfootSound backgroundMusic = new GreenfootSound("backround_music.mp3");
private Counter healthCounter = new Counter("Health : ");
private Counter scoreCounter = new Counter("Score : ");
private Counter maxEnemyEntryCounter = new Counter("Max Enemy Entry : ");
public MyWorld()
{
super(600, 600, 1);
backgroundMusic.playLoop();
prepare();
}
private void prepare()
{
guardian = new Guardian();
portal = new Portal();
addObject(guardian, 300, 350);
addObject(portal, getWidth()/2, getHeight()/2);
wallSetup();
addObject(healthCounter,65,573);
addObject(scoreCounter,65,24);
addObject(maxEnemyEntryCounter,480,24);
score = 0;
}
public void act()
{
healthCounter.setValue(neverBeNegative(guardian.health));
scoreCounter.setValue(neverBeNegative(score));
maxEnemyEntryCounter.setValue(neverBeNegative(portal.maxEnemyEntry));
guardian.getImage().scale(22, 22);
spawnEnemy();
}
private void spawnEnemy()
{
if(frame == 0){
randomSpawnPosition = Greenfoot.getRandomNumber(4);
switch(randomSpawnPosition)
{
case 0 : addObject(new FireHead(), 0, getHeight()/2);break;
case 1 : addObject(new FireHead(), getWidth(), getHeight()/2);break;
case 2 : addObject(new FireHead(), getWidth()/2, 0);break;
case 3 : addObject(new FireHead(), getWidth()/2, getHeight());break;
}
}else if(frame == 90){
randomSpawnPosition = Greenfoot.getRandomNumber(4);
switch(randomSpawnPosition)
{
case 0 : addObject(new Skeleton(), 0, getHeight()/2);break;
case 1 : addObject(new Skeleton(), getWidth(), getHeight()/2);break;
case 2 : addObject(new Skeleton(), getWidth()/2, 0);break;
case 3 : addObject(new Skeleton(), getWidth()/2, getHeight());break;
}
}else if(frame == 150){
randomSpawnPosition = Greenfoot.getRandomNumber(4);
int plusPixelSpawn = 45;
switch(randomSpawnPosition)
{
case 0 : addObject(new Vampire(), plusPixelSpawn, plusPixelSpawn); break;
case 1 : addObject(new Vampire(), getWidth()-plusPixelSpawn, plusPixelSpawn);break;
case 2 : addObject(new Vampire(), plusPixelSpawn, getHeight()-plusPixelSpawn); break;
case 3 : addObject(new Vampire(), getWidth()-plusPixelSpawn, getHeight()-plusPixelSpawn);break;
}
frame=-1;
}
frame++;
}
private void wallSetup()
{
//
for (int i = 10; i < getHeight()/2-40; i+=20){
addObject(new Wall(), 10, i);
addObject(new Wall(), getWidth()-10, i);
}
for (int i = getHeight()/2+40-10; i < getHeight() ; i+=20){
addObject(new Wall(), 10, i);
addObject(new Wall(), getWidth()-10, i);
}
for (int i = 40; i < getWidth()/2-30; i+=30){
addObject(new Wall(), i, 10);
addObject(new Wall(), i, getHeight()-10);
}
for (int i = getWidth()-40; i > getWidth()/2+30; i-=30){
addObject(new Wall(), i, 10);
addObject(new Wall(), i, getHeight()-10);
}
}
private int neverBeNegative(int val)
{
if(val < 0) {
val = 0;
}
return val;
}
}