Added C# interface for Camera and Camera Arm. Added some test scripts

This commit is contained in:
maverickdgg 2022-11-01 15:30:13 +08:00
parent d82bc8833f
commit f919d95c0b
12 changed files with 192 additions and 9 deletions

View File

@ -0,0 +1,51 @@
#include "SHpch.h"
#include "CameraArm.hxx"
namespace SHADE
{
CameraArm::CameraArm(Entity entity)
:Component(entity)
{
}
float CameraArm::Pitch::get()
{
return (GetNativeComponent()->GetPitch());
}
void CameraArm::Pitch::set(float val)
{
GetNativeComponent()->SetPitch(val);
}
float CameraArm::Yaw::get()
{
return (GetNativeComponent()->GetYaw());
}
void CameraArm::Yaw::set(float val)
{
GetNativeComponent()->SetYaw(val);
}
float CameraArm::ArmLength::get()
{
return (GetNativeComponent()->GetArmLength());
}
void CameraArm::ArmLength::set(float val)
{
GetNativeComponent()->SetArmLength(val);
}
bool CameraArm::LookAtCameraOrigin::get()
{
return GetNativeComponent()->lookAtCameraOrigin;
}
void CameraArm::LookAtCameraOrigin::set(bool val)
{
GetNativeComponent()->lookAtCameraOrigin = val;
}
}

View File

@ -0,0 +1,40 @@
#pragma once
// Project Includes
#include "Components/Component.hxx"
#include "Math/Vector3.hxx"
// External Dependencies
#include "Camera/SHCameraArmComponent.h"
namespace SHADE
{
public ref class CameraArm : public Component<SHCameraArmComponent>
{
internal:
CameraArm(Entity entity);
public:
property float Pitch
{
float get();
void set(float val);
}
property float Yaw
{
float get();
void set(float val);
}
property float ArmLength
{
float get();
void set(float val);
}
property bool LookAtCameraOrigin
{
bool get();
void set(bool val);
}
};
}

View File

@ -33,6 +33,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Components/Transform.hxx"
#include "Components\RigidBody.hxx"
#include "Components\Collider.hxx"
#include "Components/Camera.hxx"
#include "Components/CameraArm.hxx"
namespace SHADE
{
@ -248,6 +250,8 @@ namespace SHADE
componentMap.Add(createComponentSet<SHTransformComponent, Transform>());
componentMap.Add(createComponentSet<SHColliderComponent, Collider>());
componentMap.Add(createComponentSet<SHRigidBodyComponent, RigidBody>());
componentMap.Add(createComponentSet<SHCameraComponent, Camera>());
componentMap.Add(createComponentSet<SHCameraArmComponent, CameraArm>());
}
/*---------------------------------------------------------------------------------*/

View File

@ -26,4 +26,10 @@ namespace SHADE
{
return SHFrameRateController::GetRawDeltaTime();
}
float Time::DeltaTimeF::get()
{
return static_cast<float>(SHFrameRateController::GetRawDeltaTime());
}
}

View File

@ -37,5 +37,10 @@ namespace SHADE
{
double get();
}
static property float DeltaTimeF
{
float get();
}
};
}

View File

@ -99,4 +99,11 @@ namespace SHADE
return SHInputManager::GetKeyReleasedTime(static_cast<SHInputManager::SH_KEYCODE>(key));
}
Vector2 Input::GetMouseVelocity()
{
double velX, velY;
SHInputManager::GetMouseVelocity(&velX, &velY);
return Convert::ToCLI(SHVec2{ (float)velX,(float)velY });
}
}

View File

@ -321,5 +321,7 @@ namespace SHADE
/// <param name="key">The key to check.</param>
/// <returns>Time in seconds that the key was held.</returns>
static double GetMouseReleasedTime(MouseCode mouseButton);
static Vector2 GetMouseVelocity();
};
}

View File

@ -0,0 +1,22 @@
using System;
using SHADE;
namespace SHADE_Scripting
{
public class CameraControl :Script
{
public float turnSpeed = 0.5f;
public CameraControl(GameObject go) : base(go) { }
protected override void update()
{
//Camera
Camera cam = GetComponent<Camera>();
Vector2 mouseVel = Input.GetMouseVelocity();
cam.Pitch -= mouseVel.y * turnSpeed * (float)Time.DeltaTime;
cam.Yaw += mouseVel.x * turnSpeed * (float)Time.DeltaTime;
}
}
}

View File

@ -1,9 +0,0 @@
using System;
using SHADE;
namespace SHADE_Scripting
{
class CameraTest :Script
{
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SHADE;
namespace SHADE_Scripting
{
public class ThirdPersonCamera: Script
{
public float armLength = 4.0f;
public float turnSpeedPitch = 0.3f;
public float turnSpeedYaw = 0.5f;
public float pitchClamp = 45.0f;
public ThirdPersonCamera(GameObject go) : base(go) { }
protected override void awake()
{
if(!GetComponent<Camera>())
{
AddComponent<Camera>();
}
if (!GetComponent<CameraArm>())
{
AddComponent<CameraArm>();
}
GetComponent<CameraArm>().ArmLength = armLength;
}
protected override void update()
{
CameraArm arm = GetComponent<CameraArm>();
if(arm)
{
Vector2 vel = Input.GetMouseVelocity();
arm.Pitch -= vel.y * turnSpeedPitch * Time.DeltaTimeF;
arm.Yaw += vel.x * turnSpeedYaw * Time.DeltaTimeF;
if(arm.Pitch > pitchClamp)
{
arm.Pitch = pitchClamp;
}
else if(arm.Pitch < -pitchClamp)
{
arm.Pitch = -pitchClamp;
}
}
}
}
}