Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. package sk.tuke.oop.game.actors.ripley;
  2. import sk.tuke.oop.framework.Actor;
  3. import sk.tuke.oop.framework.Animation;
  4. import sk.tuke.oop.framework.Input;
  5. import sk.tuke.oop.framework.Item;
  6. import sk.tuke.oop.game.actions.Drop;
  7. import sk.tuke.oop.game.actions.Fire;
  8. import sk.tuke.oop.game.actions.Move;
  9. import sk.tuke.oop.game.actions.Shift;
  10. import sk.tuke.oop.game.actions.Take;
  11. import sk.tuke.oop.game.actions.Use;
  12. import sk.tuke.oop.game.actors.Body;
  13. import sk.tuke.oop.game.actors.Cooler;
  14.  
  15. import sk.tuke.oop.game.actors.openables.ExitDoor;
  16. import sk.tuke.oop.game.actors.openables.LockedDoor;
  17.  
  18. import sk.tuke.oop.game.actors.openables.Locker;
  19. import sk.tuke.oop.game.items.Hammer;
  20. import sk.tuke.oop.game.items.Usable;
  21. import sk.tuke.oop.game.items.Wrench;
  22.  
  23.  
  24. public class Running implements RipleyState {
  25. private Ripley ripley;
  26. private Input input = Input.getInstance();
  27. private Move move;
  28. private boolean callBreak;
  29. private Animation animation = new Animation("sprites/player.png", 32, 32, 100);
  30. public Running(Ripley ripley){
  31. ripley.setAnimation(animation);
  32. ripley.getAnimation().setPingPong(true);
  33. this.ripley = ripley;
  34. callBreak = false;
  35. move = new Move(ripley,2,ripley.getX(),ripley.getY());
  36. }
  37. private void moveRipleyUp() {
  38. if (input.isKeyDown(Input.Key.UP))
  39. move = new Move(ripley, 2, 0, -1);
  40.  
  41. }
  42. private void moveRipleyDown(){
  43. if (input.isKeyDown(Input.Key.DOWN))
  44. move = new Move(ripley, 2, 0, 1);
  45.  
  46. }
  47. private void moveRipleftLeft(){
  48. if (input.isKeyDown(Input.Key.LEFT))
  49. move = new Move(ripley, 2, -1, 0);
  50. }
  51. private void moveRipleyRight(){
  52. if (input.isKeyDown(Input.Key.RIGHT))
  53. move = new Move(ripley, 2, 1, 0);
  54. }
  55. private void moveRipleyLeftUp(){
  56. if (input.isKeyDown(Input.Key.LEFT) && input.isKeyDown(Input.Key.UP))
  57. move = new Move(ripley, 2, -1, -1);
  58. }
  59. private void moveRipleyRightUp(){
  60. if (input.isKeyDown(Input.Key.RIGHT) && input.isKeyDown(Input.Key.UP))
  61. move = new Move(ripley, 2, 1, -1);
  62. }
  63. private void moveRipleyLeftDown(){
  64. if (input.isKeyDown(Input.Key.LEFT) && input.isKeyDown(Input.Key.DOWN))
  65. move = new Move(ripley, 2, -1, 1);
  66.  
  67. }
  68. private void moveRipleyRightDown(){
  69. if (input.isKeyDown(Input.Key.RIGHT) && input.isKeyDown(Input.Key.DOWN))
  70. move = new Move(ripley, 2, 1, 1);
  71.  
  72. }
  73. private void moveRipley(){
  74. moveRipleftLeft();
  75. moveRipleyDown();
  76. moveRipleyUp();
  77. moveRipleyRight();
  78. moveRipleyLeftDown();
  79. moveRipleyRightDown();
  80. moveRipleyLeftUp();
  81. moveRipleyRightUp();
  82. }
  83. private void ripleyBehaviour(){
  84. if (!input.isKeyDown(Input.Key.UP) && !input.isKeyDown(Input.Key.DOWN) && !input.isKeyDown(Input.Key.LEFT) && !input.isKeyDown(Input.Key.RIGHT)) {
  85. ripley.getAnimation().stop();
  86. }
  87. else {
  88. ripley.getAnimation().start();
  89. ripley.getAnimation().setPingPong(true);
  90. move.act();
  91. }
  92. }
  93. private void ripleyIsUsingItem(Actor actor){
  94. if(actor instanceof Cooler && input.isKeyPressed(Input.Key.E) && ripley.getBackpack().peek() instanceof Hammer){
  95.  
  96. ((Cooler) actor).useBy(ripley);
  97. ripley.getBackpack().remove((Item) ripley.getBackpack().peek());
  98. }
  99.  
  100. }
  101. private void openTheDoor(Use use, Actor actor){
  102.  
  103. if(((ExitDoor) actor).isOpen() && !callBreak) {
  104. use.execute();
  105. callBreak = true;
  106. }
  107. else if(!((ExitDoor) actor).isOpen() && !callBreak) {
  108. use.execute();
  109. callBreak = true;
  110. }
  111. }
  112. private void ripleyCooler(Use use,Actor actor){
  113. if(actor instanceof Cooler && input.isKeyPressed(Input.Key.E) && ripley.getBackpack().peek() instanceof Wrench && !callBreak){
  114. use.execute();
  115. ripley.getBackpack().remove(ripley.getBackpack().peek());
  116. callBreak = true;
  117.  
  118. }
  119. }
  120. private void lockedDoorAction(Use use,Actor actor){
  121. if(actor instanceof LockedDoor && input.isKeyPressed(Input.Key.E) && !((LockedDoor) actor).isLocked() && !callBreak ){
  122. use.execute();
  123. callBreak = true;
  124.  
  125. }
  126.  
  127. }
  128. private void lockerAction(Use use, Actor actor){
  129. if(actor instanceof Locker && input.isKeyPressed(Input.Key.E) && !callBreak ){
  130. use.execute();
  131. callBreak = true;
  132.  
  133. }
  134. }
  135. private void finalAction(Use use, Actor actor){
  136. if(input.isKeyPressed(Input.Key.E) && !(actor instanceof Cooler) && !callBreak ){
  137. use.execute();
  138. callBreak = true;
  139.  
  140. }
  141.  
  142. }
  143. private void exitAction(Use use, Actor actor){
  144. if(actor instanceof ExitDoor && input.isKeyPressed(Input.Key.E) && !callBreak ){
  145. openTheDoor(use,actor);
  146. callBreak = true;
  147.  
  148. }
  149.  
  150. }
  151. private void ripleyAllActors(){
  152. for(Actor actor : ripley.getWorld()) {
  153. if (ripley.intersects(actor) && actor instanceof Usable && !(actor instanceof Ripley)) {
  154. Use use = new Use(actor,ripley);
  155. lockerAction(use,actor);
  156. ripleyIsUsingItem(actor);
  157. ripleyCooler(use,actor);
  158. lockedDoorAction(use,actor);
  159. exitAction(use,actor);
  160. finalAction(use,actor);
  161. if(callBreak){
  162. callBreak = false;
  163. break;
  164.  
  165. }
  166.  
  167.  
  168. }
  169.  
  170.  
  171. }
  172. }
  173. @Override
  174. public void act() {
  175. Fire fire = new Fire(ripley);
  176. ripleyBehaviour();
  177. moveRipley();
  178. ripleyAllActors();
  179. takeIt();
  180. useIt();
  181. hitIt(fire);
  182. dropIt();
  183. shifIt();
  184. exit();
  185. }
  186. private void takeIt(){
  187. if(input.isKeyPressed(Input.Key.ENTER)) {
  188. for(Actor actor : ripley.getWorld()) {
  189. if (ripley.intersects(actor) && actor instanceof Item) {
  190. // System.out.println(actor);
  191. Take<Item> take = new Take<Item>(ripley.getBackpack(), (Item)actor);
  192. take.execute();
  193. // world.removeActor(actor);
  194. break;
  195. }
  196. }
  197. }
  198.  
  199. }
  200. private void useIt(){
  201. if(input.isKeyPressed(Input.Key.U)) {
  202. for(Actor actor : ripley.getWorld()) {
  203. if (ripley.intersects(actor) && actor instanceof Body) {
  204. ripley.useBy(actor);
  205. break;
  206. }
  207. }
  208. }
  209.  
  210. }
  211. private void exit(){
  212. if(input.isKeyPressed(Input.Key.ESCAPE)){
  213. System.exit(0);
  214. }
  215. }
  216. private void shifIt(){
  217. if(input.isKeyPressed(Input.Key.S)) {
  218. Shift shift = new Shift(ripley.getBackpack());
  219. shift.execute();
  220. }
  221. }
  222. private void dropIt(){
  223. if(input.isKeyPressed(Input.Key.BACK)) {
  224. Drop<Item> drop = new Drop<Item>(ripley.getBackpack(),ripley.getWorld(),ripley.getX(),ripley.getY());
  225. drop.execute();
  226. }
  227. }
  228. private void hitIt(Fire fire){
  229. if(input.isKeyPressed(Input.Key.SPACE)){
  230.  
  231. fire.execute();
  232. }
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement