Skip to content

Commit

Permalink
Adding an area that can apply physics forces to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Interrupt committed Oct 14, 2020
1 parent e436872 commit 98320d6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Dungeoneer/assets/data/entities.dat
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
},
"Status Effect Area": {
"class": "com.interrupt.dungeoneer.entities.areas.StatusEffectArea"
},
"Physics Force Area": {
"class": "com.interrupt.dungeoneer.entities.areas.PhysicsForceArea"
}
},
"Camera": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.interrupt.dungeoneer.entities.areas;

import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.interrupt.dungeoneer.annotations.EditorProperty;
import com.interrupt.dungeoneer.entities.Entity;
import com.interrupt.dungeoneer.game.Level;
import com.interrupt.dungeoneer.interfaces.Directional;

public class PhysicsForceArea extends Area implements Directional {
public PhysicsForceArea() { hidden = true; spriteAtlas = "editor"; tex = 11; isStatic = false; isDynamic = true; collision.set(0.25f, 0.25f, 1f); }

@EditorProperty
public float forceAmount = 0.01f;

@EditorProperty
public boolean instantForce = false;

@EditorProperty
public boolean enabled = true;

public Vector3 rotation = new Vector3();

public transient Vector3 dirWork = new Vector3();

@Override
public void tick(Level level, float delta) {
if(!enabled)
return;

Array<Entity> touching = level.getEntitiesEncroaching(this);
for(int i = 0; i < touching.size; i++) {
Entity e = touching.get(i);
if(!e.isStatic) {
Vector3 dir = getDirection();

if(instantForce) {
e.xa = dir.x * forceAmount;
e.ya = dir.y * forceAmount;
e.za = dir.z * forceAmount;
} else {
e.xa += dir.x * forceAmount * delta;
e.ya += dir.y * forceAmount * delta;
e.za += dir.z * forceAmount * delta;
}
}
}
}

@Override
public void onTrigger(Entity instigator, String value) {
enabled = !enabled;
}

@Override
public void rotate90() {
super.rotate90();
rotation.z -= 90f;
}

@Override
public void rotate90Reversed() {
super.rotate90Reversed();
rotation.z += 90f;
}

@Override
public void setRotation(float rotX, float rotY, float rotZ) {
rotation.x = rotX;
rotation.y = rotY;
rotation.z = rotZ;
}

@Override
public void rotate(float rotX, float rotY, float rotZ) {
rotation.x += rotX;
rotation.y += rotY;
rotation.z += rotZ;
}

@Override
public Vector3 getRotation() {
return rotation;
}

public Vector3 getDirection() {
Vector3 dir = dirWork.set(1,0,0);
dir.rotate(Vector3.Y, -rotation.y);
dir.rotate(Vector3.X, -rotation.x);
dir.rotate(Vector3.Z, -rotation.z);
return dir;
}
}

0 comments on commit 98320d6

Please sign in to comment.