-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeapon.java
More file actions
135 lines (114 loc) · 3.37 KB
/
Copy pathWeapon.java
File metadata and controls
135 lines (114 loc) · 3.37 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
package puzzle2;
import java.util.*;
import java.lang.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
abstract class Weapon implements Drawable{
private WeaponHolder weaponHolder;
private BufferedImage weaponIcon;
private BufferedImage bulletTexture;
private Scene.Location pickupLocation;
private Scene scene;
private boolean showAsPickup;
private int projectileSpeed;
private int ammo;
private int tileSize;
public Weapon(Scene scene,Scene.Location pickupLocation,boolean showAsPickup){
this.scene = scene;
this.pickupLocation = pickupLocation;
this.showAsPickup = showAsPickup;
this.tileSize = scene.getTileSize();
}
public void setBulletProperties(BufferedImage bulletTexture,int bulletSpeed){
this.bulletTexture = bulletTexture;
this.projectileSpeed = bulletSpeed;
}
public void setWeaponProperties(BufferedImage weaponIcon,int ammo){
this.weaponIcon = weaponIcon;
this.ammo = ammo;
}
public void setWeaponHolder(WeaponHolder weaponHolder){
this.weaponHolder = weaponHolder;
}
public void addAmmo(int extra){
ammo += extra;
}
public int getAmmoLeft(){
return ammo;
}
public void showWeaponOnGrid(boolean show){
showAsPickup = show;
}
public void draw(Graphics g){
if(showAsPickup){
g.drawImage(weaponIcon,tileSize*pickupLocation.x,tileSize*pickupLocation.y,tileSize,tileSize,null);
}
}
public void update(){
}
public Type getType(){
return Drawable.Type.WEAPON;
}
public void fire(){
if(ammo <= 0)
return;
scene.addWeaponFire(new Ammunition(weaponHolder.getGridPosition(),weaponHolder.getFacingDirection(),bulletTexture));
if(--ammo == 0)
weaponHolder.notifyAmmoFinished();
}
public class Ammunition implements Drawable{
public Scene.Location pixelPosition;
public Direction dir;
public BufferedImage bulletTexture;
public Ammunition(Scene.Location gridPosition,Direction dir,BufferedImage bulletTexture){
this.pixelPosition = new Scene.Location(gridPosition.x*tileSize,
gridPosition.y*tileSize);
this.dir = dir;
this.bulletTexture = bulletTexture;
if(dir == Direction.LEFT)
pixelPosition.x -= tileSize;
else if(dir == Direction.RIGHT)
pixelPosition.x += tileSize;
else if(dir == Direction.UP)
pixelPosition.y -= tileSize;
else
pixelPosition.y += tileSize;
}
/** default moves bullet in a straight path
*/
public void update(){
switch(dir){
case LEFT:
pixelPosition.x -= projectileSpeed;
break;
case RIGHT:
pixelPosition.x += projectileSpeed;
break;
case UP:
pixelPosition.y -= projectileSpeed;
break;
case DOWN:
pixelPosition.y += projectileSpeed;
break;
}
}
public void draw(Graphics g){
g.drawImage(bulletTexture,pixelPosition.x,pixelPosition.y,tileSize,tileSize,null);
}
public Scene.Location getGridPosition(){
return new Scene.Location(pixelPosition.x/tileSize,pixelPosition.y/tileSize);
}
public Type getType(){
return Drawable.Type.BULLET;
}
}
// returns -1 and -1 to indicate absence from map
public Scene.Location getGridPosition(){
if(showAsPickup)
return pickupLocation;
else
return new Scene.Location(-1,-1);
}
}