Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kontra.main;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
- import com.badlogic.gdx.math.Vector2;
- public class Player extends Entity implements InputProcessor{
- boolean bucket = true;
- Kontra kontra;
- Window window;
- private TiledMapGame tilemap;
- private static int rowNumber = 32;
- private static int ColumnNumber = 32;
- private static int randomstuff = 0;
- private static Texture texture = new Texture(Gdx.files.internal("Images/guys.png"));
- private static TextureRegion regions= new TextureRegion(texture, 0, 0, 32, 32);
- private static TextureRegion CharacterView = regions;
- float oldX, oldY, tileWidth, tileHeight;
- private TiledMapTileLayer collisionLayer;
- boolean collisionX = false, collisionY = false;
- Vector2 velocityVEC = new Vector2();
- float speed = 60*2 ;
- float gravity2 = 60*1.8f;
- boolean canJump = true;
- public Player(float x, float y, TiledMapTileLayer tiledMapTileLayer) {
- super(x, y, new TextureRegion(texture, 0, 0, 32, 32));
- this.collisionLayer = tiledMapTileLayer;
- }
- @Override
- public void draw(SpriteBatch batch) {
- oldX = x;
- oldY = y;
- tileWidth = collisionLayer.getTileWidth();
- tileHeight = collisionLayer.getTileHeight();
- update(Gdx.graphics.getDeltaTime());
- super.draw(batch);
- }
- public void update(float delta){
- //apply gravity
- velocityVEC.y -= gravity2 * delta;
- //clamp gravity
- if(velocityVEC.y > speed){
- velocityVEC.y = speed;
- }else{
- velocityVEC.y = -speed;
- }
- // MOVE ON X
- x = x + velocityVEC.x * delta;
- System.out.println(velocityVEC.x);
- System.out.println(x);
- if(velocityVEC.x < 0){
- //top left
- collisionX = collisionLayer.getCell((int)(x / tileWidth), (int)((y+regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //middle left
- if(!collisionX)
- collisionX = collisionLayer.getCell((int)(x / tileWidth), (int)((y+regions.getRegionHeight() / 2) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //bottom left
- if(!collisionX){
- collisionX = collisionLayer.getCell((int)(x / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- System.out.println("tes");
- }
- }else if(velocityVEC.x > 0){
- //top right
- collisionX = collisionLayer.getCell((int)((x + regions.getRegionWidth()) / tileWidth), (int)((y+regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //middle right
- if(!collisionX){
- collisionX = collisionLayer.getCell((int)((x + regions.getRegionWidth()) / tileWidth), (int)((y+regions.getRegionHeight()/2) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- }
- //bottom right
- if(!collisionX){
- collisionX = collisionLayer.getCell((int)((x + regions.getRegionWidth()) / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- }
- }
- if(collisionX){
- x = oldX;
- velocityVEC.x = 0;
- }
- // MOVE ON Y
- y = y + velocityVEC.y * delta;
- if(velocityVEC.y < 0){// going down
- //bottom left
- collisionY = collisionLayer.getCell((int)(x / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //bottom left
- if(!collisionY)
- collisionY = collisionLayer.getCell((int)((x + regions.getRegionWidth() / 2) / tileWidth), (int)(y / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //bottom right
- if(!collisionY)
- collisionY = collisionLayer.getCell((int)((x + regions.getRegionWidth()) / tileWidth), (int)((y) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- canJump = collisionY;
- }else if(velocityVEC.y > 0){// going up
- //top left
- collisionY = collisionLayer.getCell((int)(x / tileWidth), (int)((y + regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //top left
- if(!collisionY)
- collisionY = collisionLayer.getCell((int)(((x + regions.getRegionWidth()) / 2) / tileWidth), (int)((y + regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- //top right
- if(!collisionY)
- collisionY = collisionLayer.getCell((int)((x + regions.getRegionWidth()) / tileWidth), (int)((y + regions.getRegionHeight()) / tileHeight) ).getTile().getProperties().containsKey("blocked");
- }
- if(collisionY){
- y = oldY;
- velocityVEC.y = 0;
- }
- }
- @Override
- public boolean keyDown(int keycode) {
- switch(keycode){
- case Keys.W:
- //if(canJump){
- velocityVEC.y = speed;
- canJump = false;
- //}
- break;
- case Keys.A:
- velocityVEC.x = -speed;
- break;
- case Keys.D:
- velocityVEC.x = speed;
- break;
- }
- return true;
- }
- @Override
- public boolean keyUp(int keycode) {
- switch(keycode){
- case Keys.W:
- break;
- case Keys.A:
- velocityVEC.x = 0;
- break;
- case Keys.D:
- velocityVEC.x = 0;
- break;
- }
- return true;
- }
- @Override
- public boolean keyTyped(char arg0) {return false;}
- @Override
- public boolean mouseMoved(int arg0, int arg1) {return false;}
- @Override
- public boolean scrolled(int arg0) {return false;}
- @Override
- public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {return false;}
- @Override
- public boolean touchDragged(int arg0, int arg1, int arg2) {return false;}
- @Override
- public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {return false;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement