lets go mario

This commit is contained in:
Optioneel
2023-07-06 22:51:32 +02:00
commit 0bd5b7e109
37 changed files with 1729 additions and 0 deletions

6
core/build.gradle Normal file
View File

@@ -0,0 +1,6 @@
sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
eclipse.project.name = appName + "-core"

View File

@@ -0,0 +1,75 @@
package com.jan.Collision;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.jan.entity.GameObject;
import com.jan.game.Display;
import com.badlogic.gdx.physics.box2d.PolygonShape;
public class Colider {
public static final String BODY_PROPERTY = "BodyType";
public static final String BODY_COLLIDER = "Colider";
private Body body;
private GameObject target;
private String bodyType;
private String colider;
private boolean isSolid = true;
public Colider(GameObject target, String bodyType, String colider, boolean isSolid){
this(target, bodyType, colider);
this.isSolid = isSolid;
}
public Colider(GameObject target, String bodyType, String colider){
this.target = target;
this.bodyType = bodyType;
this.colider = colider;
}
public void loadBody(World world){
BodyDef bodyDef = new BodyDef();
bodyDef.type = getType(bodyType);
float scale = Display.getScale();
bodyDef.position.set((target.getX() + (target.getWidth() /2)) / scale, (target.getY() + (target.getHeight() /2)) /scale);
body = world.createBody(bodyDef);
body.setUserData(this);
getColider();
}
public BodyType getType(String bodyType){
switch(bodyType){
case "Dynamic": return BodyType.DynamicBody;
case "Static": return BodyType.StaticBody;
case "Kinematic": return BodyType.KinematicBody;
}
return BodyType.StaticBody;
}
private void getColider(){
float scale = Display.getScale();
if(colider.equals("auto")){
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox((target.getWidth() / scale) /2, (target.getHeight() / scale) /2);
FixtureDef fixture = new FixtureDef();
fixture.shape = groundBox;
fixture.isSensor = !isSolid;
body.createFixture(fixture);
groundBox.dispose();
}
}
public void updatePos(){
Vector2 pos = body.getPosition();
float scale = Display.getScale();
target.setX((pos.x * scale) - target.getWidth() / 2);
target.setY((pos.y * scale) - target.getHeight() / 2);
}
public Body getBody(){ return body; }
public GameObject getEntity(){return target;}
}

View File

@@ -0,0 +1,5 @@
package com.jan.Collision;
public interface ColiderInterface {
public Colider getColider();
}

View File

@@ -0,0 +1,149 @@
package com.jan.Collision;
import java.util.ArrayList;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
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.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.jan.Collision.CollisionEvent.COLLISION_STATE;
import com.jan.game.Display;
import com.jan.game.SceneManager;
import com.jan.scene.Entity;
public class Collision {
public static final String GEOMETRY_LAYER = "Geometry";
private String[] collisionTypes = {"Actor", "Trigger"};
private Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
private float accumulator = 0;
private World physicsWorld;
private ArrayList<ColiderInterface> colliders;
public Collision(TiledMap world){
debugRenderer = new Box2DDebugRenderer();
physicsWorld = new World(new Vector2(0, -98f), true);
MapLayer geometryLayer = world.getLayers().get(GEOMETRY_LAYER);
GeometryLoader loader = new GeometryLoader(geometryLayer);
physicsWorld = loader.getWorld();
loader = null;
colliders = new ArrayList<>();
physicsWorld.setContactListener(new ListenerClass());
}
public void loadColiders(ArrayList<Entity> renderables){
for(int i = 0; i < renderables.size(); i++){
if(hasCollider(renderables.get(i).getEntityClass())){
ColiderInterface actor = (ColiderInterface) renderables.get(i);
actor.getColider().loadBody(physicsWorld);
colliders.add(actor);
}
}
}
private boolean hasCollider(String entityType){
for(int i = 0; i < collisionTypes.length; i++){
if(entityType.equals(collisionTypes[i]))
return true;
}
return false;
}
public void update(float deltaTime){
float frameTime = Math.min(deltaTime, 0.25f);
accumulator += frameTime;
while (accumulator >= 1/60f) {
physicsWorld.step(1/60f, 6, 2);
accumulator -= 1/60f;
}
updateBody();
debugRenderer.render(physicsWorld, Display.getCamera().combined);
}
private void updateBody(){
for(int i = 0; i < colliders.size(); i++)
colliders.get(i).getColider().updatePos();
}
}
class GeometryLoader{
private World collWorld;
public GeometryLoader(MapLayer collisionData){
collWorld = new World(new Vector2(0, -98f), true);
for(int i = 0; i < collisionData.getObjects().getCount(); i++)
loadBody(collisionData.getObjects().get(i));
}
private void loadBody(MapObject curr){
if(curr instanceof RectangleMapObject){
RectangleMapObject a = (RectangleMapObject)curr;
loadRectangle(a.getRectangle());
}
}
private void loadRectangle(Rectangle objRect){
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;
float bodyX = (objRect.getX() + (objRect.getWidth() / 2)) / Display.getScale();
float bodyY = (objRect.getY() + (objRect.getHeight() / 2)) / Display.getScale();
bodyDef.position.set(bodyX, bodyY);
Body groundBody = collWorld.createBody(bodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox((objRect.width /2) / Display.getScale(), (objRect.height /2) / Display.getScale());
groundBody.createFixture(groundBox, 0.0f);
groundBody.setUserData(0);
groundBox.dispose();
}
public World getWorld(){ return collWorld; }
}
class ListenerClass implements ContactListener {
public void beginContact(Contact contact){
determenCollision(contact.getFixtureA().getBody().getUserData(), contact.getFixtureB().getBody().getUserData(), COLLISION_STATE.BEGIN, contact);
}
public void endContact(Contact contact) {
determenCollision(contact.getFixtureA().getBody().getUserData(), contact.getFixtureB().getBody().getUserData(), COLLISION_STATE.END, contact);
}
public void preSolve(Contact contact, Manifold oldManifold) {}
public void postSolve(Contact contact, ContactImpulse impulse) {}
public void determenCollision(Object targetA, Object targetB, COLLISION_STATE state, Contact contact){
if(regulairCollision(targetA, targetB, state, contact))
return;
ObjectCollision(targetA, targetB, state, contact);
}
public boolean regulairCollision(Object targetA, Object targetB, COLLISION_STATE state, Contact contact){
if(targetA instanceof Integer && targetB instanceof Colider){
SceneManager.fireEvent(new CollisionEvent((int) targetA, (Colider) targetB, state, contact));
return true;
}
return false;
}
public void ObjectCollision(Object targetA, Object targetB, COLLISION_STATE state, Contact contact){
if(targetA instanceof Colider && targetB instanceof Colider){
SceneManager.fireEvent(new CollisionEvent((Colider) targetA, (Colider) targetB, state, contact));
}
}
}

View File

@@ -0,0 +1,47 @@
package com.jan.Collision;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.jan.scene.Entity;
public class CollisionEvent {
public enum COLLISION_STATE {BEGIN, END};
public enum COLLISION_TYPE {TERAIN, OBJECT};
private Colider entityA;
private Colider entityB;
private COLLISION_TYPE type;
private COLLISION_STATE state;
private Contact contact;
public CollisionEvent(Colider entityA, Colider entityB, COLLISION_STATE state, Contact contact){
this.entityA = entityA;
this.entityB = entityB;
this.state = state;
this.type = COLLISION_TYPE.OBJECT;
this.contact = contact;
}
public CollisionEvent(int entityA, Colider entityB, COLLISION_STATE state, Contact contact){
this.entityB = entityB;
this.state = state;
this.type = COLLISION_TYPE.TERAIN;
this.contact = contact;
}
public Entity getEntityA(){ return entityA.getEntity(); }
public Body getBodyA(){ return entityA.getBody(); }
public Entity getEntityB(){ return entityB.getEntity(); }
public Body getBodyB(){ return entityB.getBody(); }
public COLLISION_TYPE getType(){ return type; }
public COLLISION_STATE getState(){ return state; }
public Contact getContact(){ return contact; }
}

View File

@@ -0,0 +1,22 @@
package com.jan.entity;
import com.badlogic.gdx.maps.MapObject;
import com.jan.Collision.Colider;
import com.jan.Collision.ColiderInterface;
public class Actor extends Animator implements ColiderInterface{
private Colider colider;
public Actor(MapObject mapObject) {
super(mapObject);
String bodyType = (String)loadProperty(Colider.BODY_PROPERTY);
String coliderType = (String)loadProperty(Colider.BODY_COLLIDER);
colider = new Colider(this, bodyType, coliderType);
}
public Colider getColider(){
return colider;
}
}

View File

@@ -0,0 +1,111 @@
package com.jan.entity;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
public class Animator extends Renderable {
public static final String ANIMATOR_PROPERTY = "Animator";
private ArrayList<AnimationClip> animations;
private AnimationClip currClip;
private float stateTime;
public Animator(MapObject mapObject) {
super(mapObject);
animations = new ArrayList<>();
String fileLocation = "animations/"+(String)loadProperty(ANIMATOR_PROPERTY);
Json json = new Json();
ArrayList<JsonValue> list = json.fromJson(ArrayList.class, Gdx.files.internal(fileLocation));
for (JsonValue v : list)
animations.add(new AnimationClip(json.readValue(AnimationData.class, v)));
currClip = animations.get(0);
stateTime = 0f;
}
public void update(){//override
stateTime += Gdx.graphics.getDeltaTime();
this.setTexture(currClip.getKeyFrame(stateTime));
}
public void doAnimation(String animation, PlayMode mode){
doAnimation(animation);
setAnimationState(mode);
}
public void doAnimation(String animation){
for(int i = 0; i < animations.size(); i++){
if(animations.get(i).getName().equals(animation))
currClip = animations.get(i);
}
}
public void setAnimationState(PlayMode mode){ currClip.setPlayMode(mode); }
}
class AnimationClip extends AnimationData{
private Texture animSheet;
private Animation<TextureRegion> animation;
public AnimationClip(AnimationData data){
this.name = data.name;
this.path = data.path;
this.rows = data.rows;
this.collumns = data.collumns;
this.speed = data.speed;
animSheet = new Texture(Gdx.files.internal("images/"+path));
loadAnimation();
animation.setPlayMode(PlayMode.LOOP);
}
private void loadAnimation(){
TextureRegion[][] tmp = TextureRegion.split(animSheet, animSheet.getWidth() / collumns, animSheet.getHeight() / rows);
TextureRegion[] animationFrames = new TextureRegion[collumns * rows];
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < collumns; j++)
animationFrames[index++] = tmp[i][j];
}
animation = new Animation<TextureRegion>(speed, animationFrames);
}
public void setPlayMode(PlayMode playMode){ animation.setPlayMode(playMode); }
public TextureRegion getKeyFrame(float deltaTime){ return animation.getKeyFrame(deltaTime); }
}
class AnimationData{
protected String name;
protected String path;
protected int rows;
protected int collumns;
protected float speed;
public AnimationData(String name, String path, int rows, int collumns, float speed){
this.name = name;
this.path = path;
this.rows = rows;
this.collumns = collumns;
this.speed = speed;
}
public AnimationData(){}
public String getName(){ return name; }
public String getPath(){ return path; }
public int getRows(){ return rows; }
public int getCollumns(){ return collumns; }
public float getSpeed(){ return speed; }
}

