Add actor, make animation per actor

This commit is contained in:
austin 2021-05-26 22:16:28 -04:00
parent 329420ae94
commit 6e6979e73a
14 changed files with 198 additions and 76 deletions

View File

@ -14,22 +14,35 @@ import org.joml.Vector3f;
* @author amaterasu
*/
public class CreatureUtils {
// public static Entity spawnBasicCreature(int creatureId, float acceleration, float maxVelocity){
// Entity rVal = new Entity();
// rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true);
// rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, Globals.entityTypeMap.get(creatureId).getModelPath());
// Globals.assetManager.addModelPathToQueue(Globals.entityTypeMap.get(creatureId).getModelPath());
// rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_TYPE, creatureId);
// rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
// rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().rotateAxis((float)0, new Vector3f(1,0,0)));
// rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
// rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_BT, new MovementTree(rVal));
// rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_VECTOR, new Vector3f(0,0,0));
// rVal.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, maxVelocity);
// rVal.putData(EntityDataStrings.DATA_STRING_ACCELERATION, acceleration);
// rVal.putData(EntityDataStrings.DATA_STRING_VELOCITY, 0f);
// Globals.entityManager.registerEntity(rVal);
// Globals.entityManager.registerDrawableEntity(rVal);
// Globals.entityManager.registerMoveableEntity(rVal);
// return rVal;
// }
public static Entity spawnBasicCreature(int creatureId, float acceleration, float maxVelocity){
Entity rVal = new Entity();
Entity rVal = EntityUtils.spawnDrawableEntity(Globals.entityTypeMap.get(creatureId).getModelPath());
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true);
rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, Globals.entityTypeMap.get(creatureId).getModelPath());
Globals.assetManager.addModelPathToQueue(Globals.entityTypeMap.get(creatureId).getModelPath());
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_TYPE, creatureId);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().rotateAxis((float)0, new Vector3f(1,0,0)));
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_BT, new MovementTree(rVal));
rVal.putData(EntityDataStrings.DATA_STRING_MOVEMENT_VECTOR, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_MAX_NATURAL_VELOCITY, maxVelocity);
rVal.putData(EntityDataStrings.DATA_STRING_ACCELERATION, acceleration);
rVal.putData(EntityDataStrings.DATA_STRING_VELOCITY, 0f);
Globals.entityManager.registerEntity(rVal);
Globals.entityManager.registerDrawableEntity(rVal);
Globals.entityManager.registerMoveableEntity(rVal);
return rVal;
}

View File

