PlayerController and PickAndThrow #167

Merged
glencelow merged 21 commits from PlayerController into main 2022-11-02 17:40:51 +08:00
7 changed files with 125 additions and 83 deletions
Showing only changes of commit 8df82cae09 - Show all commits

View File

@ -29,7 +29,7 @@ namespace SHADE
{ {
system.second->Init(); system.second->Init();
#ifdef _DEBUG #ifdef _DEBUG
std::cout << system.first << " Init" << std::endl; SHLOG_INFO("Initialising System {}...", system.first)
#endif #endif
} }
} }

View File

@ -87,15 +87,6 @@ namespace SHADE
{ {
GetNativeComponent()->SetWorldScale(Convert::ToNative(val)); GetNativeComponent()->SetWorldScale(Convert::ToNative(val));
} }
Transform^ Transform::Parent::get()
{
auto node = SHSceneManager::GetCurrentSceneGraph().GetNode(owner.GetEntity());
if (!node)
throw gcnew System::InvalidOperationException("[Transform] Unable to retrieve SceneGraphNode for an Entity.");
const auto PARENT = node->GetParent();
return PARENT ? gcnew Transform(PARENT->GetEntityID()) : nullptr;
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Constructors */ /* Constructors */
@ -103,21 +94,4 @@ namespace SHADE
Transform::Transform(Entity entity) Transform::Transform(Entity entity)
: Component(entity) : Component(entity)
{} {}
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
void Transform::SetParent(Transform^ parent, bool worldPositionStays)
{
auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
auto node = sceneGraph.GetNode(owner.GetEntity());
if (!node)
throw gcnew System::InvalidOperationException("[Transform] Unable to retrieve SceneGraphNode for an Entity.");
if (parent)
node->SetParent(sceneGraph.GetNode(parent->owner.GetEntity()));
else
sceneGraph.SetParent(parent->owner.GetEntity(), nullptr);
}
} }

View File

@ -107,30 +107,6 @@ namespace SHADE
Vector3 get(); Vector3 get();
void set(Vector3 val); void set(Vector3 val);
} }
/// <summary>
/// Parent Transform that affects this Transform.
/// </summary>
property Transform^ Parent
{
Transform^ get();
}
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Sets the parent of this Transform component.
/// </summary>
/// <param name="parent">
/// Entity that contains the Transform component that this Transform will be
/// parented to. If null, unparenting will occur.
/// </param>
/// <param name="worldPositionStays">
/// If true, the transform values of this Transform component will retain their
/// pre-parent-change global transforms. The local transform values will be
/// modified to ensure that the global transforms do not change.
/// </param>
void SetParent(Transform^ parent, bool worldPositionStays);
}; };
} }

View File

@ -17,6 +17,7 @@ of DigiPen Institute of Technology is prohibited.
#include "GameObject.hxx" #include "GameObject.hxx"
// External Dependencies // External Dependencies
#include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHEntityManager.h"
#include "Scene/SHSceneGraph.h"
// Project Headers // Project Headers
#include "ECS.hxx" #include "ECS.hxx"
#include "Utility/Convert.hxx" #include "Utility/Convert.hxx"
@ -76,6 +77,27 @@ namespace SHADE
{ {
return entity; return entity;
} }
GameObject^ GameObject::Parent::get()
{
const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph();
const auto* ROOT = SCENE_GRAPH.GetRoot();
const auto* NODE = SCENE_GRAPH.GetNode(entity);
if (NODE == nullptr)
throw gcnew System::InvalidOperationException("Unable to retrieve SceneGraphNode for Entity " + entity.ToString());
const auto* PARENT = NODE->GetParent();
return PARENT != ROOT ? gcnew GameObject(PARENT->GetEntityID()) : nullptr;
}
void GameObject::Parent::set(GameObject^ newParent)
{
const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph();
if (newParent == nullptr)
SCENE_GRAPH.SetParent(entity, nullptr);
else
SCENE_GRAPH.SetParent(entity, newParent->EntityId);
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* GameObject Property Functions */ /* GameObject Property Functions */
@ -160,10 +182,12 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
GameObject::GameObject(const SHEntity& entity) GameObject::GameObject(const SHEntity& entity)
: entity { entity.GetEID() } : entity { entity.GetEID() }
, children{ gcnew System::Collections::ArrayList }
{} {}
GameObject::GameObject(Entity entity) GameObject::GameObject(Entity entity)
: entity { entity } : entity { entity }
, children{ gcnew System::Collections::ArrayList }
{} {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -93,6 +93,14 @@ namespace SHADE
{ {
Entity get(); Entity get();
} }
/// <summary>
/// The parent entity for this GameObject.
/// </summary>
property GameObject^ Parent
{
GameObject^ get();
void set(GameObject^);
}
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* GameObject Property Functions */ /* GameObject Property Functions */
@ -113,6 +121,7 @@ namespace SHADE
/// </param> /// </param>
void SetActive(bool active); void SetActive(bool active);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Component Access Functions */ /* Component Access Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -243,6 +252,7 @@ namespace SHADE
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
Entity entity; Entity entity;
System::Collections::ArrayList^ children;
public: public:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/

View File

@ -0,0 +1,30 @@
using SHADE;
using System;
using static PlayerController;
public class PickAndThrow : Script
{
private PlayerController pc;
public uint itemEID;
Transform itemHoldLocation;
public PickAndThrow(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{
pc = GetScript<PlayerController>();
itemHoldLocation = GetComponentInChildren<Transform>();
}
protected override void update()
{
if (pc != null && pc.holdItem)
{
}
}
protected override void onCollisionEnter(CollisionInfo info)
{
if (info.GameObject.Name == "item" && Input.GetKey(Input.KeyCode.E))
{
pc.holdItem = true;
itemEID = info.GameObject.EntityId;
}
}
}

View File

@ -10,7 +10,7 @@ public class PlayerController : Script
IDILE, IDILE,
WALKING, WALKING,
RUNNING, RUNNING,
INAIR, JUMP,
FALLING, FALLING,
HOLDING, HOLDING,
CAUGHT, CAUGHT,
@ -19,26 +19,48 @@ public class PlayerController : Script
private RigidBody rb; private RigidBody rb;
private Transform tranform; private Transform tranform;
private int xAxisMove;
private int zAxisMove; //to be remove
public float drag = 2.0f; public float drag = 2.0f;
private bool isMoveKeyPress = false; public bool holdItem = false;
[SerializeField]
[Tooltip("The current state fo the raccoon")]
public RaccoonStates currentState = RaccoonStates.IDILE; public RaccoonStates currentState = RaccoonStates.IDILE;
//Movement variables============================================================
[SerializeField]
[Tooltip("Max vel for walking")]
public float maxMoveVel = 2.0f; public float maxMoveVel = 2.0f;
[SerializeField]
[Tooltip("how much force is apply for walking")]
public float moveForce = 50.0f; public float moveForce = 50.0f;
[SerializeField]
[Tooltip("increase the moveForce and maxMoveVel by its amt")]
public float sprintMultiplier = 2.0f; public float sprintMultiplier = 2.0f;
private float oldForce; private float oldForce;
private float maxOldVel; private float maxOldVel;
private bool sprintIncreaseOnce = false; private bool sprintIncreaseOnce = false;
private int xAxisMove;
private int zAxisMove;
private bool isMoveKeyPress = false;
[SerializeField]
[Tooltip("curr not working")]
public float rotationFactorPerFrame = 1.0f; public float rotationFactorPerFrame = 1.0f;
//Jumping vars==================================================================
public float initialJumpVel; [SerializeField]
public float maxJumpHeight = 1.0f; [Tooltip("max height of the jump")]
public float maxJumpTime = 0.5f; public float maxJumpHeight = 4.0f;
private bool isJumping = false; [SerializeField]
public bool isGrounded = true; [Tooltip("max amt of time it will take for the jump")]
public float maxJumpTime = 0.75f;
[SerializeField]
[Tooltip("increase gravity when falling")]
public float fallMultipler = 2.0f;
private float initialJumpVel;
private bool isGrounded = true;
private float gravity = -9.8f; private float gravity = -9.8f;
private float groundGravity = -0.5f; private float groundGravity = -0.5f;
@ -80,7 +102,7 @@ public class PlayerController : Script
MoveKey(); MoveKey();
Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString()); //Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
} }
protected override void fixedUpdate() protected override void fixedUpdate()
@ -111,10 +133,10 @@ public class PlayerController : Script
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if(isMoveKeyPress && currentState != RaccoonStates.RUNNING) if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded)
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
if (!isMoveKeyPress) if (!isMoveKeyPress && isGrounded)
currentState = RaccoonStates.IDILE; currentState = RaccoonStates.IDILE;
} }
@ -144,7 +166,7 @@ public class PlayerController : Script
private void Sprint() private void Sprint()
{ {
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress) if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
{ {
currentState = RaccoonStates.RUNNING; currentState = RaccoonStates.RUNNING;
if(!sprintIncreaseOnce) if(!sprintIncreaseOnce)
@ -158,7 +180,7 @@ public class PlayerController : Script
} }
} }
if (Input.GetKeyUp(Input.KeyCode.LeftShift) && (currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)) if (Input.GetKeyUp(Input.KeyCode.LeftShift))
{ {
if(isMoveKeyPress) if(isMoveKeyPress)
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
@ -173,23 +195,17 @@ public class PlayerController : Script
{ {
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE) if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)
{ {
if (Input.GetKeyDown(Input.KeyCode.Space)&& isGrounded && rb != null) if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null)
{ {
isJumping = true; currentState = RaccoonStates.JUMP;
Vector3 v = rb.LinearVelocity; Vector3 v = rb.LinearVelocity;
v.y = initialJumpVel; v.y = initialJumpVel * 0.5f;
rb.LinearVelocity = v; rb.LinearVelocity = v;
} }
} }
if (rb != null) if(rb != null && !isGrounded && (rb.LinearVelocity.y < 0.0f || Input.GetKeyUp(Input.KeyCode.Space)))
{
if (rb.LinearVelocity.y > 0 && isJumping)
currentState = RaccoonStates.INAIR;
else if(rb.LinearVelocity.y < 0 && isJumping)
currentState = RaccoonStates.FALLING; currentState = RaccoonStates.FALLING;
}
} }
private void Rotation() private void Rotation()
@ -215,7 +231,7 @@ public class PlayerController : Script
if (rb != null) if (rb != null)
{ {
//check player vel.y if its close to zero its on the ground //check player vel.y if its close to zero its on the ground
if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f, 0.1f)) if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f))
isGrounded = true; isGrounded = true;
else else
isGrounded = false; isGrounded = false;
@ -224,8 +240,20 @@ public class PlayerController : Script
if (isGrounded) if (isGrounded)
v.y = groundGravity; v.y = groundGravity;
else if (currentState == RaccoonStates.FALLING)
{
float prevYVel = v.y;
float newYVel = v.y + (gravity * fallMultipler * (float)Time.DeltaTime);
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
else else
v.y += gravity * (float)Time.DeltaTime; {
float prevYVel = v.y;
float newYVel = v.y + (gravity * (float)Time.DeltaTime);
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
rb.LinearVelocity = v; rb.LinearVelocity = v;