Added code from old collector game
This commit is contained in:
95
core/src/com/mygdx/game/Collision.java
Normal file
95
core/src/com/mygdx/game/Collision.java
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package com.mygdx.game;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.maps.MapObject;
|
||||||
|
import com.badlogic.gdx.maps.MapObjects;
|
||||||
|
import com.badlogic.gdx.maps.objects.RectangleMapObject;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||||
|
import com.badlogic.gdx.math.Rectangle;
|
||||||
|
import com.badlogic.gdx.math.Vector2;
|
||||||
|
import com.badlogic.gdx.physics.box2d.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class Collision extends ApplicationAdapter {
|
||||||
|
private World world;
|
||||||
|
public Body body;
|
||||||
|
private CircleShape shape;
|
||||||
|
private Player player;
|
||||||
|
private float accumulator;
|
||||||
|
private float timeStep = 1/60f;
|
||||||
|
|
||||||
|
Collision(Player player) {
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void create(){
|
||||||
|
world = new World(new Vector2(0, 0), false);
|
||||||
|
|
||||||
|
BodyDef bdef = new BodyDef();
|
||||||
|
bdef.position.set(player.getX(), player.getY());
|
||||||
|
bdef.type = BodyDef.BodyType.DynamicBody;
|
||||||
|
bdef.fixedRotation = true;
|
||||||
|
body = world.createBody(bdef);
|
||||||
|
body.setLinearDamping(10f);
|
||||||
|
shape = new CircleShape();
|
||||||
|
shape.setRadius(1/3f);
|
||||||
|
body.createFixture(shape, 0.1f);
|
||||||
|
accumulator = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render(){
|
||||||
|
|
||||||
|
Environment.batch.begin();
|
||||||
|
Environment.batch.end();
|
||||||
|
player.setX(body.getPosition().x);
|
||||||
|
player.setY(body.getPosition().y);
|
||||||
|
Environment.getCam().position.set(player.getX(),player.getY(),0);
|
||||||
|
accumulator += Math.min(Gdx.graphics.getDeltaTime(), 0.25f);
|
||||||
|
while (accumulator>= timeStep) {
|
||||||
|
world.step(Gdx.graphics.getRawDeltaTime(), 8, 8);
|
||||||
|
accumulator -= timeStep;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(){
|
||||||
|
shape.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void buildBuildingsBodies(TiledMap tiledMap){
|
||||||
|
MapObjects objects = tiledMap.getLayers().get("Object Layer 1").getObjects();
|
||||||
|
for (MapObject object: objects) {
|
||||||
|
Rectangle rectangle = ((RectangleMapObject) object).getRectangle();
|
||||||
|
|
||||||
|
//create a dynamic within the world body (also can be KinematicBody or StaticBody
|
||||||
|
BodyDef bodyDef = new BodyDef();
|
||||||
|
bodyDef.type = BodyDef.BodyType.StaticBody;
|
||||||
|
Body body = world.createBody(bodyDef);
|
||||||
|
|
||||||
|
PolygonShape shape = new PolygonShape();
|
||||||
|
shape.setAsBox(0.4f,0.4f);
|
||||||
|
//create a fixture for each body from the shape
|
||||||
|
Fixture fixture = body.createFixture(shape,0.01f);
|
||||||
|
fixture.setFriction(0.1F);
|
||||||
|
|
||||||
|
Vector2 center = new Vector2();
|
||||||
|
rectangle.getCenter(center);
|
||||||
|
|
||||||
|
//Scaling the center of the body with the rest of the map
|
||||||
|
body.setTransform(center.scl(1/40f),0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
World getWorld() {
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
||||||
|
Body getBody() {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
core/src/com/mygdx/game/Dungeon.java
Normal file
12
core/src/com/mygdx/game/Dungeon.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.mygdx.game;
|
||||||
|
|
||||||
|
public class Dungeon extends Level{
|
||||||
|
int x1,x2,y1,y2 = 0;
|
||||||
|
|
||||||
|
public Dungeon(int x1, int x2, int y1, int y2) {
|
||||||
|
this.x1 = x1;
|
||||||
|
this.x2 = x2;
|
||||||
|
this.y1 = y1;
|
||||||
|
this.y2 = y2;
|
||||||
|
}
|
||||||
|
}
|
||||||
112
core/src/com/mygdx/game/Environment.java
Normal file
112
core/src/com/mygdx/game/Environment.java
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package com.mygdx.game;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Game;
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.graphics.FPSLogger;
|
||||||
|
import com.badlogic.gdx.graphics.GL20;
|
||||||
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||||
|
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
|
||||||
|
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
|
||||||
|
|
||||||
|
public class Environment extends Game {
|
||||||
|
|
||||||
|
static SpriteBatch batch;
|
||||||
|
|
||||||
|
private TiledMap map;
|
||||||
|
private TiledMapRenderer tiledMapRenderer;
|
||||||
|
private float elapsedTime = 0f;
|
||||||
|
private float time = 0;
|
||||||
|
private float tick = 1/60f;
|
||||||
|
private int maxUpdatesPerFrame = 1;
|
||||||
|
private int tileSize = 32;
|
||||||
|
private Collision collision;
|
||||||
|
private Box2DDebugRenderer box2DDebugRenderer;
|
||||||
|
public static float VIEWPORT_HEIGHT = 9/2f;
|
||||||
|
public static float VIEWPORT_WIDTH = 16/2f;
|
||||||
|
public static OrthographicCamera cam;
|
||||||
|
private static Player player;
|
||||||
|
private FPSLogger fpsLogger;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void create () {
|
||||||
|
//Instantiating the player character with their sprite and location
|
||||||
|
player = new Player();
|
||||||
|
collision = new Collision(player);
|
||||||
|
collision.create();
|
||||||
|
|
||||||
|
cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
|
||||||
|
box2DDebugRenderer = new Box2DDebugRenderer();
|
||||||
|
|
||||||
|
map = new TmxMapLoader().load(String.valueOf(Gdx.files.internal("Tilesets/"+ "island" +".tmx")));
|
||||||
|
tiledMapRenderer = new OrthogonalTiledMapRenderer(map,1/40f);
|
||||||
|
|
||||||
|
cam.setToOrtho(false,VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
|
||||||
|
cam.zoom = 2.5f;
|
||||||
|
|
||||||
|
fpsLogger = new FPSLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render () {
|
||||||
|
Gdx.gl.glClearColor(0, 0, 0, 0);
|
||||||
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
elapsedTime += Gdx.graphics.getDeltaTime();
|
||||||
|
batch = new SpriteBatch();
|
||||||
|
|
||||||
|
batch.setProjectionMatrix(getCam().combined);
|
||||||
|
tiledMapRenderer.setView(getCam());
|
||||||
|
tiledMapRenderer.render();
|
||||||
|
collision.render();
|
||||||
|
|
||||||
|
batch.begin();
|
||||||
|
batch.draw(player.getAnimation().getKeyFrame(elapsedTime, true), player.getX()-0.5f, player.getY()-0.5f,1,1);
|
||||||
|
|
||||||
|
tick =1/60f;
|
||||||
|
batch.end();
|
||||||
|
|
||||||
|
//box2DDebugRenderer.render(collision.getWorld(),cam.combined);
|
||||||
|
getCam().update();
|
||||||
|
|
||||||
|
|
||||||
|
//collision.buildBuildingsBodies(map);
|
||||||
|
|
||||||
|
|
||||||
|
time += Gdx.graphics. getDeltaTime();
|
||||||
|
Input_Handler input_handler = new Input_Handler(collision,player);
|
||||||
|
Gdx.input.setInputProcessor(input_handler);
|
||||||
|
int updatesThisFrame = 0;
|
||||||
|
while (time >= tick && updatesThisFrame < maxUpdatesPerFrame) {
|
||||||
|
updatesThisFrame++;
|
||||||
|
time -= tick;
|
||||||
|
}
|
||||||
|
System.gc();
|
||||||
|
fpsLogger.log();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Clears the screen of the following textures
|
||||||
|
@Override
|
||||||
|
public void dispose() {
|
||||||
|
map.dispose();
|
||||||
|
batch.dispose();
|
||||||
|
player.getTextureAtlas().dispose();
|
||||||
|
collision.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OrthographicCamera getCam() {
|
||||||
|
return cam;
|
||||||
|
}
|
||||||
|
|
||||||
|
void zoomCam(float zoom) {
|
||||||
|
cam.zoom -= zoom;
|
||||||
|
}
|
||||||
|
|
||||||
|
float getZoomCam(){
|
||||||
|
return cam.zoom;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
122
core/src/com/mygdx/game/Input_Handler.java
Normal file
122
core/src/com/mygdx/game/Input_Handler.java
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package com.mygdx.game;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.Input;
|
||||||
|
import com.badlogic.gdx.InputProcessor;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||||
|
|
||||||
|
public class Input_Handler implements InputProcessor {
|
||||||
|
|
||||||
|
private float playerSpeed = 0.04f;
|
||||||
|
private Player player;
|
||||||
|
private Animation<TextureAtlas.AtlasRegion> animation;
|
||||||
|
|
||||||
|
Input_Handler(Collision collision, Player player) {
|
||||||
|
this.player = player;
|
||||||
|
Environment environment = (Environment) Gdx.app.getApplicationListener();
|
||||||
|
|
||||||
|
//Zooming Keybinds
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.EQUALS) && environment.getZoomCam() >= 1) {
|
||||||
|
environment.zoomCam(0.1f);
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.MINUS) && environment.getZoomCam() <= 3) {
|
||||||
|
environment.zoomCam(-0.1f);
|
||||||
|
}
|
||||||
|
|
||||||
|
//4 Cardinal directions
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
|
||||||
|
up(collision);
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
|
||||||
|
right(collision);
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
|
||||||
|
down(collision);
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
|
||||||
|
left(collision);
|
||||||
|
}
|
||||||
|
//4 Diagonal directions
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
|
||||||
|
directionAnimation("UpRight");
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.UP) && Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
|
||||||
|
directionAnimation("UpLeft");
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
|
||||||
|
directionAnimation("DownRight");
|
||||||
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
|
||||||
|
directionAnimation("DownLeft");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void up(Collision collision) {
|
||||||
|
collision.getBody().applyLinearImpulse(0, playerSpeed,collision.getBody().getPosition().x, collision.getBody().getPosition().y, false);
|
||||||
|
directionAnimation("Up");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void down(Collision collision) {
|
||||||
|
collision.getBody().applyLinearImpulse(0, -playerSpeed, collision.getBody().getPosition().x, collision.getBody().getPosition().y, false);
|
||||||
|
directionAnimation("Down");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void right(Collision collision) {
|
||||||
|
collision.getBody().applyLinearImpulse(playerSpeed, 0, collision.getBody().getPosition().x, collision.getBody().getPosition().y, false);
|
||||||
|
directionAnimation("Right");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void left(Collision collision) {
|
||||||
|
collision.getBody().applyLinearImpulse(-playerSpeed, 0, collision.getBody().getPosition().x, collision.getBody().getPosition().y, false);
|
||||||
|
directionAnimation("Left");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyDown(int keycode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyUp(int keycode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean keyTyped(char character) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void directionAnimation(String direction) {
|
||||||
|
player.setAnimation( new Animation<>(1/4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + direction)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
34
core/src/com/mygdx/game/Level.java
Normal file
34
core/src/com/mygdx/game/Level.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package com.mygdx.game;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||||
|
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
|
||||||
|
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
|
||||||
|
|
||||||
|
public class Level extends Environment {
|
||||||
|
|
||||||
|
private OrthogonalTiledMapRenderer tiledMapRenderer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void create(){
|
||||||
|
super.create();
|
||||||
|
String nameOfMap = "island";
|
||||||
|
TiledMap map = new TmxMapLoader().load(String.valueOf(Gdx.files.internal("Tilesets/" + nameOfMap + ".tmx")));
|
||||||
|
tiledMapRenderer = new OrthogonalTiledMapRenderer(map,1/40f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render(){
|
||||||
|
super.render();
|
||||||
|
tiledMapRenderer.setView(cam);
|
||||||
|
tiledMapRenderer.render();
|
||||||
|
cam.update();
|
||||||
|
batch.setProjectionMatrix(cam.combined);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dispose(){
|
||||||
|
super.dispose();
|
||||||
|
tiledMapRenderer.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
81
core/src/com/mygdx/game/Player.java
Normal file
81
core/src/com/mygdx/game/Player.java
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package com.mygdx.game;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||||
|
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
|
||||||
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
|
||||||
|
public class Player {
|
||||||
|
private Array<TextureAtlas.AtlasRegion> keyFrames;
|
||||||
|
private float x, y;
|
||||||
|
private String spriteName;
|
||||||
|
private TextureAtlas textureAtlas;
|
||||||
|
private Animation<TextureAtlas.AtlasRegion> animation;
|
||||||
|
private float frameDuration;
|
||||||
|
|
||||||
|
Player() {
|
||||||
|
this.x = 10;
|
||||||
|
this.y = 10;
|
||||||
|
this.frameDuration = 1/4f;
|
||||||
|
this.spriteName = "man";
|
||||||
|
this.textureAtlas = new TextureAtlas("NewSprites/" + spriteName + ".atlas");
|
||||||
|
this.keyFrames = textureAtlas.findRegions(spriteName + "_Down");
|
||||||
|
this.animation = new Animation<>(frameDuration, keyFrames);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setX(float x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setY(float y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getSpriteName() {
|
||||||
|
return spriteName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSpriteName(String tempSpriteName) {
|
||||||
|
spriteName = tempSpriteName;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureAtlas getTextureAtlas() {
|
||||||
|
return textureAtlas;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Animation<TextureAtlas.AtlasRegion> getAnimation() {
|
||||||
|
return animation;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setAnimation(Animation<TextureAtlas.AtlasRegion> animation) {
|
||||||
|
this.animation = animation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getFrameDuration() {
|
||||||
|
return frameDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrameDuration(float frameDuration) {
|
||||||
|
this.frameDuration = frameDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Array<TextureAtlas.AtlasRegion> getKeyFrames() {
|
||||||
|
return keyFrames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKeyFrames(Array<TextureAtlas.AtlasRegion> keyFrames) {
|
||||||
|
this.keyFrames = keyFrames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTextureAtlas(TextureAtlas textureAtlas) {
|
||||||
|
this.textureAtlas = textureAtlas;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user