Fixed the Weapon switching, added some boilerplate classes which may be useful in the future, added music files

This commit is contained in:
Halbear 2026-07-08 13:42:45 +01:00
parent 7bad787477
commit d49ff709e2
24 changed files with 1289 additions and 923 deletions

File diff suppressed because it is too large Load diff

View file

@ -19,7 +19,7 @@ public class AudioInstance {
this.InstanceName = InstanceName; this.InstanceName = InstanceName;
SoundBuffer buffer = new SoundBuffer(AudioSourcePath); SoundBuffer buffer = new SoundBuffer(AudioSourcePath);
manager.AddSoundBuffer(BUFFER_ID, buffer); manager.AddSoundBuffer(BUFFER_ID, buffer);
AudioSource audioSource = new AudioSource(loop,true); this.audioSource = new AudioSource(loop,true);
audioSource.SetBuffer(buffer.GetBufferID()); audioSource.SetBuffer(buffer.GetBufferID());
manager.AddSoundSource(SOURCE_ID,audioSource); manager.AddSoundSource(SOURCE_ID,audioSource);
} }
@ -30,7 +30,7 @@ public class AudioInstance {
this.InstanceName = InstanceName; this.InstanceName = InstanceName;
SoundBuffer buffer = new SoundBuffer(AudioSourcePath); SoundBuffer buffer = new SoundBuffer(AudioSourcePath);
manager.AddSoundBuffer(BUFFER_ID, buffer); manager.AddSoundBuffer(BUFFER_ID, buffer);
AudioSource audioSource = new AudioSource(loop,false); this.audioSource = new AudioSource(loop,false);
audioSource.SetPosition(new Vector3f(X,Y,Z)); audioSource.SetPosition(new Vector3f(X,Y,Z));
audioSource.SetBuffer(buffer.GetBufferID()); audioSource.SetBuffer(buffer.GetBufferID());
manager.AddSoundSource(SOURCE_ID,audioSource); manager.AddSoundSource(SOURCE_ID,audioSource);

View file

@ -77,27 +77,11 @@ public class GameCore implements GameLogic {
@Override @Override
public InitData Initialise(EngineInstance engineInstance) { public InitData Initialise(EngineInstance engineInstance) {
IScene scene = engineInstance.scene(); Scene scene = (Scene) engineInstance.scene();
List<ModelData> models = new ArrayList<>(); List<ModelData> models = new ArrayList<>();
MelonaData = ModelLoader.LoadModel("resources/models/cube/Cube.json"); MelonaData = ModelLoader.LoadModel("resources/models/cube/Cube.json");
List<MaterialData> MelonaDataMat = ModelLoader.LoadMaterials("resources/models/cube/Cube_mat.json"); List<MaterialData> MelonaMat = ModelLoader.LoadMaterials("resources/models/cube/Cube_mat.json");
var Melona = new Actor("Melona", MelonaData.ID(), new Vector3f(0,0f,-5f)).SetRotation(0,(float)Math.toRadians(180),0);
Melona.CreatePhysicsController(LegacyPhysicsController.CollisionType.Sensor);
/* Melona.GetPhysicsController()
.AddCollisionSensor(new Vector3f(0,0,0))
.AddCollisionSensor(new Vector3f(0,2.4f,0))
.AddCollisionSensor(new Vector3f(0.5f,0,0))
.AddCollisionSensor(new Vector3f(-0.5f,0,0))
.AddCollisionSensor(new Vector3f(0.5f,1.8f,0))
.AddCollisionSensor(new Vector3f(-0.5f,1.8f,0))
.AddCollisionSensor(new Vector3f(0.5f,1.8f,0.5f))
.AddCollisionSensor(new Vector3f(-0.5f,1.8f,0.5f))
.AddCollisionSensor(new Vector3f(-0.5f,1.8f,-0.5f))
.AddCollisionSensor(new Vector3f(0.5f,1.8f,-0.5f))
.AddCollisionSensor(new Vector3f(0f,1.8f,0.5f))
.AddCollisionSensor(new Vector3f(-0f,1.8f,-0.5f))
.AddCollisionSensor(new Vector3f(-0f,0f,-0.5f))
.AddCollisionSensor(new Vector3f(0f,0f,0.5f));*/
CollisionMesh mesh = new CollisionMesh("Super Collision Mesh","NULL"); CollisionMesh mesh = new CollisionMesh("Super Collision Mesh","NULL");
mesh.AddNewCollisionPoly(new Vector3f(-100,-5,100),new Vector3f(-100,-5.5f,-100),new Vector3f(100,-6,-100)); mesh.AddNewCollisionPoly(new Vector3f(-100,-5,100),new Vector3f(-100,-5.5f,-100),new Vector3f(100,-6,-100));
@ -110,6 +94,9 @@ public class GameCore implements GameLogic {
Musket.SetRotation(0,(float)Math.toRadians(180),0); Musket.SetRotation(0,(float)Math.toRadians(180),0);
Musket.CreatePhysicsController( ConvexMesh.Box(0.5f,0.75f,0.5f),200f); Musket.CreatePhysicsController( ConvexMesh.Box(0.5f,0.75f,0.5f),200f);
PlayerActorID = "Player_Musket"; PlayerActorID = "Player_Musket";
scene.Musket = Musket;
scene.CreatePlayerController(scene.Musket);
RigidPhysicsController physicsController = (RigidPhysicsController) Musket.GetPhysicsController(); RigidPhysicsController physicsController = (RigidPhysicsController) Musket.GetPhysicsController();
RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).LockRotation(); RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).LockRotation();
@ -120,28 +107,18 @@ public class GameCore implements GameLogic {
Revolver.SetPositionOffset(new Vector3f(0.5f,1.0f,-1.25f)); Revolver.SetPositionOffset(new Vector3f(0.5f,1.0f,-1.25f));
Revolver.SetRotation(0,(float)Math.toRadians(180),0); Revolver.SetRotation(0,(float)Math.toRadians(180),0);
Revolver.CreatePhysicsController( ConvexMesh.Box(0.5f,0.75f,0.5f),200f); Revolver.CreatePhysicsController( ConvexMesh.Box(0.5f,0.75f,0.5f),200f);
scene.Revolver = Revolver;
PlayerActorID = "Player_Revolver";
// if I put PlayerActorID = "Player_Revolver" it just crashes // if I put PlayerActorID = "Player_Revolver" it just crashes
try {
Melona.GenerateCollisionMesh(MelonaData);
Melona.SetPhysicsType(Actor.PhysicsControllerType.T4JLegacyCollision);
Melona.GetCollisionMesh().GenerateSensorsFromVertices();
} catch(IOException e){
Logger.error("Could Not Generate Collison Mesh [{}]",e);
}
mesh.RegisterMesh(); mesh.RegisterMesh();
scene.AddActor(Melona);
scene.AddActor(Musket); scene.AddActor(Musket);
scene.AddActor(Revolver); scene.AddActor(Revolver);
ActorEditorComponents.add(new EditorActorComponent(Melona,new Vector3f(0,0f,-5f),new Vector3f(1.0f,1.8f,1.0f)));
CubeActors.forEach(scene::AddActor); CubeActors.forEach(scene::AddActor);
List<MaterialData> materials = new ArrayList<>(); List<MaterialData> materials = new ArrayList<>();
materials.addAll(MelonaDataMat);
materials.addAll(MusketMat); materials.addAll(MusketMat);
materials.addAll(RevolverMat); materials.addAll(RevolverMat);
materials.addAll(MelonaMat);
models.add(MelonaData); models.add(MelonaData);
models.add(MusketData); models.add(MusketData);
models.add(RevolverData); models.add(RevolverData);
@ -246,6 +223,7 @@ public class GameCore implements GameLogic {
Camera camera = engineInstance.scene().GetCamera(); Camera camera = engineInstance.scene().GetCamera();
audioManager.SetAttenuationModel(AL11.AL_EXPONENT_DISTANCE); audioManager.SetAttenuationModel(AL11.AL_EXPONENT_DISTANCE);
audioManager.SetListener(new SoundListener(camera.GetPosition())); audioManager.SetListener(new SoundListener(camera.GetPosition()));
engineInstance.scene().SetUpAudio(engineInstance,audioManager);
} }
@Override @Override

View file

@ -18,6 +18,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class Actor{ public class Actor{
public enum PhysicsControllerType{ public enum PhysicsControllerType{
RigidBodySeparatingAxisTheory, RigidBodySeparatingAxisTheory,
T4JLegacyCollision T4JLegacyCollision
@ -25,18 +26,18 @@ public class Actor{
public static final Map<String, Actor> Actors = new HashMap<>(); public static final Map<String, Actor> Actors = new HashMap<>();
public static final List<String> ActorNames = new ArrayList<>(); public static final List<String> ActorNames = new ArrayList<>();
private final String ID; protected final String ID;
private final String ModelID; protected final String ModelID;
private final Matrix4f ModelMatrix; protected final Matrix4f ModelMatrix;
private final Vector3f Position; protected final Vector3f Position;
private final Vector3f PositionOffset; protected final Vector3f PositionOffset;
private final Quaternionf Rotation; protected final Quaternionf Rotation;
private CollisionMesh collisionMesh; protected CollisionMesh collisionMesh;
private Vector3f Scale; protected Vector3f Scale;
private IPhysicsController physicsController; private IPhysicsController physicsController;
private final Vector3f PivotOffset; protected final Vector3f PivotOffset;
private final Vector3f AdditionalPivotOffset; protected final Vector3f AdditionalPivotOffset;
private final Vector3f TemporaryVector; protected final Vector3f TemporaryVector;
public PhysicsControllerType PhysicsType = PhysicsControllerType.RigidBodySeparatingAxisTheory; public PhysicsControllerType PhysicsType = PhysicsControllerType.RigidBodySeparatingAxisTheory;
public void SetPhysicsType(PhysicsControllerType type){ public void SetPhysicsType(PhysicsControllerType type){

View file

@ -0,0 +1,82 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.IPhysicsController;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.LegacyCollision.CollisionMesh;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.LegacyPhysicsController;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.ConvexMesh;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidPhysicsController;
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.ModelData;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ActorElement extends Actor {
public static Map<String, ActorElement> ActorElementRegistry = new HashMap<>();
public static List<String> ElementNames = new ArrayList<>();
private String ParentID = "NULL";
public ActorElement(String ID, String ModelID, String ParentID, Vector3f PositionOffset){
super(ID,ModelID,PositionOffset);
ElementNames.add(ID);
this.ParentID = ParentID;
SetPositionOffset(PositionOffset);
UpdateModelMatrix();
ActorElementRegistry.put(ID,this);
}
@Override
public IPhysicsController GetPhysicsController(){return null;}
@Override
public Actor CreatePhysicsController(ConvexMesh mesh, float Mass){
return this;
}
@Override
public Actor CreatePhysicsController(LegacyPhysicsController.CollisionType collisionType){
return this;
}
@Override
public Actor GenerateCollisionMesh(ModelData modelData) {
return this;
}
@Override
public Actor SetPosition(Vector3f position){
return this;
}
@Override
public Actor SetPosition(float x, float y, float z){
return this;
}
@Override
public Vector3f GetPosition(){
Actor Parent = Actor.Actors.get(ParentID);
if(Parent == null) return null;
return new Vector3f(Parent.GetPosition()).add(PositionOffset);
}
@Override
public void UpdateModelMatrix(){
Actor Parent = Actor.Actors.get(ParentID);
if(Parent == null) return;
ModelMatrix.translateLocal(TemporaryVector.set(PivotOffset).add(AdditionalPivotOffset));
ModelMatrix.translationRotateScale(new Vector3f(Parent.GetPosition()), new Quaternionf(Parent.GetRotation()).add(Rotation), Scale);
ModelMatrix.translate(PositionOffset);
ModelMatrix.translateLocal(TemporaryVector.negate());
}
public String GetParentID(){return ParentID;}
}

View file

@ -0,0 +1,9 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene;
public interface IPlayerController {
public void SetPlayerControls(IPlayerInputEvents Controls);
public IPlayerInputEvents GetControls();
public void SetPlayerCamera(String camera);
public Camera GetPlayerCamera();
public void SwitchActor(PlayerActor newActor);
}

View file

@ -0,0 +1,9 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene;
import net.halbear.Terrain4J.EngineCore.Input.KeyboardInput;
import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
import net.halbear.Terrain4J.EngineCore.Main.GameCore;
public interface IPlayerInputEvents {
void Input(KeyboardInput input, long FrameDiffNanoSeconds, PlayerActor playerActor, float MOVEMENT_SPEED, GameCore GameCore, IScene ParentScene);
}

View file

@ -1,5 +1,6 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene; package net.halbear.Terrain4J.EngineCore.Main.Scene;
import net.halbear.Terrain4J.EngineCore.Audio.AudioManager;
import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance; import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
import net.halbear.Terrain4J.EngineCore.Logic.Rendering.SkyBox; import net.halbear.Terrain4J.EngineCore.Logic.Rendering.SkyBox;
import net.halbear.Terrain4J.EngineCore.Main.GameCore; import net.halbear.Terrain4J.EngineCore.Main.GameCore;
@ -31,7 +32,7 @@ public interface IScene {
public void RenderGUIs(EngineInstance engineInstance, long frameTimeNS, GameCore parent); public void RenderGUIs(EngineInstance engineInstance, long frameTimeNS, GameCore parent);
public void CameraInput(EngineInstance engineInstance, long FrameDiffNanoSeconds); public void CameraInput(EngineInstance engineInstance, long FrameDiffNanoSeconds);
public String GetActiveCameraFrame(); public String GetActiveCameraFrame();
public void SetUpAudio(EngineInstance engineInstance); public void SetUpAudio(EngineInstance engineInstance, AudioManager manager);
public ISceneLightingManager GetLightingManager(); public ISceneLightingManager GetLightingManager();
public SkyBox GetSkyBox(); public SkyBox GetSkyBox();
public void SetSkyBox(SkyBox skyBox); public void SetSkyBox(SkyBox skyBox);

View file

@ -0,0 +1,39 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene.InputEvents;
import net.halbear.Terrain4J.EngineCore.Input.KeyboardInput;
import net.halbear.Terrain4J.EngineCore.Logic.EngineConfig;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidBody;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidPhysicsController;
import net.halbear.Terrain4J.EngineCore.Main.GameCore;
import net.halbear.Terrain4J.EngineCore.Main.Scene.*;
import net.halbear.Terrain4J.EngineCore.Main.Scene.GUIs.Gimbal;
import org.joml.Vector3f;
import static org.lwjgl.glfw.GLFW.*;
public class DebugCameraController implements IPlayerInputEvents {
@Override
public void Input(KeyboardInput input, long FrameDiffNanoSeconds, PlayerActor playerActor, float MOVEMENT_SPEED, GameCore GameCore, IScene ParentScene) {
Scene scene = (Scene)ParentScene;
float MovementDist = (float)(FrameDiffNanoSeconds / 1000000L) * MOVEMENT_SPEED;
Camera camera = scene.GetCamera();
if(input.keyPressed(GLFW_KEY_W)){
camera.MoveForward(MovementDist);
}
if(input.keyPressed(GLFW_KEY_S)){
camera.MoveBackward(MovementDist);
}
if(input.keyPressed(GLFW_KEY_A)){
camera.MoveLeft(MovementDist);
}
if(input.keyPressed(GLFW_KEY_D)){
camera.MoveRight(MovementDist);
}
if(input.keyPressed(GLFW_KEY_SPACE)){
camera.MoveUp(MovementDist);
}
if(input.keyPressed(GLFW_KEY_LEFT_CONTROL)){
camera.Sprint(true);
} else camera.Sprint(false);
}
}

View file

@ -0,0 +1,61 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene.InputEvents;
import net.halbear.Terrain4J.EngineCore.Display.Window;
import net.halbear.Terrain4J.EngineCore.Input.KeyboardInput;
import net.halbear.Terrain4J.EngineCore.Logic.EngineConfig;
import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidBody;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidPhysicsController;
import net.halbear.Terrain4J.EngineCore.Main.GameCore;
import net.halbear.Terrain4J.EngineCore.Main.Scene.*;
import net.halbear.Terrain4J.EngineCore.Main.Scene.GUIs.Gimbal;
import org.joml.Vector3f;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_1;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_2;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_3;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_A;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_D;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_F2;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_CONTROL;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_M;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_S;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_TAB;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_W;
public class PhysicsPlayerController implements IPlayerInputEvents {
@Override
public void Input(KeyboardInput input, long FrameDiffNanoSeconds, PlayerActor playerActor, float MOVEMENT_SPEED, GameCore GameCore, IScene ParentScene) {
Scene scene = (Scene)ParentScene;
float MovementDist = (float)(FrameDiffNanoSeconds / 1000000L) * MOVEMENT_SPEED;
Camera camera = scene.GetCamera();
if(playerActor!= null)camera = Camera.GetCamera(playerActor.AttachedCameraID);
if(input.keyPressed(GLFW_KEY_W)){
playerActor.Move(new Vector3f(MovementDist* (float)Math.sin((camera.GetRotation().y)),0,-MovementDist* (float)Math.cos((camera.GetRotation().y))));
}
if(input.keyPressed(GLFW_KEY_S)){
playerActor.Move(new Vector3f(-MovementDist* (float)Math.sin((camera.GetRotation().y)),0,MovementDist * (float)Math.cos((camera.GetRotation().y))));
}
if(input.keyPressed(GLFW_KEY_A)){
playerActor.Move(new Vector3f(-MovementDist * (float)Math.cos((camera.GetRotation().y)),0,-MovementDist * (float)Math.sin((camera.GetRotation().y))));
}
if(input.keyPressed(GLFW_KEY_D)){
playerActor.Move(new Vector3f(MovementDist * (float)Math.cos((camera.GetRotation().y)),0,MovementDist * (float)Math.sin((camera.GetRotation().y))));
}
if (playerActor != null) {
RigidPhysicsController physicsController = (RigidPhysicsController) playerActor.GetPhysicsController();
RigidBody body = RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID);
if (body != null && body.Grounded) {
scene.Jumps = 0;
}
}
if (input.keySinglePress(GLFW_KEY_SPACE)) {
if (scene.Jumps < scene.MaxJumps) {
if (playerActor != null) playerActor.Move(new Vector3f(0, 5f, 0));
scene.Jumps++;
}
}
}
}

View file

@ -28,15 +28,50 @@ public class PlayerActor extends Actor{
return this; return this;
} }
public Camera GetPlayerCamera(){
return PlayerCam;
}
public PlayerActor SetCameraOffset(Vector3f Offset){ public PlayerActor SetCameraOffset(Vector3f Offset){
CameraOffset.set(Offset); CameraOffset.set(Offset);
return this; return this;
} }
public void SetNewAngularVelocity(Vector3f Velocity){
if(PhysicsType == PhysicsControllerType.RigidBodySeparatingAxisTheory) {
RigidPhysicsController physicsController = (RigidPhysicsController) GetPhysicsController();
RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).AngularVelocity.set(Velocity);
}
}
public void SetNewVelocity(Vector3f Velocity){
if(PhysicsType == PhysicsControllerType.RigidBodySeparatingAxisTheory) {
RigidPhysicsController physicsController = (RigidPhysicsController) GetPhysicsController();
RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).LinearVelocity.set(Velocity);
}
}
public void SetNewRotation(Quaternionf Rotation){
if(PhysicsType == PhysicsControllerType.RigidBodySeparatingAxisTheory) {
RigidPhysicsController physicsController = (RigidPhysicsController) GetPhysicsController();
RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).Rotation.set(Rotation);
}
}
public void SetNewPosition(Vector3f Position){
if(PhysicsType == PhysicsControllerType.RigidBodySeparatingAxisTheory) {
RigidPhysicsController physicsController = (RigidPhysicsController) GetPhysicsController();
RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).Position.set(Position);
}
}
@Override @Override
public void TickUpdate(){ public void TickUpdate(){
if(PlayerCam == null)
{
PlayerCam = Camera.CameraRegistry.get(AttachedCameraID);
if(PlayerCam == null) return;
}
Vector3f Rotation = PlayerCam.GetRotation(); Vector3f Rotation = PlayerCam.GetRotation();
Vector3f Position = new Vector3f(GetPosition()); Vector3f Position = new Vector3f(GetPosition());
GetRotation().identity().rotateYXZ(-Rotation.y, -Rotation.x, Rotation.z); GetRotation().identity().rotateYXZ(-Rotation.y, -Rotation.x, Rotation.z);

View file

@ -0,0 +1,68 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidBody;
import net.halbear.Terrain4J.EngineCore.Logic.Physics.RigidBody.RigidPhysicsController;
import net.halbear.Terrain4J.EngineCore.Main.Scene.InputEvents.PhysicsPlayerController;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import java.util.HashMap;
import java.util.Map;
public class PlayerController implements IPlayerController{
public static Map<String, PlayerController> ControllerRegistry = new HashMap<>();
PlayerActor playerActor;
Vector3f Position;
Quaternionf Rotation;
Map<String, ActorElement> Elements = new HashMap<>();
IPlayerInputEvents InputEvents = new PhysicsPlayerController();
String PlayerCamera = "DebugCamera";
final String ID;
public PlayerController(PlayerActor Player, String ContollerID){
this.playerActor = Player;
PlayerCamera = Player.PlayerCam.GetID();
ID = ContollerID;
ControllerRegistry.put(ContollerID,this);
}
public void SwitchActor(PlayerActor newActor){
if(newActor.PlayerCam == null) return;
if(playerActor.PhysicsType == Actor.PhysicsControllerType.RigidBodySeparatingAxisTheory) {
RigidPhysicsController physicsController = (RigidPhysicsController) playerActor.GetPhysicsController();
newActor.SetNewPosition(RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).Position);
newActor.SetNewRotation(RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).Rotation);
newActor.SetNewVelocity(RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).LinearVelocity);
newActor.SetNewAngularVelocity(RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID).AngularVelocity);
}
playerActor = newActor;
PlayerCamera = newActor.PlayerCam.GetID();
}
public PlayerController AddActorElement(ActorElement newActor){
Elements.put(newActor.GetID(), newActor);
return this;
}
@Override
public void SetPlayerControls(IPlayerInputEvents Controls) {
this.InputEvents = Controls;
}
@Override
public IPlayerInputEvents GetControls() {
return InputEvents;
}
@Override
public void SetPlayerCamera(String camera) {
PlayerCamera = camera;
}
@Override
public Camera GetPlayerCamera() {
return Camera.CameraRegistry.get(PlayerCamera);
}
}

View file

@ -1,5 +1,6 @@
package net.halbear.Terrain4J.EngineCore.Main.Scene; package net.halbear.Terrain4J.EngineCore.Main.Scene;
import net.halbear.Terrain4J.EngineCore.Audio.AudioManager;
import net.halbear.Terrain4J.EngineCore.Display.Window; import net.halbear.Terrain4J.EngineCore.Display.Window;
import net.halbear.Terrain4J.EngineCore.Input.KeyboardInput; import net.halbear.Terrain4J.EngineCore.Input.KeyboardInput;
import net.halbear.Terrain4J.EngineCore.Input.MouseListener; import net.halbear.Terrain4J.EngineCore.Input.MouseListener;
@ -44,13 +45,15 @@ public class Scene implements IScene {
private CameraFrame LastCameraFrame; private CameraFrame LastCameraFrame;
public int TotalTransitionTicks = 1000; public int TotalTransitionTicks = 1000;
public float CurrentTransitionTick = 0; public float CurrentTransitionTick = 0;
private Vector2f LastMousePos = new Vector2f(0, 0); private Vector2f LastMousePos = new Vector2f(0,0);
private final ISceneLightingManager lightingManager; private final ISceneLightingManager lightingManager;
private SkyBox skyBox; private SkyBox skyBox;
private Map<String, AudioInstance> audioInstances; private Map<String,AudioInstance> audioInstances;
private DynamicTerrainMesh terrainMesh; private DynamicTerrainMesh terrainMesh;
private int Jumps = 0; public IPlayerController playerController;
private final int MaxJumps = 2;
public int Jumps = 0;
public final int MaxJumps = 2;
public PlayerActor CurrentWeapon; public PlayerActor CurrentWeapon;
public PlayerActor Musket; public PlayerActor Musket;
public PlayerActor Revolver; public PlayerActor Revolver;
@ -58,7 +61,7 @@ public class Scene implements IScene {
public List<String> ActiveGUIs = new ArrayList<>(); public List<String> ActiveGUIs = new ArrayList<>();
public Map<String, GUIOverlay> GUIReg = new HashMap<>(); public Map<String, GUIOverlay> GUIReg = new HashMap<>();
public void SetSkyBox(SkyBox skyBox) { public void SetSkyBox(SkyBox skyBox){
this.skyBox = skyBox; this.skyBox = skyBox;
} }
@ -72,161 +75,140 @@ public class Scene implements IScene {
int Width = 32; int Width = 32;
int Height = 32; int Height = 32;
float[] Heights = new float[Width * Height]; float[] Heights = new float[Width * Height];
for (int i = 0; i < Heights.length; i++) { for(int i = 0; i < Heights.length; i++){
Heights[i] = 3 * (float) Math.random(); Heights[i] = 3 * (float)Math.random();
} }
terrainMesh = new DynamicTerrainMesh(PrimaryRuntime.GetRenderThread().GetRenderer().GetVulkanContext().GetDevice(), Heights, Width, Height); terrainMesh = new DynamicTerrainMesh(PrimaryRuntime.GetRenderThread().GetRenderer().GetVulkanContext().GetDevice(), Heights, Width, Height);
} }
public SkyBox GetSkyBox() { public SkyBox GetSkyBox(){return skyBox;}
return skyBox;
}
public Scene(Window window) { public Scene(Window window) {
// CreateTerrainMesh(); // CreateTerrainMesh();
lightingManager = new SceneLightingManager(1000, new Vector3f(1f, 1f, 1f), 0.5f); lightingManager = new SceneLightingManager(1000,new Vector3f(1f,1f,1f), 0.5f);
Actors = new HashMap<>(); Actors = new HashMap<>();
audioInstances = new HashMap<>(); audioInstances = new HashMap<>();
ActiveActors = new ArrayList<>(); ActiveActors = new ArrayList<>();
CameraFrames = new HashMap<>(); CameraFrames = new HashMap<>();
var EngConfig = EngineConfig.getInstance(); var EngConfig = EngineConfig.getInstance();
camera = new Camera("DebugCamera"); camera = new Camera("DebugCamera");
LastCameraFrame = new CameraFrame(camera.GetPosition(), camera.GetRotation(), camera.GetFOV(), "NO_FRAME"); LastCameraFrame = new CameraFrame(camera.GetPosition(),camera.GetRotation(),camera.GetFOV(), "NO_FRAME");
Projection = new Project3D(camera.GetFOV(), EngConfig.GetZNearPlane(), EngConfig.GetZFarPlane(), Projection = new Project3D(camera.GetFOV(),EngConfig.GetZNearPlane(), EngConfig.GetZFarPlane(),
window.getWidth(), window.getHeight()); window.getWidth(), window.getHeight());
GUIReg.put("PerformanceOverlay", new PerformanceOverlay()); GUIReg.put("PerformanceOverlay",new PerformanceOverlay());
ActiveGUIs.add("PerformanceOverlay"); ActiveGUIs.add("PerformanceOverlay");
} }
public ISceneLightingManager GetLightingManager() { public ISceneLightingManager GetLightingManager(){return lightingManager;}
return lightingManager;
}
public void CameraTick(long FrameDiffNanoSeconds) { public void CameraTick(long FrameDiffNanoSeconds){
float FrameTimeMS = FrameDiffNanoSeconds / 1000000.0f; float FrameTimeMS = FrameDiffNanoSeconds/1000000.0f;
float DeltaTime = FrameTimeMS / (60.0f / 240.0f); float DeltaTime = FrameTimeMS/(60.0f/240.0f);
if (TransitionCameraFrame && TotalTransitionTicks > CurrentTransitionTick && !Objects.equals(SetCameraFrame, "NO_FRAME")) { if(TransitionCameraFrame && TotalTransitionTicks > CurrentTransitionTick && !Objects.equals(SetCameraFrame, "NO_FRAME")){
float StartFOV = LastCameraFrame.FOV(); float StartFOV = LastCameraFrame.FOV();
Vector3f StartPos = LastCameraFrame.Position(); Vector3f StartPos = LastCameraFrame.Position();
Vector3f StartRot = LastCameraFrame.Rotation(); Vector3f StartRot = LastCameraFrame.Rotation();
CameraFrame StartFrame; CameraFrame StartFrame;
if (CameraFrames.containsKey(ActiveCameraFrame)) { if(CameraFrames.containsKey(ActiveCameraFrame)){
StartFrame = CameraFrames.get(ActiveCameraFrame); StartFrame = CameraFrames.get(ActiveCameraFrame);
} else StartFrame = new CameraFrame(StartPos, StartRot, StartFOV, "NO_FRAME"); } else StartFrame = new CameraFrame(StartPos,StartRot,StartFOV, "NO_FRAME");
float EndFOV = camera.GetFOV(); float EndFOV = camera.GetFOV();
Vector3f EndPos = camera.GetPosition(); Vector3f EndPos = camera.GetPosition();
Vector3f EndRot = camera.GetRotation(); Vector3f EndRot = camera.GetRotation();
CameraFrame EndFrame; CameraFrame EndFrame;
if (CameraFrames.containsKey(SetCameraFrame)) { if(CameraFrames.containsKey(SetCameraFrame)){
EndFrame = CameraFrames.get(SetCameraFrame); EndFrame = CameraFrames.get(SetCameraFrame);
} else EndFrame = new CameraFrame(EndPos, EndRot, EndFOV, "NO_FRAME"); } else EndFrame = new CameraFrame(EndPos,EndRot,EndFOV, "NO_FRAME");
CameraFrame LerpedFrame = StartFrame.Lerp(EndFrame, (int) CurrentTransitionTick, TotalTransitionTicks); CameraFrame LerpedFrame = StartFrame.Lerp(EndFrame, (int)CurrentTransitionTick, TotalTransitionTicks);
camera.SetCameraFrame(LerpedFrame); camera.SetCameraFrame(LerpedFrame);
if (LastCameraFrame.FOV() != LerpedFrame.FOV()) { if(LastCameraFrame.FOV() != LerpedFrame.FOV()){
Projection.ChangeFOV(LerpedFrame.FOV()); Projection.ChangeFOV(LerpedFrame.FOV());
} }
CurrentTransitionTick += DeltaTime; CurrentTransitionTick+= DeltaTime;
} else if (TransitionCameraFrame) { } else if (TransitionCameraFrame){
CurrentTransitionTick = 0; CurrentTransitionTick = 0;
ActiveCameraFrame = SetCameraFrame; ActiveCameraFrame = SetCameraFrame;
if (!Objects.equals(CameraFrames.get(ActiveCameraFrame).NextFrame(), "NO_FRAME") && CameraFrames.containsKey(CameraFrames.get(ActiveCameraFrame).NextFrame())) { if(!Objects.equals(CameraFrames.get(ActiveCameraFrame).NextFrame(), "NO_FRAME") && CameraFrames.containsKey(CameraFrames.get(ActiveCameraFrame).NextFrame())){
SetCameraFrame = CameraFrames.get(ActiveCameraFrame).NextFrame(); SetCameraFrame = CameraFrames.get(ActiveCameraFrame).NextFrame();
} else { } else{
TotalTransitionTicks = 1000; TotalTransitionTicks = 1000;
TransitionCameraFrame = false; TransitionCameraFrame = false;
} }
} else { } else{
if (!Objects.equals(ActiveCameraFrame, SetCameraFrame) && !Objects.equals(SetCameraFrame, "NO_FRAME")) { if(!Objects.equals(ActiveCameraFrame, SetCameraFrame) && !Objects.equals(SetCameraFrame, "NO_FRAME")){
LastCameraFrame = new CameraFrame(camera.GetPosition(), camera.GetRotation(), camera.GetFOV(), "NO_FRAME"); LastCameraFrame = new CameraFrame(camera.GetPosition(),camera.GetRotation(),camera.GetFOV(), "NO_FRAME");
CurrentTransitionTick = 0; CurrentTransitionTick = 0;
TransitionCameraFrame = true; TransitionCameraFrame = true;
} }
} }
} }
public Map<String, CameraFrame> GetCameraFrameReg() { public void CreatePlayerController(PlayerActor CurrentActor){
return CameraFrames; CurrentWeapon = CurrentActor;
playerController = new PlayerController(CurrentWeapon, "PLAYERCONTROLLER");
} }
public CameraFrame GetCameraFrame(String Name) { public Map<String, CameraFrame> GetCameraFrameReg(){return CameraFrames;}
return CameraFrames.get(Name); public CameraFrame GetCameraFrame(String Name){return CameraFrames.get(Name);}
} public Scene AddCameraFrame(String Name, CameraFrame frame){
CameraFrames.put(Name,frame);
public Scene AddCameraFrame(String Name, CameraFrame frame) {
CameraFrames.put(Name, frame);
return this; return this;
} }
public void StartCameraTrack(String StartFrame, String EndFrame, int TickAmount){
public void StartCameraTrack(String StartFrame, String EndFrame, int TickAmount) {
ActiveCameraFrame = StartFrame; ActiveCameraFrame = StartFrame;
CurrentTransitionTick = 0; CurrentTransitionTick = 0;
TotalTransitionTicks = TickAmount; TotalTransitionTicks = TickAmount;
SetCameraFrame = EndFrame; SetCameraFrame = EndFrame;
} }
public void SetActiveCameraFrame(String frameName){SetCameraFrame = frameName;}
public String GetActiveCameraFrame(){return ActiveCameraFrame;}
public void SetActiveCameraFrame(String frameName) { @Override
SetCameraFrame = frameName; public void SetUpAudio(EngineInstance engineInstance, AudioManager manager) {
AudioInstance newInstance = new AudioInstance("resources/EngineResources/audio/music/groovy_fight.ogg","BGM",manager,true);
newInstance.Play();
audioInstances.put("BGM",newInstance);
} }
public String GetActiveCameraFrame() { public Camera GetCamera(){return camera;}
return ActiveCameraFrame; public void AddActor(Actor NewActor){
}
public Camera GetCamera() {
return camera;
}
public void AddActor(Actor NewActor) {
Actors.put(NewActor.GetID(), NewActor); Actors.put(NewActor.GetID(), NewActor);
ActiveActors.add(NewActor.GetID()); ActiveActors.add(NewActor.GetID());
} }
public List<Actor> GetActors(){
public List<Actor> GetActors() {
List<Actor> actors = new ArrayList<>(); List<Actor> actors = new ArrayList<>();
for (int i = 0; i < ActiveActors.size(); i++) { for(int i = 0; i < ActiveActors.size(); i++){
actors.add(Actors.get(ActiveActors.get(i))); actors.add(Actors.get(ActiveActors.get(i)));
} }
return actors; return actors;
} }
public Project3D GetProjection(){return Projection;}
public Project3D GetProjection() { public void RemoveAllActors(){Actors.clear();}
return Projection; public Actor RemoveActor(Actor actor){return Actors.remove(actor.GetID());}
}
public void RemoveAllActors() {
Actors.clear();
}
public Actor RemoveActor(Actor actor) {
return Actors.remove(actor.GetID());
}
public void SetCurrentWeapon(PlayerActor Weapon) {this.CurrentWeapon = Weapon; } public void SetCurrentWeapon(PlayerActor Weapon) {this.CurrentWeapon = Weapon; }
public void SetUpAudio(EngineInstance engineInstance) {
}
public int GUI_MODE = 0; public int GUI_MODE = 0;
@Override @Override
public void Input(EngineInstance engineInstance, long FrameDiffNanoSeconds, PlayerActor playerActor, float MOVEMENT_SPEED, GameCore parent) { public void Input(EngineInstance engineInstance, long FrameDiffNanoSeconds,PlayerActor playerActor, float MOVEMENT_SPEED, GameCore parent) {
IScene scene = engineInstance.scene(); IScene scene = engineInstance.scene();
Window window = engineInstance.window(); Window window = engineInstance.window();
KeyboardInput input = window.getKeyboardInput(); KeyboardInput input = window.getKeyboardInput();
float MovementDist = (float) (FrameDiffNanoSeconds / 1000000L) * MOVEMENT_SPEED; float MovementDist = (float)(FrameDiffNanoSeconds / 1000000L) * MOVEMENT_SPEED;
Camera camera = scene.GetCamera(); Camera camera = scene.GetCamera();
if (playerActor != null) camera = Camera.GetCamera(playerActor.AttachedCameraID); if(playerActor!= null)camera = Camera.GetCamera(playerActor.AttachedCameraID);
if (input.keySinglePress(GLFW_KEY_F3)) { if(input.keySinglePress(GLFW_KEY_F3)){
GUI_MODE = (GUI_MODE + 1) % 2; GUI_MODE = (GUI_MODE + 1) % 2;
} }
if (input.keyPressed(GLFW_KEY_ENTER)) { if(input.keyPressed(GLFW_KEY_ENTER)) {
parent.SpawnNewActor(engineInstance, new Vector3f((float) (5 * Math.random()), 20 + (float) (10 * Math.random()), (float) (5 * Math.random())), new Vector3f(-1f + (float) (2 * Math.random()), -1f + (float) (2 * Math.random()), -1f + (float) (2 * Math.random())), new Vector3f((float) Math.random(), (float) Math.random(), (float) Math.random())); parent.SpawnNewActor(engineInstance, new Vector3f( (float)(5 * Math.random()),20 + (float)(10 * Math.random()), (float)(5 * Math.random())),new Vector3f(-1f + (float)(2 * Math.random()),-1f + (float)(2 * Math.random()),-1f + (float)(2 * Math.random())),new Vector3f((float)Math.random(),(float)Math.random(),(float)Math.random()));
} }
if (input.keySinglePress(GLFW_KEY_R) && CurrentWeapon != Revolver) { if (input.keySinglePress(GLFW_KEY_R) && CurrentWeapon != Revolver) {
scene.RemoveActor(Musket); scene.RemoveActor(Musket);
scene.AddActor(Revolver); scene.AddActor(Revolver);
playerController.SwitchActor(Revolver);
CurrentWeapon = Revolver; CurrentWeapon = Revolver;
playerActor = Revolver; playerActor = Revolver;
parent.SetPlayerActorID(Revolver.GetID()); parent.SetPlayerActorID(Revolver.GetID());
@ -237,6 +219,7 @@ public class Scene implements IScene {
if (input.keySinglePress(GLFW_KEY_M) && CurrentWeapon != Musket) { if (input.keySinglePress(GLFW_KEY_M) && CurrentWeapon != Musket) {
scene.RemoveActor(Revolver); scene.RemoveActor(Revolver);
scene.AddActor(Musket); scene.AddActor(Musket);
playerController.SwitchActor(Musket);
CurrentWeapon = Musket; CurrentWeapon = Musket;
playerActor = Musket; playerActor = Musket;
parent.SetPlayerActorID(Musket.GetID()); parent.SetPlayerActorID(Musket.GetID());
@ -244,68 +227,25 @@ public class Scene implements IScene {
Revolver.AttachedCameraID = "NULL"; Revolver.AttachedCameraID = "NULL";
} }
if (input.keyPressed(GLFW_KEY_1)) { if(input.keyPressed(GLFW_KEY_1)) {
Gimbal.universalType = Gimbal.GimbalType.Move; Gimbal.universalType = Gimbal.GimbalType.Move;
} }
if (input.keyPressed(GLFW_KEY_2)) { if(input.keyPressed(GLFW_KEY_2)) {
Gimbal.universalType = Gimbal.GimbalType.Resize; Gimbal.universalType = Gimbal.GimbalType.Resize;
} }
if (input.keyPressed(GLFW_KEY_3)) { if(input.keyPressed(GLFW_KEY_3)) {
Gimbal.universalType = Gimbal.GimbalType.Rotate; Gimbal.universalType = Gimbal.GimbalType.Rotate;
} }
if (input.keyPressed(GLFW_KEY_TAB)) { if(input.keyPressed(GLFW_KEY_TAB)){
for (int i = 0; i < parent.ActorEditorComponents.size(); i++) { for(int i = 0; i < parent.ActorEditorComponents.size(); i++){
parent.ActorEditorComponents.get(i).UpdateAll(); parent.ActorEditorComponents.get(i).UpdateAll();
} }
} }
if (input.keySinglePress(GLFW_KEY_F2)) { if(input.keySinglePress(GLFW_KEY_F2)) {
EngineConfig.getInstance().SetRenderCollisionMesh(!EngineConfig.getInstance().RenderCollisionMeshes()); EngineConfig.getInstance().SetRenderCollisionMesh(!EngineConfig.getInstance().RenderCollisionMeshes());
} }
if (input.keyPressed(GLFW_KEY_W)) { playerController.GetControls().Input(input, FrameDiffNanoSeconds, CurrentWeapon, MOVEMENT_SPEED, parent, this);
if (playerActor == null) camera.MoveForward(MovementDist); if(GUI_MODE == 0 && !ActiveGUIs.contains("PerformanceOverlay")) ActiveGUIs.add("PerformanceOverlay");
else
playerActor.Move(new Vector3f(MovementDist * (float) Math.sin((camera.GetRotation().y)), 0, -MovementDist * (float) Math.cos((camera.GetRotation().y))));
}
if (input.keyPressed(GLFW_KEY_S)) {
if (playerActor == null) camera.MoveBackward(MovementDist);
else
playerActor.Move(new Vector3f(-MovementDist * (float) Math.sin((camera.GetRotation().y)), 0, MovementDist * (float) Math.cos((camera.GetRotation().y))));
}
if (input.keyPressed(GLFW_KEY_A)) {
if (playerActor == null) camera.MoveLeft(MovementDist);
else
playerActor.Move(new Vector3f(-MovementDist * (float) Math.cos((camera.GetRotation().y)), 0, -MovementDist * (float) Math.sin((camera.GetRotation().y))));
}
if (input.keyPressed(GLFW_KEY_D)) {
if (playerActor == null) camera.MoveRight(MovementDist);
else
playerActor.Move(new Vector3f(MovementDist * (float) Math.cos((camera.GetRotation().y)), 0, MovementDist * (float) Math.sin((camera.GetRotation().y))));
}
if (input.keyPressed(GLFW_KEY_SPACE)) {
if (playerActor == null) camera.MoveUp(MovementDist);
}
if (playerActor != null) {
RigidPhysicsController physicsController = (RigidPhysicsController) playerActor.GetPhysicsController();
RigidBody body = RigidBody.RigidBodyRegister.get(physicsController.RigidBodyID);
if (body != null && body.Grounded) {
Jumps = 0;
}
}
if (input.keySinglePress(GLFW_KEY_SPACE)) {
// camera.MoveUp(MovementDist);
if (Jumps < MaxJumps) {
if (playerActor != null) playerActor.Move(new Vector3f(0, 5f, 0));
Jumps++;
}
}
if (input.keyPressed(GLFW_KEY_LEFT_SHIFT)) {
if (playerActor == null) camera.MoveDown(MovementDist);
}
if (input.keyPressed(GLFW_KEY_LEFT_CONTROL)) {
camera.Sprint(true);
} else camera.Sprint(false);
if (GUI_MODE == 0 && !ActiveGUIs.contains("PerformanceOverlay")) ActiveGUIs.add("PerformanceOverlay");
else if (GUI_MODE != 0) ActiveGUIs.remove("PerformanceOverlay"); else if (GUI_MODE != 0) ActiveGUIs.remove("PerformanceOverlay");
} }