View File

@@ -0,0 +1,33 @@
package com.jan.entity;
import com.badlogic.gdx.maps.MapObject;
import com.jan.scene.Entity;
public class GameObject extends Entity {
private float x;
private float y;
private float width;
private float height;
public GameObject(MapObject mapObject) {
super(mapObject);
loadProperties();
}
private void loadProperties() {
x = (float)loadProperty("x");
y = (float)loadProperty("y");
width = (float)loadProperty("width");
height = (float)loadProperty("height");
}
public float getX(){ return x; }
public float getY(){ return y; }
public void setX(float x){ this.x = x; }
public void setY(float y){ this.y = y; }
public float getWidth(){ return width; }
public float getHeight(){ return height; }
public void setWidth(float width){ this.width = width;}
public void setHeight(float height){ this.height = height; }
}

View File

@@ -0,0 +1,27 @@
package com.jan.entity;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.TextureMapObject;
public class Renderable extends GameObject{
private TextureRegion texture;
public Renderable(MapObject mapObject) {
super(mapObject);
if(mapObject instanceof TextureMapObject){
TextureMapObject textureObj = (TextureMapObject)mapObject;
texture = textureObj.getTextureRegion();
}
}
public void setTexture(TextureRegion texture){
this.texture = texture;
}
public TextureRegion getTexture(){
return texture;
}
}

View File

@@ -0,0 +1,22 @@
package com.jan.entity;
import com.badlogic.gdx.maps.MapObject;
import com.jan.Collision.Colider;
import com.jan.Collision.ColiderInterface;
public class Trigger extends GameObject implements ColiderInterface{
private Colider colider;
public Trigger(MapObject mapObject) {
super(mapObject);
String bodyType = (String)loadProperty(Colider.BODY_PROPERTY);
String coliderType = (String)loadProperty(Colider.BODY_COLLIDER);
boolean isSolid = (boolean)loadProperty("Solid");
colider = new Colider(this, bodyType, coliderType, isSolid);
}
public Colider getColider(){
return colider;
}
}

View File

@@ -0,0 +1,46 @@
package com.jan.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class Display {
private static boolean isInitialized = false;
private static ScreenViewport viewPort;
private static OrthographicCamera mainCamera;
public static int DEFAULT_RESOLUTION_WIDTH = 960;
public static int DEFAULT_RESOLUTION_HEIGHT = 640;
private static float tileSize = 0;
public static void initialize(){
if(isInitialized == false){
Gdx.graphics.setResizable(false);
viewPort = new ScreenViewport();
viewPort.setUnitsPerPixel(0.03125f);
setResolution(DEFAULT_RESOLUTION_WIDTH, DEFAULT_RESOLUTION_HEIGHT);
isInitialized = true;
}
}
public static void setResolution(int width, int height){
Gdx.graphics.setWindowedMode(width, height);
viewPort.update(width, height);
useCamera((OrthographicCamera)viewPort.getCamera());
}
public static void useCamera(OrthographicCamera camera){
viewPort.setCamera(camera);
mainCamera = (OrthographicCamera) viewPort.getCamera();
}
public static void update(){
mainCamera.update();
}
public static OrthographicCamera getCamera(){ return mainCamera; }
public static void setScale(float scale){ tileSize = scale; }
public static float getScale(){ return tileSize; }
}

View File

@@ -0,0 +1,34 @@
package com.jan.game;
import com.jan.Collision.CollisionEvent;
import com.jan.entity.Trigger;
import com.jan.scene.Scene;
public abstract class Game {
protected Scene currScene;
public void create(Scene scene){
currScene = scene;
}
public abstract void start();
public abstract void update();
public void catchEvent(CollisionEvent event){
switch(event.getType()){
case OBJECT: findType(event);break;
case TERAIN: regulairCollision(event); break;
}
}
private void findType(CollisionEvent event){
if(event.getEntityA() instanceof Trigger){
return;
}
objectCollision(event);
}
public void regulairCollision(CollisionEvent event){}
public void objectCollision(CollisionEvent event){}
public void trigger(CollisionEvent event){}
}

View File

@@ -0,0 +1,38 @@
package com.jan.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class Main extends ApplicationAdapter {
private OrthographicCamera camera;
@Override
public void create () {
SceneManager.initialize();
camera = new OrthographicCamera(30,20);
Display.setResolution(1920, 1080);
Display.setResolution(680, 420);
Display.useCamera(camera);
camera.translate(15, 10);
SceneManager.loadScene("maps/testLevel1.tmx");
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
SceneManager.updateScene();;
Display.update();
}
@Override
public void dispose () {
}
}

View File

@@ -0,0 +1,30 @@
package com.jan.game;
import com.badlogic.gdx.physics.box2d.Box2D;
import com.jan.Collision.CollisionEvent;
import com.jan.scene.Scene;
public class SceneManager {
private static Scene currScene;
public static void initialize(){
Display.initialize();
Box2D.init();
}
public static void loadScene(String scene){
currScene = new Scene(scene);
}
public static void fireEvent(CollisionEvent event){
currScene.fireEvent(event);
}
public static void updateScene(){
currScene.render();
}
public static Scene getScene(){
return currScene;
}
}

View File

