Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. package com.mygdx.Squaizer;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4.  
  5. import com.badlogic.gdx.ApplicationAdapter;
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.graphics.GL20;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.Sprite;
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  12. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  13. import com.badlogic.gdx.input.GestureDetector;
  14. import com.badlogic.gdx.math.Vector2;
  15. import com.badlogic.gdx.math.Vector3;
  16. import com.badlogic.gdx.utils.TimeUtils;
  17. import com.badlogic.gdx.utils.Timer;
  18.  
  19.  
  20. public class MyGdxGameSquaizer extends ApplicationAdapter {
  21. public static SpriteBatch batch;
  22. float originAspect;
  23. float currentAspect;
  24. Sprite playerSprite;
  25. Joystick j1, j2;
  26. OrthographicCamera cam;
  27. Player currPlayer;
  28. Square s;
  29. LinkedList<Projectile> pewpews;
  30. LinkedList<Square> entities;
  31. TimeUtils timer;
  32. long startingTime;
  33. long currentTime;
  34.  
  35. int mover;
  36. int shooter;
  37.  
  38.  
  39. Texture bg, playerTexture, joystickTexture;
  40.  
  41. @Override
  42. public void create () {
  43. cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  44. cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  45. cam.update();
  46. batch = new SpriteBatch();
  47. bg = new Texture("Squaizer-bg.png");
  48. currPlayer = new Player("Squaizer-player.png", new Vector2(Gdx.graphics.getWidth() / 2,Gdx.graphics.getHeight() / 2), 2);
  49. s = new Square("Squaizer-player.png", new Vector2(100,500), 0);
  50.  
  51. currentAspect = (Gdx.graphics.getWidth() * Gdx.graphics.getHeight() / (1280 * 736));
  52.  
  53. j1 = new Joystick(50f,50f, currPlayer, currentAspect);
  54.  
  55. j2 = new Joystick(Gdx.graphics.getWidth() - 150 - 50, 50, currPlayer, currentAspect);
  56.  
  57. Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener(j1, j2, cam)));
  58.  
  59. pewpews = new LinkedList<Projectile>();
  60. startingTime = TimeUtils.millis();
  61. mover = -1;
  62. shooter = -1;
  63.  
  64.  
  65.  
  66. }
  67.  
  68. @Override
  69. public void render () {
  70. currentTime = TimeUtils.timeSinceMillis(startingTime);
  71. //GAME MECHANICS HERE
  72. updateGameState();
  73. //GAME MECHANICS HERE
  74.  
  75.  
  76. Gdx.gl.glClearColor(1, 0, 0, 1);
  77. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  78. batch.setProjectionMatrix(cam.combined);
  79.  
  80.  
  81.  
  82. Vector3 input0 = new Vector3(10,10,0);
  83.  
  84.  
  85.  
  86.  
  87. batch.begin();
  88.  
  89. batch.draw(bg, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  90.  
  91.  
  92. s.draw(batch);
  93.  
  94. currPlayer.move();
  95. currPlayer.draw(batch);
  96.  
  97.  
  98.  
  99. if(Gdx.input.isTouched(0)){
  100. input0.x = Gdx.input.getX(0);
  101. input0.y = Gdx.input.getY(0);
  102. cam.unproject(input0);
  103. if(Joystick.getJoystick(0) != null){
  104. Joystick.getJoystick(0).updateState(new Vector2(input0.x , input0.y), pewpews, 0);
  105. }
  106.  
  107. else if(j1.isInCircle(input0.x , input0.y)){
  108. if(mover == 1){
  109. shooter = 0;
  110. double degrees = Math.toDegrees(Math.atan2(input0.y - (currPlayer.sprite.getY() + currPlayer.sprite.getOriginY()), input0.x - (currPlayer.sprite.getX() + currPlayer.sprite.getOriginX())));
  111. this.currPlayer.rotateSprite((float)degrees);
  112. this.currPlayer.shoot(pewpews);
  113. j1.deActivate();
  114. }
  115. else{
  116. j1.updateState(new Vector2(input0.x , input0.y), pewpews, 0);
  117. mover = 0;
  118. }
  119. }
  120.  
  121. else if(j2.isInCircle(input0.x , input0.y)){
  122. if(mover == 1){
  123. shooter = 0;
  124. double degrees = Math.toDegrees(Math.atan2(input0.y - (currPlayer.sprite.getY() + currPlayer.sprite.getOriginY()), input0.x - (currPlayer.sprite.getX() + currPlayer.sprite.getOriginX())));
  125. this.currPlayer.rotateSprite((float)degrees);
  126. this.currPlayer.shoot(pewpews);
  127. j2.deActivate();
  128. }
  129. else{
  130. j2.updateState(new Vector2(input0.x , input0.y), pewpews, 0);
  131. mover = 0;
  132. }
  133. }
  134. else if(shooter != 1){
  135. shooter = 0;
  136. double degrees = Math.toDegrees(Math.atan2(input0.y - (currPlayer.sprite.getY() + currPlayer.sprite.getOriginY()), input0.x - (currPlayer.sprite.getX() + currPlayer.sprite.getOriginX())));
  137. this.currPlayer.rotateSprite((float)degrees);
  138. this.currPlayer.shoot(pewpews);
  139. }
  140. }
  141. else{
  142. if(shooter == 0)
  143. shooter = -1;
  144. if(mover == 0){
  145. Joystick.getJoystick(0).deActivate();
  146. mover = -1;
  147. }
  148.  
  149. }
  150.  
  151.  
  152. if(Gdx.input.isTouched(1)){
  153. input0.x = Gdx.input.getX(1);
  154. input0.y = Gdx.input.getY(1);
  155. cam.unproject(input0);
  156. if(Joystick.getJoystick(1) != null){
  157. Joystick.getJoystick(1).updateState(new Vector2(input0.x , input0.y), pewpews, 1);
  158. }
  159. else if(j1.isInCircle(input0.x , input0.y)){
  160. if(mover == 0){
  161. shooter = 1;
  162. double degrees = Math.toDegrees(Math.atan2(input0.y - (currPlayer.sprite.getY() + currPlayer.sprite.getOriginY()), input0.x - (currPlayer.sprite.getX() + currPlayer.sprite.getOriginX())));
  163. this.currPlayer.rotateSprite((float)degrees);
  164. this.currPlayer.shoot(pewpews);
  165. j1.deActivate();
  166. }
  167. else{
  168. j1.updateState(new Vector2(input0.x , input0.y), pewpews, 1);
  169. mover = 1;
  170. }
  171. }
  172.  
  173. else if(j2.isInCircle(input0.x , input0.y)){
  174. if(mover == 0){
  175. shooter = 1;
  176. double degrees = Math.toDegrees(Math.atan2(input0.y - (currPlayer.sprite.getY() + currPlayer.sprite.getOriginY()), input0.x - (currPlayer.sprite.getX() + currPlayer.sprite.getOriginX())));
  177. this.currPlayer.rotateSprite((float)degrees);
  178. this.currPlayer.shoot(pewpews);
  179. j2.deActivate();
  180. }
  181. else{
  182. j2.updateState(new Vector2(input0.x , input0.y), pewpews, 1);
  183. mover = 1;
  184. }
  185. }
  186. else if(shooter != 0){
  187. shooter = 1;
  188. double degrees = Math.toDegrees(Math.atan2(input0.y - (currPlayer.sprite.getY() + currPlayer.sprite.getOriginY()), input0.x - (currPlayer.sprite.getX() + currPlayer.sprite.getOriginX())));
  189. this.currPlayer.rotateSprite((float)degrees);
  190. this.currPlayer.shoot(pewpews);
  191. }
  192. }
  193. else{
  194. if(shooter == 1)
  195. shooter = -1;
  196. if(mover == 1){
  197. Joystick.getJoystick(1).deActivate();
  198. mover = -1;
  199. }
  200. }
  201.  
  202. System.out.println("Mover: "+ mover);
  203. System.out.println("Shooter: "+ shooter);
  204.  
  205. if(!pewpews.isEmpty()) {
  206. for(Iterator<Projectile> iter = pewpews.iterator(); iter.hasNext();) {
  207. Projectile p = iter.next();
  208. p.draw(batch);
  209. p.move();
  210. if (p.outOfBounds()){
  211. iter.remove();
  212. }
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. }
  220. j1.draw(batch);
  221. j2.draw(batch);
  222. batch.end();
  223.  
  224. /* HITBOX TEST
  225. ShapeRenderer rend = new ShapeRenderer();
  226. LinkedList<Vector2[]> l = currPlayer.lineList();
  227. rend.setProjectionMatrix(cam.combined);
  228. rend.begin(ShapeRenderer.ShapeType.Line);
  229. rend.setColor(0,0,1,0);
  230. rend.line(l.get(0)[0].x, l.get(0)[0].y, l.get(0)[1].x, l.get(0)[1].y);
  231. rend.line(l.get(1)[0].x, l.get(1)[0].y, l.get(1)[1].x, l.get(1)[1].y);
  232. rend.line(l.get(2)[0].x, l.get(2)[0].y, l.get(2)[1].x, l.get(2)[1].y);
  233. rend.line(l.get(3)[0].x, l.get(3)[0].y, l.get(3)[1].x, l.get(3)[1].y);
  234. rend.end();*/
  235. System.out.println(Gdx.graphics.getWidth() + " " + Gdx.graphics.getHeight());
  236. }
  237.  
  238. private void updateGameState(){
  239. //System.out.println(currentTime);
  240.  
  241. }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement