Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. private int x;
  4. private int y;
  5. private int life=70;
  6. private Input input;
  7. private int velocity = 5;
  8. private Shape hitbox;
  9. private boolean moving;
  10. private Image image;
  11. private double mouseX;
  12. private double mouseY;
  13. private boolean spawn;
  14. private ArrayList <MeleeWeapon> attack;
  15. private Shape weapon;
  16. private SpriteSheet weaponWalk;
  17. private SpriteSheet walk;
  18. private Animation walkingAnim;
  19. private Animation weaponWalkAnim;
  20. private SpriteSheet meleeAttack;
  21. private Animation attackAnim;
  22. private boolean attackCheck;
  23. private int attackTimer;
  24. private int stamina=100;
  25. private static final int maxLife = 100;
  26. private double xDistance;
  27. private double yDistance;
  28.  
  29. private SpriteSheet sprites;
  30. private Animation walkAnim;
  31.  
  32. private SpriteSheet bulletSheet;
  33. private Animation shootingAnimation;
  34. private boolean discarded;
  35. public boolean hit;
  36. private boolean shotCheck;
  37.  
  38. public Player(int x, int y, Input input) throws SlickException{
  39. this.x = x;
  40. this.y = y;
  41. this.input = input;
  42. hitbox = new Circle(x, y, 15, 20);
  43. this.image = new Image("player.png");
  44. attack = new ArrayList <MeleeWeapon>();
  45. this.weapon = new Rectangle(x+25,y+15,15,5);
  46.  
  47.  
  48. sprites = new SpriteSheet("walking.png",32,32);
  49. walkAnim = new Animation(sprites,100);
  50.  
  51. weaponWalk = new SpriteSheet("pistolSheet.png",32,32);
  52. weaponWalkAnim = new Animation(weaponWalk,100);
  53.  
  54. walk = new SpriteSheet("feet.png",32,32);
  55. walkingAnim = new Animation(walk,100);
  56.  
  57. meleeAttack = new SpriteSheet("unarmed.png",32,32);
  58. attackAnim = new Animation(meleeAttack,80);
  59.  
  60. bulletSheet = new SpriteSheet("shotm16.png",32,32);
  61. shootingAnimation = new Animation(bulletSheet,100);
  62. }
  63.  
  64. public void move(int delta){
  65.  
  66. if(input.isKeyDown(Input.KEY_W)==true){
  67. y-=5;
  68. walkingAnim.getCurrentFrame().setRotation(90);
  69. }
  70.  
  71. if(input.isKeyDown(Input.KEY_A)==true){
  72. x-=5;
  73. walkingAnim.getCurrentFrame().setRotation(30);
  74. }
  75.  
  76. if(input.isKeyDown(Input.KEY_S)==true){
  77. y+=5;
  78. walkingAnim.getCurrentFrame().setRotation(60);
  79. }
  80.  
  81. if(input.isKeyDown(Input.KEY_D)==true){
  82. x+=5;
  83. walkingAnim.getCurrentFrame().setRotation(0);
  84.  
  85. }
  86.  
  87. if(input.isKeyDown(Input.KEY_D)==true||input.isKeyDown(Input.KEY_S)==true||input.isKeyDown(Input.KEY_W)==true||input.isKeyDown(Input.KEY_A)==true){
  88. moving = true;
  89. walkingAnim.start();
  90. }
  91.  
  92. else{
  93. moving=false;
  94. }
  95.  
  96.  
  97. if(moving==false){
  98. walkingAnim.setCurrentFrame(0);
  99. walkingAnim.stop();
  100.  
  101. stamina++;
  102. if(stamina>=100){
  103. stamina=100;
  104. }
  105. if(stamina<=0){
  106. stamina=0;
  107. }
  108.  
  109. }
  110.  
  111. if(attackCheck==true){
  112. attackAnim.start();
  113. }
  114.  
  115.  
  116.  
  117. if(spawn==true){
  118. spawn=false;
  119. }
  120. hitbox.setLocation(x, y);
  121. weapon.setLocation(x+25, y+15);
  122. }
  123.  
  124. public boolean getPlayerMovement(){
  125. return moving;
  126. }
  127.  
  128.  
  129. //this is where the player and the animations rotate
  130. public void rotate(GameContainer gc){
  131.  
  132. mouseX = input.getMouseX();
  133. mouseY = input.getMouseY();
  134.  
  135. xDistance = mouseX-x;
  136. yDistance = mouseY-y;
  137.  
  138. double angle = Math.toDegrees(Math.atan2(yDistance, xDistance));
  139.  
  140. System.out.println("------------------");
  141. System.out.println("Mouse x: " +mouseX);
  142. System.out.println("Mouse y: " +mouseY);
  143. System.out.println("Mouses y: " +angle);
  144. System.out.println("------------------");
  145.  
  146. weaponWalkAnim.getCurrentFrame().setRotation((float) angle);
  147. walkAnim.getCurrentFrame().setRotation((float) angle);
  148. attackAnim.getCurrentFrame().setRotation((float) angle);
  149. shootingAnimation.getCurrentFrame().setRotation((float) angle);
  150. image.setRotation((float)angle);
  151.  
  152.  
  153. }
  154.  
  155. public int getVel(){
  156. return velocity;
  157. }
  158.  
  159. public void spawnAttack(Input input, Player player){
  160. for (MeleeWeapon weapon : attack){
  161. weapon.attack(input, player);
  162. }
  163. }
  164.  
  165. public void cooldown(int delta){
  166. if(attackCheck==true&&input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)==false){
  167. attackTimer+=delta/10;
  168. if(attackTimer>=10){
  169. attackTimer=0;
  170. attackCheck=false;
  171. }
  172. }
  173. else if(shotCheck==true){
  174. attackTimer+=delta/10;
  175. if(attackTimer>=10){
  176. attackTimer=0;
  177. shotCheck=false;
  178. }
  179. }
  180. }
  181.  
  182.  
  183.  
  184.  
  185. public void attack(int delta) throws SlickException{
  186. if(input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)&&attackCheck==false&&stamina>50&&discarded==true){
  187. attackCheck=true;
  188. attackAnim.setLooping(true);
  189. attack.add(new MeleeWeapon (x, y, 0));
  190. attackTimer=0;
  191. }
  192. if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)&&shotCheck==false&&discarded==false){
  193. shotCheck=true;
  194. attackTimer=0;
  195. }
  196. }
  197.  
  198. public boolean hit(Shape s){
  199. boolean hit = false;
  200. for (int i = 0; i < attack.size(); i++){
  201. if (!hit){
  202. hit = attack.get(i).getHitbox().intersects(s);
  203.  
  204. }
  205.  
  206. if(attack.get(i).getHitbox().intersects(s)){
  207. attack.remove(i);
  208. hit=true;
  209. }
  210. }
  211.  
  212. return hit;
  213. }
  214.  
  215.  
  216. public void setDiscarded(Firearm weapon){
  217. discarded=weapon.discarded();
  218. }
  219.  
  220. public void setAnimations(String imagePath, String animation, int spriteHeight, int spriteWidth,String bulletSprites) throws SlickException{
  221. image=new Image(imagePath);
  222. weaponWalk= new SpriteSheet(animation,32,32);
  223. bulletSheet = new SpriteSheet(bulletSprites,32,32);
  224. }
  225.  
  226. public void draw(Graphics gfx, Firearm weapon) throws SlickException{
  227. if(moving==true&&weapon.getShooting()==true&&shotCheck==true&&attackCheck==false&&discarded==false&&weapon.getmagazin()>0||input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)&&attackCheck==false&&shotCheck==true&&discarded==false&&weapon.getShooting()==true&&weapon.getmagazin()>0){
  228. walkingAnim.draw(x,y);
  229. shootingAnimation.draw(x, y);
  230. }
  231.  
  232.  
  233. else if(moving==true&&attackCheck==false&&discarded==true){
  234. walkingAnim.draw(x,y);
  235. walkAnim.draw(x,y);
  236. }
  237. else if(moving==false&&attackCheck==false){
  238. image.draw(x,y);
  239. if(moving==true){
  240.  
  241. }
  242. }
  243.  
  244. else if(discarded==false&&attackCheck==false){
  245. walkingAnim.draw(x,y);
  246. weaponWalkAnim.draw(x, y);
  247. }
  248.  
  249. else{
  250. walkingAnim.draw(x, y);
  251. attackAnim.draw(x,y);
  252. attackAnim.setLooping(false);
  253. }
  254.  
  255.  
  256. for (int i = 0; i < attack.size(); i++){
  257. gfx.draw(attack.get(i).getHitbox());
  258. spawn=true;
  259.  
  260. if(spawn=true&&attack.get(i).getHitbox().getX()>=x+10||attack.get(i).getHitbox().getY()>=y+10){
  261. attack.clear();
  262.  
  263. }
  264. else if(spawn=true&&attack.get(i).getHitbox().getX()<=x-10||attack.get(i).getHitbox().getY()<=y-10){
  265. attack.clear();
  266.  
  267. }
  268. }
  269.  
  270.  
  271.  
  272. }
  273.  
  274. public Shape getHitbox(){
  275. return hitbox;
  276. }
  277.  
  278. public int getMaxLife(){
  279. return maxLife;
  280. }
  281.  
  282.  
  283. public Shape getWeapon(){
  284. return weapon;
  285. }
  286.  
  287. public int getLife(){
  288. return life;
  289. }
  290.  
  291. public void setLife(int life){
  292. this.life=life;
  293. }
  294.  
  295. public boolean getHit(){
  296. return hit;
  297. }
  298.  
  299. public void setHit(boolean hit){
  300. this.hit=hit;
  301. }
  302.  
  303. public int getX(){
  304. return x;
  305. }
  306.  
  307. public int getY(){
  308. return y;
  309. }
  310.  
  311. public boolean getMoving(){
  312. return moving;
  313. }
  314.  
  315. import org.newdawn.slick.GameContainer;
  316.  
  317. this.numTilesX = map.getWidth();
  318. this.numTilesY = map.getHeight();
  319.  
  320. this.tileWidth = map.getTileWidth();
  321. this.tileHeight = map.getTileHeight();
  322.  
  323. this.mapHeight = this.numTilesX * this.tileWidth;
  324. this.mapWidth = this.numTilesY * this.tileHeight;
  325.  
  326. this.gc = gc;
  327.  
  328. //if the camera is at the right or left edge lock it to prevent a black bar
  329. if(cameraX < 0) cameraX = 0;
  330. if(cameraX + gc.getWidth() > mapWidth) cameraX = mapWidth - gc.getWidth();
  331.  
  332. //if the camera is at the top or bottom edge lock it to prevent a black bar
  333. if(cameraY < 0) cameraY = 0;
  334. if(cameraY + gc.getHeight() > mapHeight) cameraY = mapHeight - gc.getHeight();
  335.  
  336. //calculate the index of the leftmost tile that is being displayed
  337. int tileIndexX = (int) (cameraX / tileWidth);
  338. int tileIndexY = (int) (cameraY / tileHeight);
  339.  
  340. //finally draw the section of the map on the screen
  341. map.render(
  342. tileOffsetX + offsetX,
  343. tileOffsetY + offsetY,
  344. tileIndexX,
  345. tileIndexY,
  346. (gc.getWidth() - tileOffsetX) / tileWidth + 1,
  347. (gc.getHeight() - tileOffsetY) / tileHeight + 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement