RuneHive-Game
Loading...
Searching...
No Matches
CannonManager.java
Go to the documentation of this file.
1package com.runehive.content.combat.cannon;
2
3import com.runehive.game.world.items.Item;
4import com.runehive.game.world.position.Area;
5import com.runehive.net.packet.out.SendMessage;
6import com.runehive.game.Animation;
7import com.runehive.game.Projectile;
8import com.runehive.game.world.entity.mob.npc.Npc;
9import com.runehive.game.world.entity.mob.player.Player;
10import com.runehive.game.world.World;
11import com.runehive.game.world.region.Region;
12import com.runehive.util.Utility;
13
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.Map;
17
18public class CannonManager {
19
20 static Map<String, Cannon> ACTIVE_CANNONS = new HashMap<>();
21
30
41
42 public static void test(Player player) {
43 drop(player, new Cannon(player.getName(), player.getPosition()));
44 }
45
46 public static void drop(Player player, Cannon cannon) {
47 if (ACTIVE_CANNONS.containsKey(player.getName())) {
48 player.send(new SendMessage("You already have a cannon active!"));
49 return;
50 }
51 if(Area.inCatacombs(player)) {
52 player.send(new SendMessage("You cannot set up a cannon here!"));
53 return;
54 }
55
56 if (cannon.getStage().ordinal() != 0) {
57 player.send(new SendMessage("You have already started setting up a cannon!"));
58 return;
59 }
60
61 for (Cannon other : ACTIVE_CANNONS.values()) {
62 if (other.getPosition().isWithinDistance(player.getPosition(), 5)) {
63 player.send(new SendMessage("You are trying to build too close to another cannon!"));
64 return;
65 }
66 }
67
68 World.schedule(new CannonBuild(player, cannon));
69 }
70
71 public static void pickup(Player player) {
72 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
73
74 if (cannon == null) {
75 player.send(new SendMessage("This is not your cannon!"));
76 return;
77 }
78
79 if (!cannon.getOwner().equalsIgnoreCase(player.getName())) {
80 player.send(new SendMessage("This is not your cannon!"));
81 return;
82 }
83
84 player.animate(new Animation(827));
85
86 if(cannon.getAmmunition() != 0)
87 player.inventory.add(new Item(2, cannon.getAmmunition()));
88
89 int[] ids = { 6, 8, 10, 12 };
90 for(int index = 0; index < ids.length; index++)
91 player.inventory.add(new Item(ids[index]));
92
93 cannon.unregister();
94 ACTIVE_CANNONS.remove(player.getName());
95 }
96
97 public static void empty(Player player) {
98 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
99
100 if (cannon == null) {
101 player.send(new SendMessage("This is not your cannon!"));
102 return;
103 }
104
105 if (!cannon.getOwner().equalsIgnoreCase(player.getName())) {
106 player.send(new SendMessage("This is not your cannon!"));
107 return;
108 }
109
110 if(cannon.getAmmunition() != 0)
111 player.inventory.add(new Item(2, cannon.getAmmunition()));
112
113 cannon.setFiring(false);
114 cannon.setAmmunition(0);
115 }
116
117 public static void logout(Player player) {
118 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
119
120 if(cannon == null) return;
121
122
123 if(cannon.getAmmunition() != 0)
124 player.inventory.add(new Item(2, cannon.getAmmunition()));
125
126 int[] ids = { 6, 8, 10, 12 };
127 for(int index = 0; index < ids.length; index++)
128 player.inventory.add(new Item(ids[index]));
129
130 cannon.unregister();
131 ACTIVE_CANNONS.remove(player.getName());
132 }
133
134 public static void load(Player player) {
135 Cannon cannon = ACTIVE_CANNONS.get(player.getName());
136
137 if (cannon == null) {
138 player.send(new SendMessage("This is not your cannon!"));
139 return;
140 }
141
142 if (!cannon.getOwner().equalsIgnoreCase(player.getName())) {
143 player.send(new SendMessage("This is not your cannon!"));
144 return;
145 }
146
147 if (!player.inventory.contains(2)) {
148 player.send(new SendMessage("You do not have any Cannon balls."));
149 return;
150 }
151
152 int needed = 30 - cannon.getAmmunition();
153
154 if (needed == 0) {
155 player.send(new SendMessage("Your cannon is full."));
156 return;
157 }
158
159 int cannon_balls = player.inventory.computeAmountForId(2);
160
161 if (cannon_balls <= needed) {
162 player.inventory.remove(2, cannon_balls);
163 player.send(new SendMessage("You load the last of your cannon balls"));
164 cannon.setAmmunition(cannon.getAmmunition() + cannon_balls);
165 } else {
166 player.inventory.remove(2, needed);
167 player.send(new SendMessage("You load " + needed + " balls into the cannon."));
168 cannon.setAmmunition(cannon.getAmmunition() + needed);
169 }
170
171 if (cannon.isFiring()) {
172 player.send(new SendMessage("The cannon is already firing!"));
173 return;
174 }
175
176 cannon.setFiring(true);
178 }
179
180 public static Projectile getCannonFire() {
181 Projectile p = new Projectile(53);
182 p.setStartHeight(50);
183 p.setEndHeight(50);
184 return p;
185 }
186
187 public static Npc[] getNpc(Cannon cannon) {
188 ArrayList<Npc> attack = new ArrayList<>();
189
190 for (Npc npc : World.getNpcs()) {
191 if (npc == null) {
192 continue;
193 }
194
196 continue;
197 }
198
199 if (!npc.definition.isAttackable()) {
200 continue;
201 }
202
203 attack.add(npc);
204 }
205
206 Npc[] npc = new Npc[attack.size()];
207
208 for (int i = 0; i < npc.length; i++) {
209 npc[i] = attack.get(i);
210 }
211
212 return npc;
213 }
214
215 public static void rotate(Cannon cannon) {
216 switch (cannon.getRotation()) {
217 case NORTH:
218 World.sendObjectAnimation(516, cannon.getObject());
219 break;
220 case NORTH_EAST:
221 World.sendObjectAnimation(517, cannon.getObject());
222 break;
223 case EAST:
224 World.sendObjectAnimation(518, cannon.getObject());
225 break;
226 case SOUTH_EAST:
227 World.sendObjectAnimation(519, cannon.getObject());
228 break;
229 case SOUTH:
230 World.sendObjectAnimation(520, cannon.getObject());
231 break;
232 case SOUTH_WEST:
233 World.sendObjectAnimation(521, cannon.getObject());
234 break;
235 case WEST:
236 World.sendObjectAnimation(514, cannon.getObject());
237 break;
238 case NORTH_WEST:
239 World.sendObjectAnimation(515, cannon.getObject());
240 break;
241 }
242 }
243
244}
static void drop(Player player, Cannon cannon)
Class that models a single animation used by an entity.
void setStartHeight(int startHeight)
void setEndHeight(int endHeight)
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
static MobList< Npc > getNpcs()
Definition World.java:548
static void sendObjectAnimation(int animation, GameObject object)
Sends a world object animation.
Definition World.java:284
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
String getName()
Gets the name of this entity.
Definition Player.java:774
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int computeAmountForId(int id)
Computes the total quantity of the Items in this container with id.
boolean remove(Item item)
Attempts to withdraw item from this container.
boolean add(Item item)
Attempts to deposit item into this container.
boolean contains(int id)
Determines if this container contains id.
Handles checking if mobs are in a certain area.
Definition Area.java:13
static boolean inCatacombs(Interactable entity)
Definition Area.java:204
Represents a single region.
Definition Region.java:21
The OutgoingPacket that sends a message to a Players chatbox in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static boolean withinDistance(Interactable source, Interactable target, int radius)
Definition Utility.java:470