Created
November 22, 2016 17:36
-
-
Save zacharycarter/540c577d0e9e497472a21e8fce88a4ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carterza.world; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.Animation; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.g2d.TextureAtlas; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.math.Vector2; | |
import com.carterza.action.Action; | |
import com.carterza.action.ActionManager; | |
import com.carterza.action.ActionResult; | |
import com.carterza.actor.Actor; | |
import com.carterza.actor.ActorTurn; | |
import com.carterza.actor.Hero; | |
import com.carterza.actor.Monster; | |
import com.carterza.component.AnimationComponent; | |
import com.carterza.map.WorldMap; | |
import com.carterza.math.Vec2I; | |
import java.util.*; | |
public class World { | |
private com.badlogic.gdx.physics.box2d.World physics; | |
private final List<Actor> actors; | |
private final Queue<ActorTurn> actorTurnQueue; | |
private final WorldMap worldMap; | |
private long currentActorId = 0; | |
private Hero hero; | |
private TextureAtlas creatureTextures; | |
long counter = 0; | |
SpriteBatch batch; | |
private float age = 0; | |
private OrthographicCamera camera; | |
public World(OrthographicCamera camera, TiledMap map, TextureAtlas creatureTextures) { | |
this.actors = new ArrayList<Actor>(); | |
this.camera = camera; | |
this.actorTurnQueue = new PriorityQueue<ActorTurn>(); | |
this.worldMap = new WorldMap(camera, map); | |
this.creatureTextures = creatureTextures; | |
initialize(); | |
} | |
private void initialize() { | |
worldMap.generate(); | |
physics = new com.badlogic.gdx.physics.box2d.World(new Vector2(0,0),true); | |
batch = new SpriteBatch(); | |
Hero hero = new Hero( | |
currentActorId | |
, this | |
, 2 | |
, new AnimationComponent(new Animation(2.0f, this.creatureTextures.findRegions("player_idle_unarmed"), Animation.PlayMode.LOOP)) | |
, new Vec2I(1,1) | |
); | |
this.hero = hero; | |
actors.add(hero); | |
actorTurnQueue.add(new ActorTurn(hero, hero.actionDelay(), counter)); | |
counter++; | |
currentActorId++; | |
Actor monsterOne = new Monster( | |
currentActorId | |
, this | |
, 2 | |
, new AnimationComponent(new Animation(2.0f, this.creatureTextures.findRegions("ai"), Animation.PlayMode.LOOP)) | |
, new Vec2I(23,23) | |
); | |
actors.add(monsterOne); | |
actorTurnQueue.add(new ActorTurn(monsterOne, monsterOne.actionDelay(), counter)); | |
counter++; | |
currentActorId++; | |
Actor monsterTwo = new Monster( | |
currentActorId | |
, this | |
, 1 | |
, new AnimationComponent(new Animation(2.0f, this.creatureTextures.findRegions("ai"), Animation.PlayMode.LOOP)) | |
, new Vec2I(1,23) | |
); | |
actors.add(monsterTwo); | |
actorTurnQueue.add(new ActorTurn(monsterTwo, monsterTwo.actionDelay(), counter)); | |
counter++; | |
currentActorId++; | |
} | |
public void render() { | |
worldMap.render(); | |
renderActors(); | |
} | |
private void renderActors() { | |
batch.setProjectionMatrix(camera.combined); | |
batch.begin(); | |
for(Actor a : actors) { | |
AnimationComponent actorAnimationComponent = a.getAnimationComponent(); | |
if(actorAnimationComponent != null) { | |
actorAnimationComponent.setAnimationAge(actorAnimationComponent.getAnimationAge() + Gdx.graphics.getDeltaTime()); | |
final TextureRegion frame = actorAnimationComponent.getAnimation().getKeyFrame(actorAnimationComponent.getAnimationAge()); | |
batch.draw(frame, a.getPosition().x * 24, a.getPosition().y * 24); | |
} | |
} | |
batch.end(); | |
} | |
public void dispose() { | |
physics.dispose(); | |
} | |
public WorldUpdate update() { | |
this.age += Gdx.graphics.getDeltaTime(); | |
WorldUpdate worldUpdate = new WorldUpdate(); | |
ActorTurn actorTurn = actorTurnQueue.remove(); | |
Actor actor = actorTurn.getActor(); | |
if(actor instanceof Hero) { | |
if (((Hero) actor).needsInput()) { | |
actorTurnQueue.add(actorTurn); | |
return worldUpdate; | |
} | |
} | |
worldUpdate.worldProgressed = true; | |
updateTurnQueue(-actorTurn.getTime()); | |
int actionCost = actor.act(); | |
actorTurnQueue.add(new ActorTurn(actor, actor.actionDelay(actionCost), counter)); | |
counter++; | |
Action actionToPerform = null; | |
ActionResult actionResult = null; | |
Iterator<Action> actionIterator = ActionManager.getActionQueue().iterator(); | |
while(actionIterator.hasNext()) { | |
actionToPerform = actionIterator.next(); | |
actionResult = actionToPerform.perform(); | |
actionIterator.remove(); | |
} | |
return worldUpdate; | |
} | |
private void updateTurnQueue(double time) { | |
Iterator<ActorTurn> turnQueueIterator = actorTurnQueue.iterator(); | |
while(turnQueueIterator.hasNext()) { | |
ActorTurn actorTurn = turnQueueIterator.next(); | |
actorTurn.setTime(actorTurn.getTime()+time); | |
} | |
} | |
public Hero getHero() { | |
return hero; | |
} | |
public WorldMap getWorldMap() { | |
return this.worldMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment