Attaching items to entity's bones

This commit is contained in:
austin 2021-06-07 20:11:56 -04:00
parent 7379afbbc7
commit 323a966516
20 changed files with 259 additions and 13 deletions

View File

@ -75,10 +75,27 @@ public class EntityDataStrings {
public static final String DATA_STRING_COLLISION_ENTITY_TYPE_SPHERE = "collisionSphere";
public static final String COLLISION_ENTITY_DATA_TYPE = "collisionDataType";
public static final String COLLISION_ENTITY_DATA_TYPE_HIT = "collisionDataTypeHit";
public static final String COLLISION_ENTITY_DATA_TYPE_HURT = "collisionDataTypeHurt";
public static final String COLLISION_ENTITY_DATA_PARENT = "collisionDataParent";
public static final String COLLISION_ENTITY_DATA_BONE = "collisionDataBone";
public static final String COLLISION_ENTITY_DATA_SIZE = "collisionSphereSize";
/*
Attach Entity
*/
public static final String ATTACH_ENTITY_IS_ATTACHED = "attachIsAttached";
public static final String ATTACH_PARENT = "attachParent";
public static final String ATTACH_TARGET_BONE = "attachTargetBone";
/*
Item Entity
*/
public static final String ITEM_IS_ITEM = "itemIsItem";
public static final String ITEM_TYPE = "itemType";
}

View File

@ -17,6 +17,8 @@ public class EntityManager {
static CopyOnWriteArrayList<Entity> moveableList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> lightList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> uiList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> itemList = new CopyOnWriteArrayList();
static CopyOnWriteArrayList<Entity> attachList = new CopyOnWriteArrayList();
public EntityManager(){
@ -59,6 +61,22 @@ public class EntityManager {
return uiList;
}
public void registerItemEntity(Entity e){
itemList.add(e);
}
public CopyOnWriteArrayList<Entity> getItemEntities(){
return itemList;
}
public void registerAttachedEntity(Entity e){
attachList.add(e);
}
public CopyOnWriteArrayList<Entity> getAttachedEntities(){
return attachList;
}
public void deregisterEntity(Entity e){
if(lightList.contains(e)){
lightList.remove(e);
@ -76,6 +94,9 @@ public class EntityManager {
if(uiList.contains(e)){
uiList.remove(e);
}
if(itemList.contains(e)){
itemList.remove(e);
}
}
public void overrideEntityId(Entity e, int id){

View File

@ -44,7 +44,7 @@ public class CreatureUtils {
CreatureType rawType = Globals.entityTypeMap.get(creatureId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
for(HitboxData hitboxdata : rawType.getHitboxes()){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone()));
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getSize()));
}
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true);
rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_TYPE, creatureId);

View File

@ -7,6 +7,7 @@ package electrosphere.entity.types.creature.creaturemap;
public class HitboxData {
String type;
String bone;
float size;
public String getType() {
return type;
@ -16,5 +17,9 @@ public class HitboxData {
return bone;
}
public float getSize() {
return size;
}
}

View File

@ -15,10 +15,20 @@ import org.joml.Vector4f;
public class HitboxUtils {
public static Entity spawnRegularHitbox(Entity parent, String bone){
public static Entity spawnRegularHitbox(Entity parent, String bone, float size){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE,size);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
return rVal;
}
public static Entity spawnRegularHurtbox(Entity parent, String bone, float size){
Entity rVal = new Entity();
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_PARENT, parent);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_BONE, bone);
rVal.putData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE,size);
rVal.putData(EntityDataStrings.DATA_STRING_POSITION, new Vector3f(0,0,0));
return rVal;
}

View File

@ -0,0 +1,45 @@
package electrosphere.entity.types.item;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.MovementTree;
import electrosphere.entity.types.creature.creaturemap.CreatureType;
import electrosphere.entity.types.creature.creaturemap.HitboxData;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.main.Globals;
import electrosphere.renderer.Actor;
import electrosphere.renderer.Model;
import org.joml.Quaternionf;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class ItemUtils {
public static Entity spawnBasicItem(int itemId){
CreatureType rawType = Globals.entityTypeMap.get(itemId);
Entity rVal = EntityUtils.spawnDrawableEntity(rawType.getModelPath());
for(HitboxData hitboxdata : rawType.getHitboxes()){
Globals.hitboxManager.registerHitbox(HitboxUtils.spawnRegularHitbox(rVal, hitboxdata.getBone(), hitboxdata.getSize()));
}
rVal.putData(EntityDataStrings.ITEM_IS_ITEM, true);
rVal.putData(EntityDataStrings.ITEM_TYPE, itemId);
rVal.putData(EntityDataStrings.DATA_STRING_SCALE, new Vector3f(0.005f,0.005f,0.005f));
rVal.putData(EntityDataStrings.DATA_STRING_ROTATION, new Quaternionf().identity().rotateY((float)(-Math.PI/2)).rotateZ(-(float)(Math.PI/2)));
Globals.entityManager.registerItemEntity(rVal);
return rVal;
}
public static void updateItemActorAnimation(Entity item){
Actor actor = EntityUtils.getEntityActor(item);
if(actor.getCurrentAnimation() == null){
// Model model;
// if((model = Globals.assetManager.fetchModel(actor.getModelPath()))!=null){
// model.describeAllAnimations();
// }
actor.playAnimation("Sword|Idle");
}
}
}

View File

@ -0,0 +1,33 @@
package electrosphere.game.state;
import electrosphere.entity.Entity;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author amaterasu
*/
public class AliveManager {
List<Entity> aliveList = new ArrayList();
List<Entity> deadList = new ArrayList();
List<Entity> invulnerableList = new ArrayList();
public List<Entity> getAliveEntities(){
return aliveList;
}
public List<Entity> getDeadEntities(){
return deadList;
}
public List<Entity> getInvulnerableEntities(){
return invulnerableList;
}
public void registerAliveEntity(Entity e){
aliveList.add(e);
}
}

View File

@ -0,0 +1,42 @@
package electrosphere.game.state;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.main.Globals;
import electrosphere.renderer.Actor;
import org.joml.Quaternionf;
import org.joml.Vector3f;
/**
*
* @author amaterasu
*/
public class AttachUtils {
public static void attachEntityToEntityAtBone(Entity parent, Entity toAttach, String boneName){
Globals.entityManager.registerAttachedEntity(toAttach);
toAttach.putData(EntityDataStrings.ATTACH_ENTITY_IS_ATTACHED, true);
toAttach.putData(EntityDataStrings.ATTACH_PARENT, parent);
toAttach.putData(EntityDataStrings.ATTACH_TARGET_BONE, boneName);
}
public static void updateAttachedEntityPositions(){
for(Entity currentEntity : Globals.entityManager.getAttachedEntities()){
Entity parent;
if((parent = (Entity)currentEntity.getData(EntityDataStrings.ATTACH_PARENT))!=null){
String targetBone;
if((targetBone = (String)currentEntity.getData(EntityDataStrings.ATTACH_TARGET_BONE))!=null){
Actor parentActor = EntityUtils.getEntityActor(parent);
Vector3f position = new Vector3f(parentActor.getBonePosition(targetBone));
position = position.mul(((Vector3f)EntityUtils.getEntityScale(parent)));
position = position.rotate(((Quaternionf)EntityUtils.getEntityRotation(parent)));
position.add(new Vector3f(EntityUtils.getEntityPosition(parent)));
EntityUtils.getEntityPosition(currentEntity).set(position);
EntityUtils.getEntityRotation(currentEntity).add(EntityUtils.getEntityRotation(parent)).normalize();
}
}
}
}
}

View File

@ -6,6 +6,7 @@ import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.collision.CollisionEngine;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.game.cell.DrawCellManager;
import electrosphere.game.state.SimulationState.SimulationStateMachine;
import electrosphere.game.terrain.TerrainManager;
@ -379,6 +380,12 @@ public class LoadingThread extends Thread {
// EntityUtils.getEntityPosition(creature).set(rand.nextFloat() * 10, rand.nextFloat() * 10, rand.nextFloat() * 10);
// EntityUtils.getEntityScale(creature).set(0.01f);
// }
Entity sword = ItemUtils.spawnBasicItem(2);
Entity testHomie = CreatureUtils.spawnBasicCreature(0, 0.1f, 0.5f);
EntityUtils.getEntityScale(testHomie).set(0.005f);
EntityUtils.getEntityPosition(testHomie).set(2,0,2);
AttachUtils.attachEntityToEntityAtBone(testHomie, sword, "Bone.020");
}

View File

@ -57,7 +57,7 @@ public class World {
rVal.type = WorldType.GAME_WORLD;
rVal.terrainManager = terrainManager;
rVal.worldMinPoint = new Vector3f(0,0,0);
int worldDim = rVal.terrainManager.getWorldDiscreteSize();
int worldDim = rVal.terrainManager.getWorldDiscreteSize() * rVal.terrainManager.getChunkWidth();
rVal.worldMaxPoint = new Vector3f(worldDim,0,worldDim);
return rVal;
}

View File