@@ -0,0 +1,40 @@
package com.jan.scene;
import java.util.Iterator;
import com.badlogic.gdx.maps.MapObject;
public abstract class Entity {
private MapObject objectData;
private String entityName;
private String entityType;
private int entityID;
public Entity(MapObject mapObject){
objectData = mapObject;
entityName = objectData.getName();
entityType = (String)loadProperty("type");
entityID = (int)loadProperty("id");
}
public void update(){}
protected void verifyData(){
if(isDataLoaded() == false)
System.out.println("het");
}
private boolean isDataLoaded(){
Iterator<String> props = objectData.getProperties().getKeys();
return !props.hasNext();
}
protected Object loadProperty(String property){
Object newProperty = objectData.getProperties().get(property);
objectData.getProperties().remove(property);
return newProperty;
}
public String getEntityClass(){ return entityType; }
public String getEntityName(){ return entityName; }
public int getEntityID() { return entityID; }
}

View File

@@ -0,0 +1,92 @@
package com.jan.scene;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.jan.Collision.Collision;
import com.jan.Collision.CollisionEvent;
import com.jan.entity.Renderable;
import com.jan.game.Display;
import com.jan.game.Game;
public class Scene{
private World map;
private Collision collision;
private ArrayList<Entity> entities;
private Game currState;
private OrthographicCamera sceneCamera;
public Scene(String mapData){
super();
entities = new ArrayList<>();
TiledMap world = new TmxMapLoader().load(mapData);
this.map = new World(world);
sceneCamera = new OrthographicCamera((float)world.getProperties().get("TilesViewportWidth"), (float)world.getProperties().get("TilesViewportHeight"));
Display.useCamera(sceneCamera);
setScale((int)world.getProperties().get("tilewidth"), (int)world.getProperties().get("tileheight"));
this.collision = new Collision(world);
collision.loadColiders(map.getEntities());
populate();
currState = new GameLoader().loadState((String)world.getProperties().get("State"));
currState.create(this);
currState.start();
}
private void populate(){
ArrayList<Renderable> renderables = map.getRenderables();
for(int i = 0; i < renderables.size(); i++)
entities.add(renderables.get(i));
}
private void setScale(int tilesWidth, int tileHeight){
if(tilesWidth == tileHeight){
Display.setScale(tileHeight);
return;
}
System.out.println("Tile Width & Height must be equal");
}
public void render(){
currState.update();
collision.update(Gdx.graphics.getDeltaTime());
map.render();
}
public Entity getEntity(String name){
for(int i = 0; i < entities.size(); i++){
if(entities.get(i).getEntityName().equals(name))
return entities.get(i);
}
return null;
}
public void fireEvent(CollisionEvent event){ currState.catchEvent(event); }
}
class GameLoader{
public Game loadState(String stateClass){
try {
Class gameClass = Class.forName(stateClass);
Class[] types = {};
Constructor constructor = gameClass.getConstructor(types);
Object gamObject = constructor.newInstance();
return (Game)gamObject;
}
catch (ClassNotFoundException e) { e.printStackTrace();}
catch (NoSuchMethodException e) { e.printStackTrace();}
catch (SecurityException e) { e.printStackTrace();}
catch (InstantiationException e) { e.printStackTrace();}
catch (IllegalAccessException e) { e.printStackTrace();}
catch (IllegalArgumentException e) { e.printStackTrace();}
catch (InvocationTargetException e) { e.printStackTrace();}
return null;
}
}

View File

