-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrownTurret.java
155 lines (142 loc) · 5.91 KB
/
BrownTurret.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
import greenfoot.*;
/**
* <p><b>File name: </b> BrownTurret.java
* @version 1.6
* @since 07.06.2018
* <p><b>Last modification date: </b> 29.09.2018
* @author Alexandru F. Dascalu
* <p><b>Copyright (C)</b> 2018 Alexandru F. Dascalu
*
* <p>BrownTurret.java is part of Panzer Batallion.
* Panzer Batallion is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <p>You should have received a copy of the GNU General Public License v3
* along with this program. If not, see <a href="https://www.gnu.org/licenses/">https://www.gnu.org/licenses/</a> .
*
* <p>A summary of the license can be found here:
* <a href="https://choosealicense.com/licenses/gpl-3.0/">https://choosealicense.com/licenses/gpl-3.0/</a> .
*
* <p><b>Purpose: </b>
* <p> This class models a brown turret for a Greenfoot recreation of
* the Wii Tanks game for the Nintendo Wii. It is the most basic enemy
* turret. It randomly turns around, looking for the player.It's line of
* sight bounces off walls as many times as the shells it fires do.
* If it detects the player, it fires a shell, unless it already has
* 2 shells fired by it still in the world. It also has a cooldown period,
* after which it will not fire another shell.
*
* <p><b>Version History</b>
* <p> -1.0 - Created the class.
* <p> -1.1 - Made the random turning be more varied.
* <p> -1.2 - Made the turret detect the enemy player if there is a straight
* line to it.
* <p> -1.3 - Made the turret's line of sight bounce off walls as many times
* as the shells it fires do.
* <p> -1.4 - Made the aiming of the turret more accurate.
* <p> -1.5 - Moved code for firing and detecting targets to a new class called
* EnemyTurret that is the supertype of all enemy tank turrets and added methods
* to allow different enemy turrets to have different stats.
* <p> -1.6 - Added some static variables and getters for the stats of a brown
* turret to work with the standard code from the enemy turret class.
*/
public class BrownTurret extends EnemyTurret
{
/**The cooldown period in miliseconds after firing which the turret will not
* fire again. Its value is {@value}.*/
private static final int FIRE_COOLDOWN=1400;
/**The maximum number of shells that the turret has fired that can be in the
* world at the same time. Its value is {@value}.*/
private static final int LIVE_SHELLS_ALLOWED=2;
/**
* Makes a new Brown Turret on the tank given as an argument.
* @param tank The tank on which this Turret will be placed.
*/
public BrownTurret(Tank tank)
{
//just call the super type constructor
super(tank);
}
/**Calculates how the turret should turn next. It modifies the nextRotation,
* nextTurn and finishTurn attributes. Makes the turret move randomly.*/
@Override
protected void calculateNextRotation()
{
/*We generate a new angle. We would like this turret to have varried
* movements, but then if the generated angle is big it would turn
* around too much. Therefore, we generate a random number (turnChance),
* based on which we will decide the upper limit of the degrees the
* turret will turn.*/
int turnChance=Greenfoot.getRandomNumber(100);
int upperLimit;
/*Based on the value of turnChance, we decide the upperLimit in degree
* of the next turn. This is because we would like the turret to have varied
* moves, so it will mostly make small turns, but sometimes it might
* also make huge turns.*/
if(turnChance<60)
{
/*There is a 60% chance that the upperLimit is 160.*/
upperLimit=160;
}
/*Otherwise, the upper limit is 720.*/
else
{
upperLimit=720;
}
/*We would like the turret to be able to turn in either direction,
* so we subtract from the random number half of the upper limit. This
* means we will get a number either from -80 to 80, exclusive, or from
* -360 to 360, exclusive.*/
int nextTurn = Greenfoot.getRandomNumber(upperLimit)-(upperLimit/2);
/*We need to know when to stop, so we calculate the rotation of the
* turret after it will have turned nextTurn degrees. In Greenfoot
* the rotation of an object is from 0 to 359, so we use MOD 360
* to ensure that, after we add nextTurn to the current rotation
* of the turret.*/
nextRotation=(getRotation()+nextTurn)%360;
/*nextTurn may be negative and thus nextRotation could also be.
* By adding 360 we get the equivalent positive angle.*/
if(nextRotation<0)
{
nextRotation=360+nextRotation;
}
}
/**Gets the cool down period(in milliseconds) after which this turret can
* fire another shell. This period is a static variable and is the same for
* all objects of this class.
* @return The period in milliseconds after which this turret can fire another
* shell.*/
@Override
public int getFireCooldown()
{
return FIRE_COOLDOWN;
}
/**Gets the limit of how many shells fired by this turret can be in the world
* at the same time. This number is a static variable and is the same for
* all objects of this class.
* @return the limit of how many shells fired by this turret can be in the world
* at the same time. */
@Override
public int getLiveShellLimit()
{
return LIVE_SHELLS_ALLOWED;
}
/**
* Gets the size in degrees of the angle of an imaginary cone whose axis extends
* to the position of the player tank. In this angle the turret moves
* randomly. Returns 0 unless overriden, since there is no set behaviour for
* a basic enemy turret.
* @return 0, since this turret aims randomly and does not follow the player.
*/
public int getAimAngle()
{
return 0;
}
}