-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGun.java
More file actions
74 lines (66 loc) · 2.04 KB
/
Gun.java
File metadata and controls
74 lines (66 loc) · 2.04 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
/*
* Gun class that handles any gun functions
*/
public class Gun {
public GunModel model;
private Timer gunCooldown;
private int ammo;
private int preReloadAmmo;
private boolean active;
Gun(String type, int ammo) {
this.model = GunModel.valueOf(type);
this.gunCooldown = new Timer();
this.ammo = ammo;
this.preReloadAmmo = this.ammo;
this.active = false;
}
public boolean ready(){ // Check if the gun is ready to be used
boolean ready = gunCooldown.finished();
if(ready) this.preReloadAmmo = this.ammo;
return ready;
}
public int[] fire(){ // Return if fired and the degree offset
int[] bullet = new int[2];
if(this.ammo > 0 && this.ready()) {
this.ammo--;
this.preReloadAmmo--;
gunCooldown.setTimerLength(1/(model.getFireRate()));
gunCooldown.start();
bullet[0] = 1;
bullet[1] = (int)((Math.random()*(this.model.getFireError()+1)) - (this.model.getFireError()/2));
}
return bullet;
}
public double reload(){
if(this.ammo < model.getMaxAmmo() && this.ready()) {
this.ammo = model.getMaxAmmo();
gunCooldown.setTimerLength(model.getReloadSpeed());
gunCooldown.start();
return (model.getReloadSpeed());
}
return 0;
}
public double takeOut(){ // Start a timer that represents the take-out time
gunCooldown.setTimerLength(1/(model.getFireRate()/3));
gunCooldown.start();
return (model.getFireRate()/3);
}
public String getModel(){
return this.model.toString();
}
public int getAmmo() {
return this.ammo;
}
public void setAmmo(int ammo) {
this.ammo = ammo;
}
public boolean isActive() {
return this.active;
}
public void setActive(boolean active) {
this.active = active;
if(!this.active && !this.ready()){
this.ammo = this.preReloadAmmo;
}
}
}