@@ -0,0 +1,193 @@
package com.jan.scene;
import java.util.ArrayList;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.tiled.TiledMapImageLayer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.jan.entity.Actor;
import com.jan.entity.Animator;
import com.jan.entity.Renderable;
import com.jan.entity.Trigger;
import com.jan.game.Display;
public class World {
private TiledMap tiledMap;
private WorldRenderer worldRenderer;
private ArrayList<Layer> layers;
private ArrayList<Renderable> renderables;
private ArrayList<Entity> nonRenderables;
public World(TiledMap tiledMap){
this.tiledMap = tiledMap;
WorldLoader loader = new WorldLoader(tiledMap);
layers = loader.getLayers();
renderables = loader.getRenderables();
nonRenderables = loader.getNonRenderables();
worldRenderer = new WorldRenderer(tiledMap);
}
public void render(){
worldRenderer.render(layers);
}
public Renderable getRenderable(String name){
for(int i = 0; i < renderables.size(); i++){
if(renderables.get(i).getEntityName() == name)
return renderables.get(i);
}
return null;
}
public ArrayList<Renderable> getRenderables(){ return renderables; }
public ArrayList<Entity> getNonRenderables(){ return nonRenderables; }
public ArrayList<Entity> getEntities(){
ArrayList<Entity> entities = new ArrayList<>();
for(int i = 0; i < renderables.size(); i++)
entities.add(renderables.get(i));
for(int i = 0; i < nonRenderables.size(); i++)
entities.add(nonRenderables.get(i));
return entities;
}
public TiledMap getMap(){ return tiledMap; }
}
enum LAYER_TYPES{STATIC_IMAGE, TILES, OBJECTS}
class WorldRenderer{
private OrthogonalTiledMapRenderer tiledMapRenderer;
public WorldRenderer(TiledMap tiledMap){
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, 1/32f);
}
public void render(ArrayList<Layer> layers){
tiledMapRenderer.setView(Display.getCamera());
tiledMapRenderer.getBatch().begin();
for(int i = 0; i < layers.size(); i++)
renderLayer(layers.get(i));
tiledMapRenderer.getBatch().end();
}
private void renderLayer(Layer curr){
switch(curr.getType()){
case OBJECTS: renderObjectLayer(curr.getObjectLayer());break;
case STATIC_IMAGE: tiledMapRenderer.renderImageLayer(curr.getImageLayer()); break;
case TILES: tiledMapRenderer.renderTileLayer(curr.getTileLayer()); break;
}
}
private void renderObjectLayer(ArrayList<Renderable> objLayer){
for(int i = 0; i < objLayer.size(); i++){
Renderable obj = objLayer.get(i);
obj.update();
tiledMapRenderer.getBatch().draw(obj.getTexture(), obj.getX() /32, obj.getY() /32 , obj.getWidth() /32, obj.getHeight() /32);
}
}
}
class WorldLoader{
private TiledMap worldMap;
private ArrayList<Layer> worldLayers;
public WorldLoader(TiledMap worldMap){
this.worldMap = worldMap;
worldLayers = new ArrayList<>();
populate();
}
private void populate(){
int count = worldMap.getLayers().getCount();
for(int i = 0; i < count; i++)
determenType(worldMap.getLayers().get(i));
}
private void determenType(MapLayer curr){
if(curr instanceof TiledMapImageLayer){ worldLayers.add(new Layer((TiledMapImageLayer)curr)); return; }
if(curr instanceof TiledMapTileLayer){ worldLayers.add(new Layer((TiledMapTileLayer)curr)); return; }
if(curr instanceof MapLayer){ worldLayers.add(new Layer(curr)); return; }
}
public ArrayList<Renderable> getRenderables(){
ArrayList<Renderable> renederables = new ArrayList<>();
for(int i = 0; i < worldLayers.size(); i++){
if(worldLayers.get(i).getType() == LAYER_TYPES.OBJECTS){
ArrayList<Renderable> objLayer = worldLayers.get(i).getObjectLayer();
for(int j = 0; j < objLayer.size(); j++)
renederables.add(objLayer.get(j));
}
}
return renederables;
}
public ArrayList<Entity> getNonRenderables(){
ArrayList<Entity> nonRenderables = new ArrayList<>();
for(int i = 0; i < worldLayers.size(); i++){
if(worldLayers.get(i).getType() == LAYER_TYPES.OBJECTS){
ArrayList<Entity> objLayer = worldLayers.get(i).getNonRenderables();
for(int j = 0; j < objLayer.size(); j++)
nonRenderables.add(objLayer.get(j));
}
}
return nonRenderables;
}
public ArrayList<Layer> getLayers(){
return worldLayers;
}
}
class Layer{
private TiledMapTileLayer tileLayer;
private TiledMapImageLayer imageLayer;
private ArrayList<Renderable> objectLayer;
private ArrayList<Entity> nonRenderables;
private LAYER_TYPES type;
public Layer(TiledMapTileLayer tileLayer){
this.type = LAYER_TYPES.TILES;
this.tileLayer = tileLayer;
}
public Layer(TiledMapImageLayer imageLayer){
this.type = LAYER_TYPES.STATIC_IMAGE;
this.imageLayer = imageLayer;
}
public Layer(MapLayer objLayer){
this.type = LAYER_TYPES.OBJECTS;
objectLayer = new ArrayList<>();
nonRenderables = new ArrayList<>();
for(int i = 0; i < objLayer.getObjects().getCount(); i++){
MapObject currObj = objLayer.getObjects().get(i);
String type = (String)objLayer.getObjects().get(i).getProperties().get("type");
if(type == null){continue;};
switch(type){
case "Renderable": objectLayer.add(new Renderable(currObj)); break;
case "Animator": objectLayer.add(new Animator(currObj)); break;
case "Actor": objectLayer.add(new Actor(currObj)); break;
case "Trigger": nonRenderables.add(new Trigger(currObj));
}
}
}
public TiledMapTileLayer getTileLayer(){ return tileLayer; }
public TiledMapImageLayer getImageLayer(){ return imageLayer; }
public ArrayList<Renderable> getObjectLayer(){ return objectLayer; }
public ArrayList<Entity> getNonRenderables() {return nonRenderables; }
public LAYER_TYPES getType(){
return type;
}
}

View File

@@ -0,0 +1,96 @@
package com.jan.state;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.jan.Collision.CollisionEvent;
import com.jan.entity.Actor;
import com.jan.game.Display;
import com.jan.game.Game;
public class Platformer extends Game{
private Actor player;
private boolean isColiding;
private float maxSpeed = 8;
private float accelRate = 50f;
private float jumpForce = 30f;
private float coolDown = 0.3f;
private float currCooldown = 0;
public Platformer(){
super();
}
public void start() {
player = (Actor)this.currScene.getEntity("Player");
}
@Override
public void update() {
updateInput();
updateSpeed();
updateCamera();
}
private void updateInput(){
if(Gdx.input.isKeyPressed(Input.Keys.NUMPAD_6))
player.getColider().getBody().applyForceToCenter(new Vector2(accelRate, 0), true);
if(Gdx.input.isKeyPressed(Input.Keys.NUMPAD_4))
player.getColider().getBody().applyForceToCenter(new Vector2(-accelRate, 0), true);
if(Gdx.input.isKeyPressed(Input.Keys.A) && isColiding == true && currCooldown <= 0){
float playerSpeed = player.getColider().getBody().getLinearVelocity().x;
player.getColider().getBody().setLinearVelocity(playerSpeed, jumpForce);
}
}
private void updateCamera(){
if(player.getX() < 420){
Display.getCamera().position.set(13.644001f ,8.388437f, 0);
return;
}
System.out.println(Display.getCamera().position);
float offsetHeight = (Display.getCamera().viewportHeight /2);
float offsetWidth = (Display.getCamera().viewportWidth /2);
Vector3 pos = new Vector3(player.getColider().getBody().getWorldCenter(), 0);
pos.add(offsetWidth, offsetHeight, 0);
pos.sub(Display.getCamera().viewportWidth /2, (player.getHeight() /32)*2.6f, 0) ;
Display.getCamera().position.set(pos);
}
private void updateSpeed(){
Vector2 speed = player.getColider().getBody().getLinearVelocity();
speed = new Vector2(speed.x, speed.y);
if(player.getColider().getBody().getPosition().x <= 0){
System.out.println("Fuck");
float angle = player.getColider().getBody().getAngle();
player.getColider().getBody().setTransform(87.0f /32, 128.22997f /32, angle);
}
if(speed.x > maxSpeed )
player.getColider().getBody().setLinearVelocity(maxSpeed, speed.y);
if( speed.x < maxSpeed*-1)
player.getColider().getBody().setLinearVelocity(-maxSpeed, speed.y);
if(currCooldown > 0)
currCooldown -= Gdx.graphics.getDeltaTime();
}
public void regulairCollision(CollisionEvent event){
Vector2 distance = event.getBodyB().getPosition();
distance.sub(event.getContact().getFixtureA().getBody().getPosition());
if(distance.y < 1.4f){
isColiding = false;
return;
}
switch(event.getState()){
case BEGIN: isColiding = true; currCooldown = coolDown; break;
case END: isColiding = false; break;
}
}
}