Character controller working better

Moved ECS systems to separate folders
better raycast normal result
This commit is contained in:
Antoine Pilote
2021-06-18 16:44:54 -04:00
parent 8f238cd4ca
commit 5b56e2ff9a
73 changed files with 1179 additions and 489 deletions

View File

@@ -5,21 +5,24 @@ import "Scripts/Math" for Vector3, Math
import "Scripts/Input" for Input
class CamScript is ScriptableEntity {
construct new() {
_Pitch = 0
_Yaw = 0
_mouseLastX = 0
_mouseLastY = 0
_BobHeight = 0.1
_BobSpeed = 5.0
_CamHeight = 0.5
_deltaTime = 0
Input.HideMouse()
}
init() {
}
init() {}
update(ts) {
_deltaTime = _deltaTime + ts
var x = Input.GetMouseX()
var y = Input.GetMouseY()
@@ -35,6 +38,7 @@ class CamScript is ScriptableEntity {
_Yaw = _Yaw + diffx
_Pitch = _Pitch + diffy
if(_Pitch > 89) _Pitch = 89
if(_Pitch < -89) _Pitch = -89
@@ -48,13 +52,27 @@ class CamScript is ScriptableEntity {
var newDir = Vector3.new(camX, camY, camZ)
var player = Scene.GetEntity("Player")
var playerScript = player.GetComponent("Script")
var velocity = playerScript.Velocity.Duplicate()
velocity.y = 0 // Dont count gravity in bob.
velocity = velocity.Sqrt()
var amount = (Math.Sin(_deltaTime * (velocity * 0.01)) / 2 + 1) * _BobHeight
Engine.Log("BobAmount: %(amount)")
var pTransform = player.GetComponent("Transform")
var pPos = pTransform.GetTranslation()
if(amount < 1 && amount > -1) amount = 0
var transform = this.GetComponent("Transform")
var newPos = Vector3.new(pPos.x, pPos.y + _CamHeight + amount , pPos.z)
transform.SetTranslation(newPos)
cam.SetDirection(newDir)
}
CheckInput() {
}
exit() {
}
exit() {}
}

View File

@@ -49,6 +49,16 @@ class Vector3 {
}
}
/(other) {
if(other is Vector3) {
return Vector3.new(_x / other.x,
_y / other.y,
_z / other.z)
} else if(other is Num) {
return Vector3.new(_x / other, _y / other, _z / other)
}
}
+(other) {
if(other is Vector3) {
return Vector3.new(_x + other.x,
@@ -79,6 +89,10 @@ class Vector3 {
_z = z
}
Duplicate() {
return Vector3.new(_x, _y, _z)
}
Sqrt() {
return Math.Sqrt_(_x, _y, _z)
}
@@ -94,4 +108,12 @@ class Vector3 {
var z = _z / length
return Vector3.new(x, y, z)
}
Angle(vec) {
this.Normalize() * vec.Normalize()
}
Length() {
this.Sqrt()
}
}

View File

@@ -7,16 +7,18 @@ import "Scripts/Physics" for Physics, CollisionResult
class PlayerScript is ScriptableEntity {
Velocity {_Velocity}
construct new() {
_InputDir = Vector3.new(0, 0, 0)
_Velocity = Vector3.new(0, 0, 0)
_Accel = 150
_Accel = 25
_Deccel = 0.92
_AirDeccel = 0.7
_Gravity = 20
_Gravity = 10
_MaxSpeed = 20
_Jump = 800
_Jump = 200
//Input.HideMouse()
}
@@ -24,7 +26,7 @@ class PlayerScript is ScriptableEntity {
Engine.Log("Player init")
}
update(ts) {
fixedUpdate(ts) {
this.CheckInput()
var camEntity = Scene.GetEntity("Camera")
@@ -37,25 +39,26 @@ class PlayerScript is ScriptableEntity {
var right = cam.GetRight()
right.y = 0
right = right.Normalize()
var new_Velocity = (dir * _InputDir.z) + (right * _InputDir.x)
var move = _InputDir
var new_Velocity = (dir * move.z) + (right * move.x)
var controller = this.GetComponent("CharacterController")
var transform = this.GetComponent("Transform")
var from = transform.GetTranslation()
var to = from - Vector3.new(0, 1, 0)
var normal = Physics.Raycast(from, to).Normal.Normalize()
Engine.Log("Normal col: (%(normal.x), %(normal.y), %(normal.z))")
var normal = Physics.Raycast(from, to).Normal
if(!controller.IsOnGround()) {
_Velocity.y = _Velocity.y - _Gravity
_Velocity.y = _Velocity.y -_Gravity
//_Velocity.x = _Velocity.x * _AirDeccel
//_Velocity.z = _Velocity.z * _AirDeccel
} else {
_Velocity.y = -20
_Velocity.y = -10
Engine.Log("Grav: %(_Velocity.y)")
if(Input.IsKeyPressed(32)) _Velocity.y = _Jump
@@ -65,6 +68,8 @@ class PlayerScript is ScriptableEntity {
_Velocity.x = _Velocity.x * _Deccel
_Velocity.z = _Velocity.z * _Deccel
}
Engine.Log("Grav: %(_Velocity.y)")
controller.MoveAndSlide(_Velocity * ts)
}

View File

@@ -26,7 +26,9 @@ class Scene {
return Camera.new(id)
} else if (component == "Transform") {
return TransformComponent.new(id)
}
} else if (component == "Script") {
return this.GetScript_(id)
}
}
@@ -34,8 +36,10 @@ class Scene {
// Components
//
// Transform
foreign static GetScript_(e)
foreign static GetTranslation_(e)
//foreign static SetTranslation_(e, x, y, z)
foreign static SetTranslation_(e, x, y, z)
//foreign static SetRotation_(e, x, y, z)
//foreign static SetScale_(e, x, y, z)
@@ -111,6 +115,9 @@ class TransformComponent {
return Vector3.new(result[0], result[1], result[2])
}
SetTranslation(t) {
Scene.SetTranslation_(_entityId, t.x, t.y, t.z)
}
}