added getright in camera.cxx/.hxx

PickandThrow is done
adjusted thirdPersonCamera and PlayerController to work with each other
This commit is contained in:
Glence 2022-11-02 17:31:57 +08:00
parent 218fc89fc8
commit fc56973645
6 changed files with 182 additions and 43 deletions

View File

@ -244,9 +244,12 @@ namespace SHADE
SHCollider* collider = &component->GetCollider(i); SHCollider* collider = &component->GetCollider(i);
auto cursorPos = ImGui::GetCursorPos(); auto cursorPos = ImGui::GetCursorPos();
//collider->IsTrigger
if (collider->GetType() == SHCollider::Type::BOX) if (collider->GetType() == SHCollider::Type::BOX)
{ {
SHEditorWidgets::BeginPanel(std::format("{} Box Collider #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); SHEditorWidgets::BeginPanel(std::format("{} Box Collider #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger");
auto box = reinterpret_cast<SHBoundingBox*>(collider->GetShape()); auto box = reinterpret_cast<SHBoundingBox*>(collider->GetShape());
SHEditorWidgets::DragVec3 SHEditorWidgets::DragVec3
( (
@ -257,6 +260,7 @@ namespace SHADE
else if (collider->GetType() == SHCollider::Type::SPHERE) else if (collider->GetType() == SHCollider::Type::SPHERE)
{ {
SHEditorWidgets::BeginPanel(std::format("{} Sphere Collider #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); SHEditorWidgets::BeginPanel(std::format("{} Sphere Collider #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger");
auto sphere = reinterpret_cast<SHBoundingSphere*>(collider->GetShape()); auto sphere = reinterpret_cast<SHBoundingSphere*>(collider->GetShape());
SHEditorWidgets::DragFloat SHEditorWidgets::DragFloat
( (

View File

@ -123,5 +123,14 @@ namespace SHADE
} }
Vector3 Camera::GetRight()
{
auto system = SHSystemManager::GetSystem<SHCameraSystem>();
SHVec3 forward, up, right;
system->GetCameraAxis(*GetNativeComponent(), forward, right, up);
return Convert::ToCLI(right);
}
} }

View File

@ -66,5 +66,7 @@ namespace SHADE
void SetMainCamera(); void SetMainCamera();
void LookAt(Vector3 targetPosition); void LookAt(Vector3 targetPosition);
Vector3 GetForward(); Vector3 GetForward();
Vector3 GetRight();
}; };
} }

View File

@ -5,26 +5,94 @@ using static PlayerController;
public class PickAndThrow : Script public class PickAndThrow : Script
{ {
private PlayerController pc; private PlayerController pc;
public uint itemEID; public GameObject item;
Transform itemHoldLocation; public Vector3 throwForce = new Vector3(200.0f, 300.0f, 200.0f);
private Transform itemTransform;
private RigidBody itemRidibody;
private Transform raccoonHoldLocation;
private float lastXDir;
private float lastZDir;
private bool inRange = false;
public PickAndThrow(GameObject gameObj) : base(gameObj) { } public PickAndThrow(GameObject gameObj) : base(gameObj) { }
protected override void awake() protected override void awake()
{ {
pc = GetScript<PlayerController>(); pc = GetScript<PlayerController>();
itemHoldLocation = GetComponentInChildren<Transform>(); raccoonHoldLocation = GetComponentInChildren<Transform>();
if (raccoonHoldLocation == null)
Debug.Log("CHILD EMPTY");
itemTransform = item.GetComponent<Transform>();
if (itemTransform == null)
Debug.Log("Item transform EMPTY");
itemRidibody = item.GetComponent<RigidBody>();
if (itemRidibody == null)
Debug.Log("Item rb EMPTY");
} }
protected override void update() protected override void update()
{ {
if (pc != null && pc.holdItem) if (!pc.isMoveKeyPress)
{ {
if (pc.xAxisMove != 0)
{
lastXDir = pc.xAxisMove;
lastZDir = 0.0f;
} }
if (pc.zAxisMove != 0)
{
lastXDir = 0.0f;
lastZDir = pc.zAxisMove;
}
}
else
{
lastXDir = pc.xAxisMove;
lastZDir = pc.zAxisMove;
}
if (pc != null && inRange && !pc.holdItem && Input.GetKey(Input.KeyCode.E))
pc.holdItem = true;
if (pc != null && itemRidibody != null && itemTransform!= null && pc.holdItem)
{
itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition;
itemRidibody.IsGravityEnabled = false;
itemRidibody.LinearVelocity = Vector3.Zero;
itemRidibody.AngularVelocity = Vector3.Zero;
if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton))
{
pc.holdItem = false;
itemRidibody.IsGravityEnabled = true;
itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir));
itemRidibody.LinearVelocity += pc.rb.LinearVelocity;
Debug.Log($"x: {itemRidibody.LinearVelocity.x} z: {itemRidibody.LinearVelocity.z}");
}
}
else if(!pc.holdItem)
itemRidibody.IsGravityEnabled = true;
} }
protected override void onCollisionEnter(CollisionInfo info) protected override void onCollisionEnter(CollisionInfo info)
{ {
if (info.GameObject.Name == "item" && Input.GetKey(Input.KeyCode.E)) }
protected override void onTriggerEnter(CollisionInfo info)
{ {
pc.holdItem = true; Debug.Log("ENTER");
itemEID = info.GameObject.EntityId; if (info.GameObject == item && !pc.holdItem)
{
inRange = true;
} }
} }
protected override void onTriggerStay(CollisionInfo info)
{
Debug.Log("STAY");
}
protected override void onTriggerExit(CollisionInfo info)
{
Debug.Log("EXIT");
if (info.GameObject == item && !pc.holdItem)
{
inRange = false;
}
}
} }

View File

@ -12,17 +12,17 @@ public class PlayerController : Script
RUNNING, RUNNING,
JUMP, JUMP,
FALLING, FALLING,
HOLDING,
CAUGHT, CAUGHT,
TOTAL TOTAL
} }
private RigidBody rb; public RigidBody rb;
private Transform tranform; private Transform tranform;
private Camera cam;
//to be remove //to be remove
public float drag = 2.0f; public float drag = 2.0f;
public bool holdItem = false; public bool holdItem { get; set; }
[SerializeField] [SerializeField]
[Tooltip("The current state fo the raccoon")] [Tooltip("The current state fo the raccoon")]
public RaccoonStates currentState = RaccoonStates.IDILE; public RaccoonStates currentState = RaccoonStates.IDILE;
@ -41,9 +41,11 @@ public class PlayerController : Script
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; public float xAxisMove { get; set; }
private bool isMoveKeyPress = false; public float zAxisMove { get; set; }
public bool isMoveKeyPress { get; set; }
[SerializeField] [SerializeField]
[Tooltip("curr not working")] [Tooltip("curr not working")]
@ -68,12 +70,21 @@ public class PlayerController : Script
protected override void awake() protected override void awake()
{ {
isMoveKeyPress = false;
holdItem = false;
//rigidbody check //rigidbody check
rb = GetComponent<RigidBody>(); rb = GetComponent<RigidBody>();
if (rb == null) if (rb == null)
Debug.LogError("RigidBody is NULL!"); Debug.LogError("RigidBody is NULL!");
else else
{
rb.IsGravityEnabled = false;
rb.FreezeRotationX = true;
rb.FreezeRotationY = true;
rb.FreezeRotationZ = true;
rb.Drag = drag; rb.Drag = drag;
}
//Transform check //Transform check
tranform = GetComponent<Transform>(); tranform = GetComponent<Transform>();
@ -92,6 +103,9 @@ public class PlayerController : Script
protected override void update() protected override void update()
{ {
if (cam == null)
cam = GetComponentInChildren<Camera>();
//toRemove //toRemove
if (Input.GetKey(Input.KeyCode.G)) if (Input.GetKey(Input.KeyCode.G))
{ {
@ -117,7 +131,7 @@ public class PlayerController : Script
private void MoveKey() private void MoveKey()
{ {
if (Input.GetKey(Input.KeyCode.A)) /* if (Input.GetKey(Input.KeyCode.A))
xAxisMove = -1; xAxisMove = -1;
else if (Input.GetKey(Input.KeyCode.D)) else if (Input.GetKey(Input.KeyCode.D))
xAxisMove = 1; xAxisMove = 1;
@ -129,8 +143,43 @@ public class PlayerController : Script
else if (Input.GetKey(Input.KeyCode.S)) else if (Input.GetKey(Input.KeyCode.S))
zAxisMove = 1; zAxisMove = 1;
else else
zAxisMove = 0; zAxisMove = 0;*/
xAxisMove = 0;
zAxisMove = 0;
if (Input.GetKey(Input.KeyCode.W))
{
Vector3 camerAixs = cam.GetForward();
camerAixs.y = 0;
camerAixs.Normalise();
xAxisMove = camerAixs.x;
zAxisMove = camerAixs.z;
}
if (Input.GetKey(Input.KeyCode.S))
{
Vector3 camerAixs = cam.GetForward();
camerAixs.y = 0;
camerAixs.Normalise();
xAxisMove = -camerAixs.x;
zAxisMove = -camerAixs.z;
}
if (Input.GetKey(Input.KeyCode.A))
{
Vector3 camerAixs = cam.GetRight();
camerAixs.y = 0;
camerAixs.Normalise();
xAxisMove = -camerAixs.x;
zAxisMove = -camerAixs.z;
}
if (Input.GetKey(Input.KeyCode.D))
{
Vector3 camerAixs = cam.GetRight();
camerAixs.y = 0;
camerAixs.Normalise();
xAxisMove = camerAixs.x;
zAxisMove = camerAixs.z;
}
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded)
@ -169,6 +218,7 @@ public class PlayerController : Script
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded) if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
{ {
currentState = RaccoonStates.RUNNING; currentState = RaccoonStates.RUNNING;
holdItem = false;
if (!sprintIncreaseOnce) if (!sprintIncreaseOnce)
{ {
sprintIncreaseOnce = true; sprintIncreaseOnce = true;

View File

@ -19,10 +19,13 @@ namespace SHADE_Scripting
protected override void awake() protected override void awake()
{ {
AddComponent<Transform>();
if(!GetComponent<Camera>()) if(!GetComponent<Camera>())
{ {
AddComponent<Camera>(); AddComponent<Camera>();
} }
GetComponent<Camera>().SetMainCamera();
if (!GetComponent<CameraArm>()) if (!GetComponent<CameraArm>())
{ {
AddComponent<CameraArm>(); AddComponent<CameraArm>();
@ -31,6 +34,8 @@ namespace SHADE_Scripting
} }
protected override void update() protected override void update()
{
if (Input.GetMouseButton(Input.MouseCode.RightButton))
{ {
CameraArm arm = GetComponent<CameraArm>(); CameraArm arm = GetComponent<CameraArm>();
if (arm) if (arm)
@ -43,13 +48,14 @@ namespace SHADE_Scripting
{ {
arm.Pitch = pitchClamp; arm.Pitch = pitchClamp;
} }
else if(arm.Pitch < -pitchClamp) else if (arm.Pitch < 0)
{ {
arm.Pitch = -pitchClamp; arm.Pitch = 0;
} }
} }
} }
}
} }
} }