Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MMO extends Game implements InputProcessor {
- SpriteBatch batch;
- Texture img;
- static TiledMap tiledMap;
- OrthographicCamera camera;
- TiledMapRenderer tiledMapRenderer;
- Player player;
- static mapManager mapManager;
- static MapLayer MAPcollision;
- @Override
- public void create () {
- mapManager = new mapManager();
- batch = new SpriteBatch();
- float w = Gdx.graphics.getWidth();
- float h = Gdx.graphics.getHeight();
- camera = new OrthographicCamera();
- camera.setToOrtho(false,w,h);
- camera.update();
- tiledMap = new TmxMapLoader().load("Maps/base.tmx");
- tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
- player = new Player(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
- Gdx.input.setInputProcessor(this);
- }
- @Override
- public void render () {
- Gdx.gl.glClearColor(1, 0, 0, 1);
- Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- MAPcollision = mapManager.getCollisionLayer();
- camera.update();
- player.update();
- update();
- tiledMapRenderer.setView(camera);
- tiledMapRenderer.render();
- camera.update();
- batch.setProjectionMatrix(camera.combined);
- batch.begin();
- player.render(batch);
- batch.end();
- camera.update();
- }
- @Override
- public boolean keyDown(int keycode){
- return false;
- }
- @Override
- public boolean keyUp(int keycode){
- if(keycode == Input.Keys.LEFT){
- //camera.translate(-32,0);
- player.nextPlayerPosition.x -= 32;
- player.direction = "LEFT";
- if(player.nextPlayerPosition.x < Gdx.graphics.getWidth() / 2){
- camera.translate(-32,0);
- }
- }
- if(keycode == Input.Keys.RIGHT){
- //camera.translate(32,0);
- player.nextPlayerPosition.x += 32;
- player.direction = "RIGHT";
- if(player.nextPlayerPosition.x > Gdx.graphics.getWidth() / 2){
- camera.translate(32,0);
- }
- }
- if(keycode == Input.Keys.UP){
- //camera.translate(0,-32);
- player.nextPlayerPosition.y += 32;
- player.direction = "UP";
- if(player.nextPlayerPosition.y > Gdx.graphics.getHeight() / 2){
- camera.translate(0,32);
- }
- }
- if(keycode == Input.Keys.DOWN){
- //camera.translate(0,32);
- player.nextPlayerPosition.y -= 32;
- player.direction = "DOWN";
- if(player.nextPlayerPosition.y < Gdx.graphics.getHeight() / 2){
- camera.translate(0,-32);
- }
- }
- if(keycode == Input.Keys.NUM_1)
- tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
- if(keycode == Input.Keys.NUM_2)
- tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
- return false;
- }
- @Override
- public boolean keyTyped(char character) {
- return false;
- }
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- if(screenX < Gdx.graphics.getWidth() / 2){
- camera.translate(-32,0);
- }
- if(screenX > Gdx.graphics.getWidth() / 2){
- camera.translate(32,0);
- }
- if(screenY < Gdx.graphics.getHeight() / 2){
- camera.translate(0,32);
- }
- if(screenY > Gdx.graphics.getHeight() / 2){
- camera.translate(0,-32);
- }
- return false;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- return false;
- }
- @Override
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- return false;
- }
- @Override
- public boolean mouseMoved(int screenX, int screenY) {
- return false;
- }
- @Override
- public boolean scrolled(int amount) {
- return false;
- }
- //custom methods
- public void update(){
- if(!Map.isCollisionWithMapLayer(player.boundingBox)){
- player.currentPlayerPosition.x = player.nextPlayerPosition.x;
- player.currentPlayerPosition.y = player.nextPlayerPosition.y;
- }else{
- if(player.direction.equals("LEFT")){
- System.out.println("LEFT");
- player.currentPlayerPosition.x += 100;
- player.currentPlayerPosition.y = player.currentPlayerPosition.y;
- player.nextPlayerPosition.x = player.currentPlayerPosition.x;
- player.nextPlayerPosition.y = player.currentPlayerPosition.y;
- }
- else if(player.direction.equals("RIGHT")){
- System.out.println("RIGHT");
- player.currentPlayerPosition.x -= 100;
- player.currentPlayerPosition.y = player.currentPlayerPosition.y;
- player.nextPlayerPosition.x = player.currentPlayerPosition.x;
- player.nextPlayerPosition.y = player.currentPlayerPosition.y;
- }
- else if(player.direction.equals("UP")){
- System.out.println("UP");
- player.currentPlayerPosition.x = player.currentPlayerPosition.x;
- player.currentPlayerPosition.y -= 100;
- player.nextPlayerPosition.x = player.currentPlayerPosition.x;
- player.nextPlayerPosition.y = player.currentPlayerPosition.y;
- }
- else if(player.direction.equals("DOWN")){
- System.out.println("DOWN");
- player.currentPlayerPosition.x = player.currentPlayerPosition.x;
- player.currentPlayerPosition.y += 100;
- player.nextPlayerPosition.x = player.currentPlayerPosition.x;
- player.nextPlayerPosition.y = player.currentPlayerPosition.y;
- }
- else{
- System.out.println("FAIL");
- player.currentPlayerPosition.x = player.currentPlayerPosition.x;
- player.currentPlayerPosition.y = player.currentPlayerPosition.y;
- player.nextPlayerPosition.x = player.currentPlayerPosition.x;
- player.nextPlayerPosition.y = player.currentPlayerPosition.y;
- }
- }
- }
- public void setCameraPosition(float x,float y){
- camera.position.set(x,y,0f);
- }
- }
- public class Player {
- static Sprite sprite;
- Texture skin;
- int x;
- int y;
- public Rectangle boundingBox;
- Vector2 nextPlayerPosition;
- Vector2 currentPlayerPosition;
- String direction;
- public Player(int x, int y){
- this.skin = new Texture("playerShip.png");
- sprite = new Sprite(skin);
- this.nextPlayerPosition = new Vector2();
- this.currentPlayerPosition = new Vector2();
- this.x = x;
- this.y = y;
- currentPlayerPosition.x = 0;
- currentPlayerPosition.y = 0;
- sprite.setPosition(currentPlayerPosition.x, currentPlayerPosition.y);
- this.boundingBox = new Rectangle();
- boundingBox.set(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
- }
- public void update(){
- boundingBox.set(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
- }
- public void render(SpriteBatch batch){
- sprite.setPosition(currentPlayerPosition.x, currentPlayerPosition.y);
- sprite.draw(batch);
- }
- }
- public class mapManager {
- private final static String MAP_COLLISION_LAYER = "collisionLayer";
- public static MapLayer collisionLayer;
- public static void loadCollisionMap(TiledMap currentMap){
- collisionLayer = currentMap.getLayers().get(MAP_COLLISION_LAYER);
- }
- public MapLayer getCollisionLayer(){
- loadCollisionMap(MMO.tiledMap);
- return collisionLayer;
- }
- }
- public static boolean isCollisionWithMapLayer(Rectangle boundingBox){
- MapLayer mapCollisionLayer = MMO.MAPcollision;
- if(mapCollisionLayer == null){
- return false;
- }
- Rectangle rectangle = null;
- for(MapObject object: mapCollisionLayer.getObjects()){
- if(object instanceof RectangleMapObject){
- rectangle = ((RectangleMapObject)object).getRectangle();
- if(Player.sprite.getBoundingRectangle().overlaps(rectangle)){
- return true;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement