Guest User

Untitled

a guest
Jan 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. package org.rs2server.rs2.model;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import org.rs2server.rs2.Constants;
  8. import org.rs2server.rs2.model.region.Region;
  9. import org.rs2server.rs2.tickable.Tickable;
  10.  
  11. /**
  12. * Represents a cannon in game
  13. * @author Michael
  14. *
  15. */
  16. public class Cannon {
  17.  
  18. /**
  19. * The random number generator.
  20. */
  21. private final Random random = new Random();
  22.  
  23. /**
  24. * The player who owns this cannon.
  25. */
  26. private Player player;
  27.  
  28. /**
  29. * The game object for the cannon.
  30. */
  31. private GameObject gameObject;
  32.  
  33. /**
  34. * The parts added to this cannon.
  35. */
  36. private List<Item> partsAdded;
  37.  
  38. /**
  39. * The facing state of this cannon.
  40. */
  41. private FacingState facingState;
  42.  
  43. /**
  44. * The running tick.
  45. */
  46. private Tickable runningTick;
  47.  
  48. /**
  49. * The amount of cannon balls currently loaded.
  50. */
  51. private int cannonBalls = 0;
  52.  
  53. /**
  54. * Represents the states that this cannon can face.
  55. * @author Michael
  56. *
  57. */
  58. private enum FacingState {
  59. NORTH(0, 515),
  60. NORTH_EAST(1, 516),
  61. EAST(2, 517),
  62. SOUTH_EAST(3, 518),
  63. SOUTH(4, 519),
  64. SOUTH_WEST(5, 520),
  65. WEST(6, 521),
  66. NORTH_WEST(7, 514);
  67.  
  68. /**
  69. * A map of ids to facing states.
  70. */
  71. private static List<FacingState> facingStates = new ArrayList<FacingState>();
  72.  
  73. /**
  74. * Populates the facing state list.
  75. */
  76. static {
  77. for(FacingState facingState : FacingState.values()) {
  78. facingStates.add(facingState);
  79. }
  80. }
  81.  
  82. public static FacingState forId(int id) {
  83. for(FacingState facingState : facingStates) {
  84. if(facingState.getId() == id) {
  85. return facingState;
  86. }
  87. }
  88. return null;
  89. }
  90.  
  91. /**
  92. * The id of this facing state.
  93. */
  94. private int id;
  95.  
  96. /**
  97. * The animation id this face performs.
  98. */
  99. private int animationId;
  100.  
  101. FacingState(int id, int animationId) {
  102. this.id = id;
  103. this.animationId = animationId;
  104. }
  105.  
  106. public int getId() {
  107. return id;
  108. }
  109.  
  110. public int getAnimationId() {
  111. return animationId;
  112. }
  113. }
  114.  
  115. public static Item CANNON_BASE = new Item(6);
  116.  
  117. public Cannon(Player player, Location location) {
  118. this.player = player;
  119. this.facingState = FacingState.NORTH;
  120. this.gameObject = new GameObject(location, 7, 10, 0, false);
  121. this.partsAdded = new ArrayList<Item>();
  122. partsAdded.add(CANNON_BASE);
  123. player.getInventory().remove(CANNON_BASE);
  124. World.getWorld().register(this.gameObject);
  125. }
  126.  
  127. public void destroy() {
  128. World.getWorld().unregister(gameObject, true);
  129. for(Item item : partsAdded) {
  130. if(!player.getInventory().add(item)) {
  131. if(player.getBank().add(item)) {
  132. player.getActionSender().sendMessage("You don't have enough inventory space to pick up the " + item.getDefinition().getName().toLowerCase() + " so");
  133. player.getActionSender().sendMessage("it has been deposited into your bank.");
  134. }
  135. //uh oh
  136. }
  137. }
  138. if(cannonBalls > 0) {
  139. Item item = new Item(2, cannonBalls);
  140. if(!player.getInventory().add(item)) {
  141. if(player.getBank().add(item)) {
  142. player.getActionSender().sendMessage("You don't have enough inventory space to pick up the " + item.getDefinition().getName().toLowerCase() + " so");
  143. player.getActionSender().sendMessage("it has been deposited into your bank.");
  144. }
  145. //uh oh
  146. }
  147. }
  148. if(runningTick != null) {
  149. runningTick.stop();
  150. }
  151. player.setAttribute("cannon", null);
  152. }
  153.  
  154. public void fire() {
  155. if(runningTick != null) {
  156. //already running
  157. return;
  158. }
  159. if(cannonBalls < 1) {
  160. player.getActionSender().sendMessage("There are no cannonballs currently loaded.");
  161. return;
  162. }
  163. runningTick = new Tickable(1) {
  164. @Override
  165. public void execute() {
  166. if(cannonBalls < 1) {
  167. this.stop();
  168. runningTick = null;
  169. player.getActionSender().sendMessage("Your cannon has run out of ammunition.");
  170. return;
  171. }
  172. for(Region r : gameObject.getRegion().getSurroundingRegions()) {
  173. for(Player player : r.getPlayers()) {
  174. player.getActionSender().animateObject(gameObject, facingState.getAnimationId());
  175. }
  176. }
  177.  
  178. int id = facingState.getId();
  179. if(id == 7) {
  180. id = -1;
  181. }
  182. facingState = FacingState.forId(id + 1);
  183.  
  184.  
  185. int delay = 2;
  186. for(Region r : gameObject.getRegion().getSurroundingRegions()) {
  187. for(final NPC npc : r.getNpcs()) {
  188. if(cannonBalls < 1) {
  189. break;
  190. }
  191. if(delay > 3) {
  192. break;
  193. }
  194. int newDist = gameObject.getLocation().distanceToEntity(gameObject, npc);
  195. if(newDist <= 5 && newDist >= 1) {
  196. boolean canHit = false;
  197. int myX = gameObject.getCentreLocation().getX();
  198. int myY = gameObject.getCentreLocation().getY();
  199. int theirX = npc.getCentreLocation().getX();
  200. int theirY = npc.getCentreLocation().getY();
  201. switch(facingState) {
  202. case NORTH:
  203. if(theirY > myY && theirX >= myX - 1 && theirX <= myX + 1) {
  204. canHit = true;
  205. }
  206. break;
  207. case NORTH_EAST:
  208. if(theirX >= myX + 1 && theirY >= myY + 1) {
  209. canHit = true;
  210. }
  211. break;
  212. case EAST:
  213. if(theirX > myX && theirY >= myY - 1 && theirY <= myY + 1) {
  214. canHit = true;
  215. }
  216. break;
  217. case SOUTH_EAST:
  218. if(theirY <= myY - 1 && theirX >= myX + 1) {
  219. canHit = true;
  220. }
  221. break;
  222. case SOUTH:
  223. if(theirY < myY && theirX >= myX - 1 && theirX <= myX + 1) {
  224. canHit = true;
  225. }
  226. break;
  227. case SOUTH_WEST:
  228. if(theirX <= myX - 1 && theirY <= myY - 1) {
  229. canHit = true;
  230. }
  231. break;
  232. case WEST:
  233. if(theirX < myX && theirY >= myY - 1 && theirY <= myY + 1) {
  234. canHit = true;
  235. }
  236. break;
  237. case NORTH_WEST:
  238. if(theirX <= myX - 1 && theirY >= myY + 1) {
  239. canHit = true;
  240. }
  241. break;
  242. }
  243. if(!canHit) {
  244. continue;
  245. }
  246. if(player.getActiveCombatAction().canHit(player, npc, false, true)) {
  247. gameObject.playProjectile(Projectile.create(gameObject.getCentreLocation(), npc.getCentreLocation(), 53, 15 + (delay * 10), 50, 50 + (newDist * 10), 37, 37, npc.getProjectileLockonIndex(), 0, 96));
  248. cannonBalls--;
  249. delay += 1;
  250. World.getWorld().submit(new Tickable(delay) {
  251. @Override
  252. public void execute() {
  253. int damage = random.nextInt(30);
  254. player.getSkills().addExperience(Skills.RANGE, (4 * damage) * Constants.EXP_MODIFIER);
  255. npc.inflictDamage(new Hit(damage), player);
  256. npc.getActiveCombatAction().defend(player, npc, true);
  257. this.stop();
  258. }
  259. });
  260. }
  261. }
  262. }
  263. }
  264. }
  265. };
  266. World.getWorld().submit(runningTick);
  267. }
  268.  
  269. public void addPart(Item item) {
  270. int id = -1;
  271. switch(item.getId()) {
  272. case 8:
  273. id = 8;
  274. break;
  275. case 10:
  276. id = 9;
  277. break;
  278. case 12:
  279. id = 6;
  280. break;
  281. }
  282. if(id != -1) {
  283. player.getInventory().remove(item);
  284. World.getWorld().unregister(gameObject, true);
  285. this.gameObject = new GameObject(gameObject.getLocation(), id, 10, 0, false);
  286. World.getWorld().register(this.gameObject);
  287. partsAdded.add(item);
  288. }
  289. }
  290.  
  291. public GameObject getGameObject() {
  292. return gameObject;
  293. }
  294.  
  295. public int getCannonBalls() {
  296. return cannonBalls;
  297. }
  298.  
  299. public void addCannonBalls(int cannonBalls) {
  300. this.cannonBalls += cannonBalls;
  301. }
  302. }
Add Comment
Please, Sign In to add comment