-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerShip.java
130 lines (111 loc) · 3.83 KB
/
PlayerShip.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
package io.github.moonslanding.tlm.entities;
import io.github.moonslanding.tlm.engine.GameWorld;
import io.github.moonslanding.tlm.engine.SpritedGameObject;
import io.github.moonslanding.tlm.managers.ProjectileManager;
import io.github.moonslanding.tlm.weapons.Weapon;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.awt.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class PlayerShip extends SpritedGameObject {
// Attributes for basic turret
private final int baseDamage = 5;
private final double fireSpeed = 1.5;
private final ObjectArrayList<WeaponEntity> weapons = new ObjectArrayList<>();
private final Timer firing = new Timer();
private int level = 1;
private int hullCount = 10;
private int resources = 0;
public PlayerShip(int spawnX, int spawnY) {
super(spawnX, spawnY, "player_ship");
setTint(Color.CYAN);
setWidth(getWidth() - 2);
setHeight(getHeight() - 2);
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getHullCount() {
return hullCount;
}
public void setHullCount(int hullCount) {
this.hullCount = hullCount;
}
public int getBaseDamage() {
return baseDamage;
}
public double getFireSpeed() {
return fireSpeed;
}
public int getResources() {
return resources;
}
public void setResources(int resources) {
this.resources = resources;
}
public void startFiring(GameWorld world) {
firing.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
ProjectileEntity proj = ProjectileManager.getInstance().getProjectile(ProjectileEntity.ProjectileSide.PLAYER);
proj.relocate(getX(), getY());
proj.setFacing(getFacing());
proj.setMaxAliveTime(60);
proj.setVelocity(3);
proj.setDamage(getBaseDamage());
world.addObject(proj);
proj.setAlive(true);
}
}, 10L, (long) (1000 / getFireSpeed()));
}
public void addWeaponEntity(GameWorld world, Weapon weapon) {
WeaponEntity entity = new WeaponEntity(this, weapon);
weapons.add(entity);
world.addObject(entity);
updateWeaponsPosition();
entity.startAI(world, entity);
}
public void callSupply(GameWorld world) {
Random rand = new Random();
WeaponPickupEntity e = new WeaponPickupEntity();
int x = rand.nextInt(getX() - 200, getX() + 201);
int y = rand.nextInt(getY() - 200, getY() + 201);
if (x < 0) x = 0;
if (x > world.getWidth()) x = world.getWidth();
if (y < 0) y = 0;
if (y > world.getHeight()) y = world.getHeight();
e.relocate(x, y);
e.setAlive(true);
world.addObject(e);
}
private void updateWeaponsPosition() {
if (weapons.size() == 0) return;
for (int i = 0; i < weapons.size(); i++) {
weapons.get(i).relocate(
(int) ((Math.sin((double) i / weapons.size() * 2 * Math.PI) * (getWidth() * 2)) + getX()),
(int) ((Math.cos((double) i / weapons.size() * 2 * Math.PI) * (getHeight() * 2)) + getY())
);
}
}
public void destroy(GameWorld world) {
firing.cancel();
if (weapons.size() == 0) return;
for (WeaponEntity weapon : weapons) {
world.removeObject(weapon);
weapon.destroy();
}
world.removeObject(this);
}
@Override
public void move(int dx, int dy) {
super.move(dx, dy);
if (weapons.size() == 0) return;
for (WeaponEntity weapon : weapons) {
weapon.move(dx, dy);
}
}
}