Skip to content

Commit

Permalink
Merge pull request #31 from Interrupt/tile-elevators
Browse files Browse the repository at this point in the history
Adding some prototype elevator triggers that move tiles
  • Loading branch information
Interrupt authored Jan 29, 2019
2 parents e4b874c + 8c044ed commit 5f7c365
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 11 deletions.
1 change: 1 addition & 0 deletions Dungeoneer/assets/data/entities.dat
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"triggerAnimationTime": 10
},
"Repeater": { class: com.interrupt.dungeoneer.entities.triggers.RepeatingTrigger, floating: true },
"Elevator": { class: com.interrupt.dungeoneer.entities.triggers.TriggeredElevator, floating: true, triggerType: "PLAYER_TOUCHED" },
"DamageTrigger": { class: com.interrupt.dungeoneer.entities.triggers.DamageTrigger, floating: true, isSolid: true },
"LookAtTrigger": { class: com.interrupt.dungeoneer.entities.triggers.LookAtTrigger, floating: true},
"DeleteTrigger": { class: com.interrupt.dungeoneer.entities.triggers.TriggeredDelete, floating: true, triggerType: "PLAYER_TOUCHED" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.interrupt.dungeoneer.entities.triggers;

import com.interrupt.dungeoneer.GameManager;
import com.interrupt.dungeoneer.annotations.EditorProperty;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.gfx.WorldChunk;
import com.interrupt.dungeoneer.tiles.Tile;

public class TriggeredElevator extends Trigger {

public enum ElevatorType { FLOOR, CEILING, BOTH, OPPOSITE }
public enum ElevatorState { NONE, MOVING, WAITING, RETURNING }
public enum ReturnType { AUTO, WAITS }

@EditorProperty
public float moveSpeed = 0.1f;

@EditorProperty
public float moveAmount = 1.0f;

@EditorProperty
public float endWaitTime = 1.0f;

@EditorProperty
public ElevatorType elevatorType = ElevatorType.FLOOR;

@EditorProperty
public ReturnType returnType = ReturnType.AUTO;

protected float hasMoved = 0;
protected float waitTime = 0;
protected float deltaBuffer = 0;
protected ElevatorState state = ElevatorState.NONE;

public TriggeredElevator() { hidden = true; spriteAtlas = "editor"; tex = 11; }

boolean elevatorMoving = false;

@Override
public void doTriggerEvent(String value) {
if(state == ElevatorState.NONE) {
waitTime = 0;
state = ElevatorState.MOVING;
} else if(state == ElevatorState.WAITING) {
if (waitTime > endWaitTime) {
waitTime = 0;
state = ElevatorState.RETURNING;
}
}

super.doTriggerEvent(value);
}

@Override
public void tick(Level level, float delta) {
super.tick(level, delta);

float moving = 0;

// Put a cap on how often the level updates
deltaBuffer += delta;

if(deltaBuffer >= 1f) {
if (state == ElevatorState.MOVING) {
moving = moveSpeed * deltaBuffer * 0.1f;
if(moveAmount < 0) moving *= -1;

hasMoved += moving;

if (Math.abs(hasMoved) > Math.abs(moveAmount)) {
moving -= hasMoved - moveAmount;
hasMoved = moveAmount;

state = ElevatorState.WAITING;
}
} else if (state == ElevatorState.RETURNING) {
moving = -moveSpeed * deltaBuffer * 0.1f;
if(moveAmount < 0) moving *= -1;

hasMoved += moving;

if (moveAmount > 0 && hasMoved < 0 || moveAmount < 0 && hasMoved > 0) {
moving += 0 - hasMoved;
hasMoved = 0;
state = ElevatorState.NONE;
}
} else if (state == ElevatorState.WAITING) {
waitTime += delta;

if(returnType == ReturnType.AUTO) {
if (waitTime > endWaitTime) {
waitTime = 0;
state = ElevatorState.RETURNING;
}
}
}

deltaBuffer = 0;
}

if(moving != 0) {
for(int tileX = (int)x - (int)collision.x; tileX < x + collision.x; tileX++) {
for(int tileY = (int)y - (int)collision.y; tileY < y + collision.y; tileY++) {
Tile t = level.getTileOrNull(tileX, tileY);

if(t != null) {
if (elevatorType == ElevatorType.FLOOR || elevatorType == ElevatorType.BOTH) {
t.floorHeight += moving;
}
if (elevatorType == ElevatorType.CEILING || elevatorType == ElevatorType.BOTH) {
t.ceilHeight += moving;
}
if (elevatorType == ElevatorType.OPPOSITE) {
t.floorHeight += moving;
t.ceilHeight -= moving;
}

// Make this tile totally solid when fully closed
t.blockMotion = t.getMinOpenHeight() <= 0.0f;

markWorldAsDirty(tileX, tileY);
markWorldAsDirty(tileX + 1, tileY);
markWorldAsDirty(tileX - 1, tileY);
markWorldAsDirty(tileX, tileY + 1);
markWorldAsDirty(tileX, tileY - 1);
}
}
}
}
}

private void markWorldAsDirty(int xPos, int yPos) {
WorldChunk chunk = GameManager.renderer.GetWorldChunkAt(xPos, yPos);
if(chunk != null) {
chunk.needsRetessellation = true;
}
}
}
23 changes: 12 additions & 11 deletions Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2512,16 +2512,17 @@ public FrameBuffer CreateFrameBuffer(FrameBuffer previousBuffer, int width, int
}
}

public void refreshChunksNear(float x, float y, float range) {
if(chunks != null) {
for (int i = 0; i < chunks.size; i++) {
WorldChunk c = chunks.get(i);

float distanceX = Math.abs(x - c.position.x);
float distanceY = Math.abs(y - c.position.z);

if(distanceX <= range && distanceY <= range) {
c.refresh();
public void refreshChunksNear(float xPos, float yPos, float range) {
int startX = ((int)xPos - (int)range) / 17;
int startY = ((int)yPos - (int)range) / 17;
int endX = ((int)xPos + (int)range) / 17;
int endY = ((int)yPos + (int)range) / 17;

for(int x = startX; x <= endX; x++) {
for(int y = startY; y <= endY; y++) {
WorldChunk chunk = GetWorldChunkAt(x * 17, y * 17);
if(chunk != null) {
chunk.refresh();
}
}
}
Expand Down Expand Up @@ -2588,7 +2589,7 @@ public void Tesselate(Level level)
// Might need to update these chunks if lights have changed
for (int i = 0; i < chunks.size; i++) {
WorldChunk c = chunks.get(i);
if(c != null && !c.hasBuilt) {
if(c != null && (!c.hasBuilt || c.needsRetessellation)) {
triangleSpatialHash.dropWorldChunk(c);
c.Tesselate(loadedLevel, this);
c.tesselators.world.addCollisionTriangles(triangleSpatialHash);
Expand Down
2 changes: 2 additions & 0 deletions Dungeoneer/src/com/interrupt/dungeoneer/gfx/WorldChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class WorldChunk {

public boolean visible = true;
public boolean hasBuilt = false;
public boolean needsRetessellation = false;

public ArrayMap<String,Array<Mesh>> staticMeshBatch = null;

Expand Down Expand Up @@ -146,6 +147,7 @@ public void AddEntityForEditor(Entity e, Level level) {
public void Tesselate(Level level, GlRenderer renderer)
{
tesselator.Tesselate(level, renderer, this, xOffset, yOffset, width, height, tesselators, makeFloors, makeCeilings, makeWalls, true);
needsRetessellation = false;

if(hasBuilt) return;
hasBuilt = true;
Expand Down

0 comments on commit 5f7c365

Please sign in to comment.