@ -13,6 +13,7 @@ public class EntityDataStrings {
public static final String DATA_STRING_ROTATION = "rotation";
public static final String DATA_STRING_SCALE = "scale";
public static final String DATA_STRING_MODEL_PATH = "modelPath";
public static final String DATA_STRING_ACTOR = "actor";
/*

View File

@ -8,6 +8,8 @@ package electrosphere.entity;
import electrosphere.entity.state.MovementTree;
import electrosphere.renderer.Model;
import electrosphere.main.Globals;
import electrosphere.renderer.Actor;
import electrosphere.renderer.ActorUtils;
import org.joml.Quaternionf;
import org.joml.Vector3f;
@ -35,7 +37,19 @@ public class EntityUtils {
public static Entity spawnDrawableEntity(String modelPath){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, modelPath);
rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
// rVal.putData(EntityDataStrings.DATA_STRING_MODEL_PATH, modelPath);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().rotateAxis((float)0, new Vector3f(1,0,0)));
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
Globals.entityManager.registerEntity(rVal);
Globals.entityManager.registerDrawableEntity(rVal);
return rVal;
}
public static Entity spawnDrawableEntityWithPreexistingModel(String modelPath){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.DATA_STRING_ACTOR, ActorUtils.createActorFromModelPath(modelPath));
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().rotateAxis((float)0, new Vector3f(1,0,0)));
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(1,1,1));
@ -55,5 +69,9 @@ public class EntityUtils {
Globals.entityManager.overrideEntityId(e, id);
}
public static Actor getEntityActor(Entity e){
return (Actor)e.getData(EntityDataStrings.DATA_STRING_ACTOR);
}
}

View File

@ -6,7 +6,8 @@ import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import electrosphere.net.NetUtils;
import electrosphere.net.message.EntityMessage;
import electrosphere.renderer.Animation;
import electrosphere.renderer.Actor;
import electrosphere.renderer.anim.Animation;
import electrosphere.renderer.Model;
import java.util.LinkedList;
import java.util.concurrent.CopyOnWriteArrayList;
@ -75,7 +76,8 @@ public class MovementTree {
float velocity = CreatureUtils.getVelocity(parent);
float acceleration = CreatureUtils.getAcceleration(parent);
float maxNaturalVelocity = CreatureUtils.getMaxNaturalVelocity(parent);
Model entityModel = Globals.assetManager.fetchModel(EntityUtils.getEntityModelPath(parent));
Actor entityActor = EntityUtils.getEntityActor(parent);
// Model entityModel = Globals.assetManager.fetchModel(EntityUtils.getEntityModelPath(parent));
Vector3f position = EntityUtils.getEntityPosition(parent);
Vector3f movementVector = CreatureUtils.getMovementVector(parent);
@ -99,10 +101,10 @@ public class MovementTree {
//run startup code
velocity = velocity + acceleration;
CreatureUtils.setVelocity(parent, velocity);
if(entityModel != null){
if(entityModel.currentAnimation == null || !entityModel.currentAnimation.name.equals(Animation.ANIMATION_MOVEMENT_STARTUP)){
entityModel.playAnimation(Animation.ANIMATION_MOVEMENT_STARTUP);
entityModel.currentAnimation.incrementTime(0.01);
if(entityActor != null){
if(!entityActor.isPlayingAnimation() || !entityActor.getCurrentAnimation().equals(Animation.ANIMATION_MOVEMENT_STARTUP)){
entityActor.playAnimation(Animation.ANIMATION_MOVEMENT_STARTUP);
entityActor.incrementAnimationTime(0.01);
}
}
//check if can transition state
@ -118,10 +120,10 @@ public class MovementTree {
case MOVE:
//check if can restart animation
//if yes, restart animation
if(entityModel != null){
if(entityModel.currentAnimation == null || !entityModel.currentAnimation.name.equals(Animation.ANIMATION_MOVEMENT_MOVE)){
entityModel.playAnimation(Animation.ANIMATION_MOVEMENT_MOVE);
entityModel.currentAnimation.incrementTime(0.01);
if(entityActor != null){
if(!entityActor.isPlayingAnimation() || !entityActor.getCurrentAnimation().equals(Animation.ANIMATION_MOVEMENT_MOVE)){
entityActor.playAnimation(Animation.ANIMATION_MOVEMENT_MOVE);
entityActor.incrementAnimationTime(0.01);
}
}
//check if can move forward (collision engine)
@ -134,10 +136,10 @@ public class MovementTree {
//run slowdown code
velocity = velocity - acceleration;
CreatureUtils.setVelocity(parent, velocity);
if(entityModel != null){
if(entityModel.currentAnimation == null || !entityModel.currentAnimation.name.equals(Animation.ANIMATION_MOVEMENT_STARTUP)){
entityModel.playAnimation(Animation.ANIMATION_MOVEMENT_STARTUP);
entityModel.currentAnimation.incrementTime(0.01);
if(entityActor != null){
if(!entityActor.isPlayingAnimation() || !entityActor.getCurrentAnimation().equals(Animation.ANIMATION_MOVEMENT_STARTUP)){
entityActor.playAnimation(Animation.ANIMATION_MOVEMENT_STARTUP);
entityActor.incrementAnimationTime(0.01);
}
}
//check if can transition state
@ -151,10 +153,10 @@ public class MovementTree {
EntityUtils.getEntityRotation(parent).rotationTo(new Vector3f(0,0,1), movementVector);
break;
case IDLE:
if(entityModel != null){
if(entityModel.currentAnimation == null || !entityModel.currentAnimation.name.equals(Animation.ANIMATION_IDLE_1)){
entityModel.playAnimation(Animation.ANIMATION_IDLE_1);
entityModel.currentAnimation.incrementTime(0.01);
if(entityActor != null){
if(!entityActor.isPlayingAnimation() || !entityActor.getCurrentAnimation().equals(Animation.ANIMATION_IDLE_1)){
entityActor.playAnimation(Animation.ANIMATION_IDLE_1);
entityActor.incrementAnimationTime(0.01);
}
}
break;

View File

@ -19,6 +19,7 @@ import electrosphere.game.terrain.TerrainManager;
import electrosphere.net.client.ClientNetworkMessage;
import electrosphere.net.client.ClientNetworking;
import electrosphere.net.server.Server;
import electrosphere.renderer.Actor;
import electrosphere.renderer.ModelUtils;
import electrosphere.terraingen.TerrainGen;
import electrosphere.terraingen.models.TerrainModel;
@ -168,9 +169,11 @@ public class Main {
///
/// C A M E R A C R E A T I O N
///
Camera cam_Player_Orbit = new Camera();
Vector3f cameraRotationVector = new Vector3f();
Entity tempCreature = CreatureUtils.spawnBasicCreature(0, 1, 1);
EntityUtils.getEntityScale(tempCreature).set(0.1f);
//main loop
@ -278,18 +281,34 @@ public class Main {
//
// D R A W A L L E N T I T I E S
//
Matrix4f modelTransformMatrix = new Matrix4f();
for(Entity currentEntity : Globals.entityManager.getDrawable()){
Model currentModel = Globals.assetManager.fetchModel(EntityUtils.getEntityModelPath(currentEntity));
if(currentModel != null){
if(currentModel.currentAnimation != null){
currentModel.incrementTime(deltaTime * 500);
}
currentModel.modelMatrix = new Matrix4f();
currentModel.modelMatrix.translate(new Vector3f(EntityUtils.getEntityPosition(currentEntity)).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
currentModel.modelMatrix.rotate(EntityUtils.getEntityRotation(currentEntity));
currentModel.modelMatrix.scale(EntityUtils.getEntityScale(currentEntity));
currentModel.draw();
//fetch actor
Actor currentActor = EntityUtils.getEntityActor(currentEntity);
//increment animations
if(currentActor.getCurrentAnimation() != null){
currentActor.incrementAnimationTime(deltaTime * 500);
}
//calculate and apply model transform
modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(EntityUtils.getEntityPosition(currentEntity)).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
modelTransformMatrix.rotate(EntityUtils.getEntityRotation(currentEntity));
modelTransformMatrix.scale(EntityUtils.getEntityScale(currentEntity));
currentActor.applyModelMatrix(modelTransformMatrix);
//draw
currentActor.draw();
// Model currentModel = Globals.assetManager.fetchModel(EntityUtils.getEntityModelPath(currentEntity));
// if(currentModel != null){
// if(currentModel.currentAnimation != null){
// currentModel.incrementTime(deltaTime * 500);
// }
// currentModel.modelMatrix = new Matrix4f();
// currentModel.modelMatrix.translate(new Vector3f(EntityUtils.getEntityPosition(currentEntity)).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// currentModel.modelMatrix.rotate(EntityUtils.getEntityRotation(currentEntity));
// currentModel.modelMatrix.scale(EntityUtils.getEntityScale(currentEntity));
// currentModel.draw();
// }
}

View File

@ -54,7 +54,7 @@ public class ServerConnectionHandler implements Runnable {
@Override
public void run() {
System.out.println("aaaaaaaaaaaeoiu");
System.out.println("ServerConnectionHandler start");
initialized = false;
try {
socket.setSoTimeout(100);

View File

@ -1,33 +0,0 @@
package electrosphere.net.server;
import java.util.concurrent.CopyOnWriteArrayList;
/**
*
* @author amaterasu
*/
public class ServerNetworkMessage {
byte[] rawBytes;
public byte[] getRawBytes(){
return rawBytes;
}
public static ServerNetworkMessage parseServerNetworkMessage(CopyOnWriteArrayList<Byte> byteQueue){
ServerNetworkMessage rVal = null;
if(byteQueue.size() > 0){
byte firstByte = byteQueue.get(0);
switch(firstByte){
case 0:
break;
case 1:
break;
case 2:
break;
}
}
return rVal;
}
}

View File

@ -0,0 +1,78 @@
package electrosphere.renderer;
import electrosphere.main.Globals;
import electrosphere.renderer.anim.Animation;
import org.joml.Matrix4f;
/**
*
* @author amaterasu
*/
public class Actor {
String modelPath;
String animation;
double animationTime;
boolean playingAnimation;
public Actor(String modelPath){
playingAnimation = false;
this.modelPath = modelPath;
}
public void incrementAnimationTime(double deltaTime){
Model model = Globals.assetManager.fetchModel(modelPath);
if(playingAnimation){
animationTime = animationTime + deltaTime;
}
if(model != null){
if(animation != null){
model.playAnimation(animation);
model.incrementTime(animationTime);
if(model.currentAnimation == null){
playingAnimation = false;
}
}
}
}
public double getAnimationTime(){
return animationTime;
}
public String getCurrentAnimation(){
return animation;
}
public void playAnimation(String animationName){
animationTime = 0;
playingAnimation = true;
animation = animationName;
}
public boolean isPlayingAnimation(){
return playingAnimation;
}
public void applyModelMatrix(Matrix4f modelMatrix){
Model model = Globals.assetManager.fetchModel(modelPath);
if(model != null){
model.modelMatrix = modelMatrix;
}
}
public void draw(){
Model model = Globals.assetManager.fetchModel(modelPath);
if(model != null){
if(animation != null){
model.playAnimation(animation);
model.incrementTime(0.001);
model.incrementTime(animationTime);
if(model.currentAnimation == null){
playingAnimation = false;
}
}
model.draw();
}
}
}

View File

@ -0,0 +1,17 @@
package electrosphere.renderer;
import electrosphere.main.Globals;
/**
*
* @author amaterasu
*/
public class ActorUtils {
public static Actor createActorFromModelPath(String modelPath){
Actor rVal = new Actor(modelPath);
Globals.assetManager.addModelPathToQueue(modelPath);
return rVal;
}
}

View File

@ -1,5 +1,8 @@
package electrosphere.renderer;
import electrosphere.renderer.anim.AnimChannel;
import electrosphere.renderer.anim.Animation;
import electrosphere.renderer.anim.AnimNode;
import electrosphere.main.Globals;
import java.io.File;
import java.util.ArrayList;

View File

@ -1,4 +1,4 @@
package electrosphere.renderer;
package electrosphere.renderer.anim;
import java.util.ArrayList;
import java.util.Iterator;

View File

@ -1,4 +1,4 @@
package electrosphere.renderer;
package electrosphere.renderer.anim;
import java.util.ArrayList;
import org.joml.Matrix4f;

View File

@ -1,4 +1,4 @@
package electrosphere.renderer;
package electrosphere.renderer.anim;
import java.util.ArrayList;
import java.util.HashMap;
@ -253,4 +253,8 @@ public class Animation {
return false;
}
}
public void setTime(double time){
timeCurrent = time;
}
}

View File

@ -1,4 +1,4 @@
package electrosphere.renderer;
package electrosphere.renderer.anim;
import org.joml.Quaternionf;
import org.joml.Vector3f;