Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. public class MMO extends Game implements InputProcessor {
  2. SpriteBatch batch;
  3. Texture img;
  4. static TiledMap tiledMap;
  5. OrthographicCamera camera;
  6. TiledMapRenderer tiledMapRenderer;
  7. Player player;
  8. static mapManager mapManager;
  9. static MapLayer MAPcollision;
  10.  
  11.  
  12.  
  13. @Override
  14. public void create () {
  15. mapManager = new mapManager();
  16. batch = new SpriteBatch();
  17. float w = Gdx.graphics.getWidth();
  18. float h = Gdx.graphics.getHeight();
  19.  
  20. camera = new OrthographicCamera();
  21. camera.setToOrtho(false,w,h);
  22. camera.update();
  23. tiledMap = new TmxMapLoader().load("Maps/base.tmx");
  24. tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
  25. player = new Player(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. Gdx.input.setInputProcessor(this);
  33.  
  34. }
  35.  
  36. @Override
  37. public void render () {
  38. Gdx.gl.glClearColor(1, 0, 0, 1);
  39. Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  40. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  41. MAPcollision = mapManager.getCollisionLayer();
  42. camera.update();
  43. player.update();
  44. update();
  45. tiledMapRenderer.setView(camera);
  46. tiledMapRenderer.render();
  47. camera.update();
  48. batch.setProjectionMatrix(camera.combined);
  49. batch.begin();
  50. player.render(batch);
  51. batch.end();
  52. camera.update();
  53.  
  54.  
  55. }
  56.  
  57. @Override
  58. public boolean keyDown(int keycode){
  59. return false;
  60. }
  61.  
  62. @Override
  63. public boolean keyUp(int keycode){
  64. if(keycode == Input.Keys.LEFT){
  65. //camera.translate(-32,0);
  66. player.nextPlayerPosition.x -= 32;
  67. player.direction = "LEFT";
  68. if(player.nextPlayerPosition.x < Gdx.graphics.getWidth() / 2){
  69. camera.translate(-32,0);
  70. }
  71. }
  72.  
  73.  
  74. if(keycode == Input.Keys.RIGHT){
  75. //camera.translate(32,0);
  76. player.nextPlayerPosition.x += 32;
  77. player.direction = "RIGHT";
  78. if(player.nextPlayerPosition.x > Gdx.graphics.getWidth() / 2){
  79. camera.translate(32,0);
  80. }
  81. }
  82.  
  83.  
  84. if(keycode == Input.Keys.UP){
  85. //camera.translate(0,-32);
  86. player.nextPlayerPosition.y += 32;
  87. player.direction = "UP";
  88. if(player.nextPlayerPosition.y > Gdx.graphics.getHeight() / 2){
  89. camera.translate(0,32);
  90. }
  91. }
  92.  
  93.  
  94. if(keycode == Input.Keys.DOWN){
  95. //camera.translate(0,32);
  96. player.nextPlayerPosition.y -= 32;
  97. player.direction = "DOWN";
  98. if(player.nextPlayerPosition.y < Gdx.graphics.getHeight() / 2){
  99. camera.translate(0,-32);
  100. }
  101. }
  102.  
  103.  
  104. if(keycode == Input.Keys.NUM_1)
  105. tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
  106. if(keycode == Input.Keys.NUM_2)
  107. tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
  108. return false;
  109. }
  110.  
  111. @Override
  112. public boolean keyTyped(char character) {
  113.  
  114. return false;
  115. }
  116.  
  117. @Override
  118. public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  119. if(screenX < Gdx.graphics.getWidth() / 2){
  120. camera.translate(-32,0);
  121. }
  122. if(screenX > Gdx.graphics.getWidth() / 2){
  123. camera.translate(32,0);
  124. }
  125. if(screenY < Gdx.graphics.getHeight() / 2){
  126. camera.translate(0,32);
  127. }
  128. if(screenY > Gdx.graphics.getHeight() / 2){
  129. camera.translate(0,-32);
  130. }
  131. return false;
  132. }
  133.  
  134. @Override
  135. public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  136. return false;
  137. }
  138.  
  139. @Override
  140. public boolean touchDragged(int screenX, int screenY, int pointer) {
  141. return false;
  142. }
  143.  
  144. @Override
  145. public boolean mouseMoved(int screenX, int screenY) {
  146. return false;
  147. }
  148.  
  149. @Override
  150. public boolean scrolled(int amount) {
  151. return false;
  152. }
  153.  
  154.  
  155. //custom methods
  156.  
  157. public void update(){
  158. if(!Map.isCollisionWithMapLayer(player.boundingBox)){
  159. player.currentPlayerPosition.x = player.nextPlayerPosition.x;
  160. player.currentPlayerPosition.y = player.nextPlayerPosition.y;
  161. }else{
  162. if(player.direction.equals("LEFT")){
  163. System.out.println("LEFT");
  164. player.currentPlayerPosition.x += 100;
  165. player.currentPlayerPosition.y = player.currentPlayerPosition.y;
  166. player.nextPlayerPosition.x = player.currentPlayerPosition.x;
  167. player.nextPlayerPosition.y = player.currentPlayerPosition.y;
  168. }
  169. else if(player.direction.equals("RIGHT")){
  170. System.out.println("RIGHT");
  171. player.currentPlayerPosition.x -= 100;
  172. player.currentPlayerPosition.y = player.currentPlayerPosition.y;
  173. player.nextPlayerPosition.x = player.currentPlayerPosition.x;
  174. player.nextPlayerPosition.y = player.currentPlayerPosition.y;
  175. }
  176. else if(player.direction.equals("UP")){
  177. System.out.println("UP");
  178. player.currentPlayerPosition.x = player.currentPlayerPosition.x;
  179. player.currentPlayerPosition.y -= 100;
  180. player.nextPlayerPosition.x = player.currentPlayerPosition.x;
  181. player.nextPlayerPosition.y = player.currentPlayerPosition.y;
  182. }
  183. else if(player.direction.equals("DOWN")){
  184. System.out.println("DOWN");
  185. player.currentPlayerPosition.x = player.currentPlayerPosition.x;
  186. player.currentPlayerPosition.y += 100;
  187. player.nextPlayerPosition.x = player.currentPlayerPosition.x;
  188. player.nextPlayerPosition.y = player.currentPlayerPosition.y;
  189. }
  190. else{
  191. System.out.println("FAIL");
  192. player.currentPlayerPosition.x = player.currentPlayerPosition.x;
  193. player.currentPlayerPosition.y = player.currentPlayerPosition.y;
  194. player.nextPlayerPosition.x = player.currentPlayerPosition.x;
  195. player.nextPlayerPosition.y = player.currentPlayerPosition.y;
  196. }
  197.  
  198. }
  199.  
  200. }
  201.  
  202. public void setCameraPosition(float x,float y){
  203. camera.position.set(x,y,0f);
  204. }
  205.  
  206.  
  207.  
  208.  
  209. }
  210.  
  211. public class Player {
  212.  
  213. static Sprite sprite;
  214. Texture skin;
  215. int x;
  216. int y;
  217. public Rectangle boundingBox;
  218. Vector2 nextPlayerPosition;
  219. Vector2 currentPlayerPosition;
  220. String direction;
  221.  
  222. public Player(int x, int y){
  223. this.skin = new Texture("playerShip.png");
  224. sprite = new Sprite(skin);
  225. this.nextPlayerPosition = new Vector2();
  226. this.currentPlayerPosition = new Vector2();
  227. this.x = x;
  228. this.y = y;
  229. currentPlayerPosition.x = 0;
  230. currentPlayerPosition.y = 0;
  231. sprite.setPosition(currentPlayerPosition.x, currentPlayerPosition.y);
  232. this.boundingBox = new Rectangle();
  233. boundingBox.set(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
  234.  
  235. }
  236.  
  237. public void update(){
  238. boundingBox.set(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
  239. }
  240.  
  241. public void render(SpriteBatch batch){
  242. sprite.setPosition(currentPlayerPosition.x, currentPlayerPosition.y);
  243. sprite.draw(batch);
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250. }
  251.  
  252. public class mapManager {
  253.  
  254. private final static String MAP_COLLISION_LAYER = "collisionLayer";
  255. public static MapLayer collisionLayer;
  256.  
  257.  
  258. public static void loadCollisionMap(TiledMap currentMap){
  259. collisionLayer = currentMap.getLayers().get(MAP_COLLISION_LAYER);
  260. }
  261.  
  262. public MapLayer getCollisionLayer(){
  263. loadCollisionMap(MMO.tiledMap);
  264. return collisionLayer;
  265. }
  266.  
  267. }
  268.  
  269. public static boolean isCollisionWithMapLayer(Rectangle boundingBox){
  270. MapLayer mapCollisionLayer = MMO.MAPcollision;
  271.  
  272. if(mapCollisionLayer == null){
  273. return false;
  274. }
  275.  
  276. Rectangle rectangle = null;
  277.  
  278. for(MapObject object: mapCollisionLayer.getObjects()){
  279. if(object instanceof RectangleMapObject){
  280. rectangle = ((RectangleMapObject)object).getRectangle();
  281. if(Player.sprite.getBoundingRectangle().overlaps(rectangle)){
  282. return true;
  283. }
  284. }
  285. }
  286. return false;
  287. }
  288.  
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement