-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.java
More file actions
162 lines (140 loc) · 4.31 KB
/
Block.java
File metadata and controls
162 lines (140 loc) · 4.31 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
160
161
162
/**
* @author Bryan Xu, Brian Wu
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Block extends JComponent
{
public Rectangle body;
public int bodyWidth;
public int bodyHeight;
public int frameWidth;
public int frameHeight;
//player1 is top, player2 is bottom
public int player;
public int speed = 7;
public int angle;
public int bulletSpeed = 6;
public ArrayList<Bullet> bullets;
public int MAX_CAPACITY = 300;
public double currentCapacity = MAX_CAPACITY;
public Rectangle block ;
public int MAX_AMMO = 100000;
public int cleanness = MAX_AMMO;
public Rectangle currentAmmo;
public boolean isABot;
public boolean botCanShoot;
public boolean shot = false;
public boolean shot2 = false;
public Block(int frameWidth, int frameHeight, int bodyWidth, int bodyHeight, int player){
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.bodyWidth = bodyWidth;
this.bodyHeight = bodyHeight;
this.player = player;
bullets = new ArrayList<Bullet>();
int space = frameHeight/4;
if(player==1){
space = -space;
}
body = new Rectangle(frameWidth/2,frameHeight/2+space,bodyWidth,bodyHeight);
block = new Rectangle((int)body.getX(),(int)(body.getY()+bodyHeight-(currentCapacity*bodyHeight/MAX_CAPACITY)),(int)bodyWidth,(int)(currentCapacity*bodyHeight/MAX_CAPACITY));
if(player==1){
currentAmmo = new Rectangle(0,0,(int)(cleanness*frameWidth/MAX_AMMO),50);
angle = 90;
}
if(player==2){
currentAmmo = new Rectangle(0,frameHeight-50,(int)(cleanness*frameWidth/MAX_AMMO),50);
angle = 270;
}
isABot = false;
botCanShoot = true;
}
public void moveUp(){
if(player==1){
if(body.getY()>=0){
body.translate(0,-speed);
}
}
if(player==2){
if(body.getY()>=0+frameHeight/2){
body.translate(0,-speed);
}
}
repaint();
}
public void moveDown(){
if(player==1){
if(body.getY()+bodyHeight<=frameHeight/2){
body.translate(0,speed);
}
}
if(player==2){
if(body.getY()+bodyHeight<=0+frameHeight){
body.translate(0,speed);
}
}
repaint();
}
public void moveLeft(){
if(body.getX()>=0){
body.translate(-speed,0);
}
repaint();
}
public void moveRight(){
if(body.getX()+bodyWidth<=frameWidth){
body.translate(speed,0);
}
repaint();
}
public void shoot(){
int bulletSize = 4;
if(currentCapacity>0){
Bullet bul = new Bullet(body.x+bodyWidth/2,body.y+bodyHeight/2,bulletSize,3*bulletSize, angle, bulletSpeed);
bullets.add(bul);
bul.start();
currentCapacity -= 1;
}
}
public void replenish(double num){
currentCapacity += num;
}
public void shootBullets(){
for(int i = 0; i<bullets.size(); i++){
if(player==1){
bullets.get(i).move();
}else{
bullets.get(i).move();
}
}
repaint();
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g ;
if(player==1){
g2.setColor(Color.orange);
}else{
g2.setColor(Color.green);
}
g2.fill(body);
g2.setColor(Color.yellow);
for(Bullet b:bullets){
g2.fill(b.body);
}
}
public void update(){
block = new Rectangle((int)body.getX(),(int)(body.getY()+bodyHeight-(currentCapacity*bodyHeight/MAX_CAPACITY*9/10)),(int)bodyWidth,(int)(currentCapacity*bodyHeight/MAX_CAPACITY*9/10));
if(player==1){
currentAmmo = new Rectangle(0,0,(int)(cleanness*frameWidth/MAX_AMMO),50);
}
if(player==2){
currentAmmo = new Rectangle(0,frameHeight-60,(int)(cleanness*frameWidth/MAX_AMMO),50);
}
}
public void changeAngleBy(int ang){
angle += ang;
}
}