@ -18,6 +18,7 @@ import electrosphere.entity.types.creature.creaturemap.CreatureType;
import electrosphere.entity.types.creature.creaturemap.CreatureTypeList;
import electrosphere.entity.types.hitbox.HitboxManager;
import electrosphere.game.cell.DrawCellManager;
import electrosphere.game.state.AliveManager;
import electrosphere.game.state.LoadingThread;
import electrosphere.game.terrain.TerrainManager;
import electrosphere.game.world.World;
@ -185,6 +186,9 @@ public class Globals {
//ui text box for loading text
public static TextBox loadingBox;
//life status entity manager
public static AliveManager aliveManager;
//the player camera entity
public static Entity playerCamera;
@ -225,6 +229,8 @@ public class Globals {
widgetManager = new WidgetManager();
//hitbox manager
hitboxManager = new HitboxManager();
//alive manager
aliveManager = new AliveManager();
}
public static void initDefaultGraphicalResources(){

View File

@ -16,7 +16,9 @@ import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.MovementTree;
import electrosphere.entity.types.hitbox.HitboxUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.game.cell.DrawCellManager;
import electrosphere.game.state.AttachUtils;
import electrosphere.game.state.LoadingThread;
import electrosphere.game.state.SimulationState;
import electrosphere.game.state.SimulationState.SimulationStateMachine;
@ -210,6 +212,12 @@ public class Main {
/// C L I E N T S I M U L A T I O N S T U F F
///
if(SimulationState.simulationState == SimulationStateMachine.MAIN_SIMULATION || SimulationState.simulationState == SimulationStateMachine.ARENA_SIMULATION){
//make items play idle animation
for(Entity item : Globals.entityManager.getItemEntities()){
ItemUtils.updateItemActorAnimation(item);
}
//update attached entity positions
AttachUtils.updateAttachedEntityPositions();
//simulate creature behavior trees
for(Entity currentMoveable : Globals.entityManager.getMoveable()){
MovementTree behaviorTree = CreatureUtils.getEntityMovementTree(currentMoveable);

View File

@ -3,6 +3,8 @@ package electrosphere.net.server;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.types.item.ItemUtils;
import electrosphere.game.state.AttachUtils;
import electrosphere.main.Globals;
import electrosphere.main.Main;
import electrosphere.net.NetUtils;
@ -88,6 +90,10 @@ public class ServerConnectionHandler implements Runnable {
Entity newPlayerCharacter = CreatureUtils.spawnBasicCreature(0, 0.001f, 0.05f);
EntityUtils.getEntityScale(newPlayerCharacter).set(0.005f);
EntityUtils.getEntityPosition(newPlayerCharacter).set(Globals.spawnPoint.x,Globals.drawCellManager.getElevationAtRealPoint(Globals.spawnPoint.x, Globals.spawnPoint.z),Globals.spawnPoint.z);
//spawn player sword
Entity sword = ItemUtils.spawnBasicItem(2);
AttachUtils.attachEntityToEntityAtBone(newPlayerCharacter, sword, "Bone.020");
//set controller id
CreatureUtils.setControllerPlayerId(newPlayerCharacter, playerID);
if(Globals.mainConfig.runServer && Main.playerId == -1){
Globals.playerCharacter = newPlayerCharacter;

View File

@ -111,9 +111,7 @@ public class Actor {
model.playAnimation(animation);
model.incrementTime(0.001);
model.incrementTime(animationTime);
if(model.currentAnimation == null){
playingAnimation = false;
}
model.update_node_transform(model.root_anim_node);
Bone currentBone = model.boneMap.get(boneName);
if(currentBone != null){
Vector4f result = currentBone.final_transform.transform(new Matrix4f(currentBone.inverseBindPoseMatrix).invert().transform(new Vector4f(rVal.x,rVal.y,rVal.z,1)));

View File

@ -1,8 +1,3 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package electrosphere.renderer;
import electrosphere.entity.CameraEntityUtils;
@ -685,7 +680,7 @@ public class RenderUtils {
modelTransformMatrix.identity();
modelTransformMatrix.translate(new Vector3f(position).sub(CameraEntityUtils.getCameraCenter(Globals.playerCamera)).sub(new Vector3f(0,1,0)));
// modelTransformMatrix.translate(-0.25f, 0.0f, 0.5f); //center sphere
modelTransformMatrix.scale(0.1f);
modelTransformMatrix.scale((Float)currentHitbox.getData(EntityDataStrings.COLLISION_ENTITY_DATA_SIZE));
hitboxModel.modelMatrix = modelTransformMatrix;
hitboxModel.draw();
}

View File

@ -5,7 +5,36 @@
"modelPath" : "Models/person1walkanim.fbx",
"hitboxes" : [
{
"bone": "Bone.031"
"bone": "Bone.031",
"size": 0.08
},
{
"bone": "Bone.017",
"size": 0.08
},
{
"bone": "Bone.012",
"size": 0.08
},
{
"bone": "Bone.003",
"size": 0.08
},
{
"bone": "Bone.010",
"size": 0.11
},
{
"bone": "Bone.001",
"size": 0.11
},
{
"bone": "Bone",
"size": 0.15
},
{
"bone": "Bone.014",
"size": 0.12
}
]
},
@ -13,6 +42,24 @@
"id" : 1,
"modelPath" : "",
"hitboxes" : []
},
{
"id" : 2,
"modelPath" : "Models/katana1alt.fbx",
"hitboxes" : [
{
"bone": "Blade1",
"size": 0.08
},
{
"bone": "Blade2",
"size": 0.08
},
{
"bone": "Blade3",
"size": 0.08
}
]
}
]
}

Binary file not shown.

Binary file not shown.

View File

@ -23,6 +23,12 @@
"Textures/transparent_blue.png",
"Textures/transparent_blue.png"
]
},
"Models/katana1.fbx": {
"SwordMesh": [
"Textures/katana1.png",
"Textures/katana1.png"
]
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB