-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTack.java
More file actions
159 lines (146 loc) · 5.26 KB
/
Tack.java
File metadata and controls
159 lines (146 loc) · 5.26 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
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.*;
import com.googlecode.lanterna.terminal.Terminal.SGR;
import com.googlecode.lanterna.TerminalFacade;
import com.googlecode.lanterna.input.Key;
import com.googlecode.lanterna.input.Key.Kind;
import com.googlecode.lanterna.terminal.Terminal;
import com.googlecode.lanterna.terminal.Terminal.Color;
import com.googlecode.lanterna.terminal.TerminalSize;
import com.googlecode.lanterna.LanternaException;
import com.googlecode.lanterna.input.CharacterPattern;
import com.googlecode.lanterna.input.InputDecoder;
import com.googlecode.lanterna.input.InputProvider;
import com.googlecode.lanterna.input.Key;
import com.googlecode.lanterna.input.KeyMappingProfile;
import com.googlecode.lanterna.screen.*;
public class Tack{
private int x, y; //x and y coordinates
private int direction; //direction of tack movement
private int steps; //number of steps the tack has taken
private long sinceTime; //time needed for the next tack movement
private int delay; //time in between tack movements
private int hits; //how many lives a tack can take from a balloon with one hit
/**A constructor of a tack owned by a tackShooter
*@param int xCord is the x-coordinate
*@param int yCord is the y-coordinate
*@param int direx is the direction the tack will move
0 is up
1 is right
2 is down
3 is left
*@param int delay is the delay time between the tack's movements
*@param int hits is how many lives a tack can take from a balloon with one hit
*/
public Tack(int xCord, int yCord, int direx, int delay, int hits){
x = xCord;
y = yCord;
direction = direx;
this.delay = delay;
sinceTime = 0;
steps = 0;
this.hits = hits;
}
/**A method that will make the tack move according to its direction
*@param long timer is how long the game has been going on for
*/
public void move(long timer){
sinceTime = timer; //time is updated
if(direction == 0){
y -= 1;
steps++;
}
if(direction == 1){
x += 1;
steps++;
}
if(direction == 2){
y += 1;
steps++;
}
if(direction == 3){
x -= 1;
steps++;
}
sinceTime += delay; //updates the sinceTime to relect new time the game needs to reach before the next tack movement
if ((x == 1 || x == 60) && (y >= 3 && y <= 33)) steps = 4; //if the tack reaches the radius of its movement, the number of steps becomes 4 and it will die
if ((y == 3 || y == 33) && (x >= 1 && x <= 60)) steps = 4;
}
/**A method that checks if the tack has hit any balloons based on coordinates
*@param List<Balloon> balloons
*@return int earned is the amount of money earned by the user for hitting a balloon
*/
public int hitTarget(List<Balloon> balloons){
int earned = 0;
for (int i = balloons.size()-1; i >= 0; i--){
Balloon temp = balloons.get(i);
if (temp.getX() == x && temp.getY() == y){ //if the balloon and tack are at the same coordinates
temp.setLives(temp.getLives() - hits); //takes lives from the balloon
if (hits == 2 && temp.getLives() >= 0) earned += 10;
else if (hits == 2 && temp.getLives() < 0) earned += 5;
else if (hits == 1) earned += 5; //the user earns $5 for every hit or life the tack takes from the balloon
if (temp.getLives() <= 0) balloons.remove(i); //if the balloon has no more lives, it is removed
}
}
return earned; //the amount of money earned for taking lives from a balloon
}
/**A method that draws the tack onto the screen according to its direction
*@param Screen s
*/
public void draw(Screen s){
if (direction == 0) s.putString(x,y,"^",Terminal.Color.BLUE,Terminal.Color.CYAN);
if (direction == 1) s.putString(x,y,">",Terminal.Color.BLUE,Terminal.Color.CYAN);
if (direction == 2) s.putString(x,y,"v",Terminal.Color.BLUE,Terminal.Color.CYAN);
if (direction == 3) s.putString(x,y,"<",Terminal.Color.BLUE,Terminal.Color.CYAN);
}
/**A method that undraws the tack if it is on a road tile
*@param Screen s
*@param int xcor of the tack
*@param int ycor of the tack
*@param List<Tile> road
*/
public void undraw(Screen s, int xcor, int ycor, List<Tile> road){
for (Tile x: road){
if (xcor == x.getX() && ycor == x.getY()) s.putString(xcor,ycor," ",Terminal.Color.DEFAULT,Terminal.Color.WHITE); //if its on the road, change the background color to white
else s.putString(xcor,ycor," ",Terminal.Color.DEFAULT,Terminal.Color.GREEN);
}
}
/**A method to get the tack's x-coordinate
*@return int x
*/
public int getX(){
return x;
}
/**A method to get the tack's y-coordinate
*@return int y
*/
public int getY(){
return y;
}
/**A method to get the nmber of steps the tack has taken
*@return int steps
*/
public int getSteps(){
return steps;
}
/**A method to get the next time the game needs to reach before the tack can move again
*@return long sinceTime
*/
public long getSince(){
return sinceTime;
}
/**A method to get a tack's direction
*@return int 0 is up
1 is right
2 is down
3 is left
*/
public int getDirection(){
return direction;
}
/**A method to set the steps a tack has taken
*@param int num
*/
public void setSteps(int num){
steps = num;
}
}