-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelLogic.java
163 lines (163 loc) · 5.2 KB
/
LevelLogic.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LevelLogic{
public LevelLogic(){
frame = new JFrame();
scene = new Scene();
player = new Tank(100,300,40,60,Color.GREEN.darker());
computer = new BadTank(500,300,40,60,Color.RED);
scene.addObject(player);
scene.addObject(computer);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_WIDTH);
frame.setVisible(true);
}
public void update(double[] xyStick,double[] zrzStick,boolean[] buttons){
if(state == 0 && buttons[9]){
state = 1;
createLevel();
}else if(state == 1){
updateLevel(xyStick,zrzStick,buttons);
}else if(state == 2 && buttons[9]){
state = 1;
createLevel();
}else if(state == 3 && buttons[9]){
state = 1;
createLevel();
}
}
public void updateLevel(double[] xyStick,double[] zrzStick,boolean[] buttons){
if(buttons[5] && !trigger){
trigger = true;
player.fireBullet();
}else if(!buttons[5]){trigger = false;}
if(player.contains(computer.getBullets())){
state = 3;
createLoseScreen();
}
if(computer.contains(player.getBullets())){
state = 2;
level++;
createWinScreen();
}
player.setMagAngle((int)((xyStick[1] + zrzStick[1])*-3),-1*(xyStick[1] - zrzStick[1])*0.05);
moveDelay++;
if(moveDelay > 1){
moveDelay = 0;
double xDiff = player.getXY().getX()-computer.getXY().getX();
double yDiff = player.getXY().getY()-computer.getXY().getY();
angleRotation = Math.abs(oldAngleRotation)-Math.abs((double)Math.round(Math.atan(yDiff/xDiff)*1000)/1000);
if(computer.getXY().getY()<player.getXY().getY() &&
computer.getXY().getX()>player.getXY().getX()){angleRotation*=-1;}
if(computer.getXY().getY()>player.getXY().getY() &&
computer.getXY().getX()<player.getXY().getX()){angleRotation*=-1;}
computer.setMagAngle(computer.getSpeed(),angleRotation);
oldAngleRotation = Math.abs((double)Math.round(Math.atan(yDiff/xDiff)*1000)/1000);
shotTimer++;
if(computer.checkRadius(player.getXY()) && shotTimer > computer.getShotDelay()){
computer.fireBullet();
shotTimer = 0;
}else if(!computer.checkRadius(player.getXY())){
computer.translate();
}
}
player.translate();
int correction = 5;
if(player.getXY().getY()<20){
player.translateRect(0,correction);
}else if(player.getXY().getY()>FRAME_HEIGHT-20){
player.translateRect(0,-correction);
}else if(player.getXY().getX()<20){
player.translateRect(correction,0);
}else if(player.getXY().getX()>FRAME_WIDTH-20){
player.translateRect(-correction,0);
}else{}
scene.repaint();
}
public void createLevel(){
shotTimer = 0;
moveDelay = 0;
angleRotation = 0;
oldAngleRotation = 0;
scene.resetScene();
frame.getContentPane().removeAll();
frame.setVisible(true);
player = new Tank(100,200,40,60,Color.GREEN.darker());
computer = new BadTank(300,400,40,60,Color.RED);
int radiusDifficulty = 0;
if(80+level*10 >= 200){radiusDifficulty = 200;}
else{radiusDifficulty = 80+level*10;}
int shotDelayDifficulty = 0;
if(50-level*2 <= 10){shotDelayDifficulty = 10;}
else{shotDelayDifficulty = 50-level*2;}
computer.setRadius(radiusDifficulty);
computer.setSpeed(level+1);
computer.setShotDelay(shotDelayDifficulty);
computer.translatePolar(0,Math.PI/2);
scene.addObject(player);
scene.addObject(computer);
frame.add(scene);
frame.revalidate();
frame.repaint();
}
public void createWinScreen(){
frame.getContentPane().removeAll();
frame.setVisible(true);
contentPanel = new JPanel();
JLabel winlabel = createLabel("You Win!",90);
JLabel nextLabel = createLabel("Press Start for Level #"+level,40);
contentPanel.add(winlabel);
contentPanel.add(nextLabel);
frame.add(contentPanel);
frame.revalidate();
frame.repaint();
}
public void createLoseScreen(){
level = 1;
frame.getContentPane().removeAll();
frame.setVisible(true);
contentPanel = new JPanel();
JLabel loselabel = createLabel("You Lose.",90);
JLabel startLabel = createLabel("Press Start for Level 1",40);
contentPanel.add(loselabel);
contentPanel.add(startLabel);
frame.add(contentPanel);
frame.revalidate();
frame.repaint();
}
public void createNewGameScreen(){
state = 0;
level = 1;
frame.getContentPane().removeAll();
frame.setVisible(true);
contentPanel = new JPanel();
JLabel newLabel = createLabel("New Game.",90);
JLabel startLabel = createLabel("Press Start for Level 1",40);
contentPanel.add(newLabel);
contentPanel.add(startLabel);
frame.add(contentPanel);
frame.revalidate();
frame.repaint();
}
public static JLabel createLabel(String value,int fontSize){
JLabel label = new JLabel(value);
label.setFont(new Font("Helvetica",Font.PLAIN,fontSize));
return label;
}
private static final int FRAME_WIDTH = 750;
private static final int FRAME_HEIGHT = 750;
private static int state = 0;
private static int level = 1;
private static int shotTimer = 0;
private static int moveDelay = 0;
private static double angleRotation;
private static double oldAngleRotation;
private static boolean trigger = false;
private static JFrame frame;
private static JPanel contentPanel;
private static Scene scene;
private static Tank player;
private static BadTank computer;
private static Bullet bullet;
}