src/player/player_server.odin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
package player
import "core:fmt"
import cm "../common"
import sim "../simulation"
import clt "../client"
PlayerInit :: proc() {
using cm
index, found := sim.AvailableSlot()
if !found do return
player.id = index
player.foward = [3]cm.iFP{0,0,METER}
player.kinematics = sim.entityDerivX[0]
player.clientKinematics = clt.clientEntities
player.walkForce = sim.DEFAULT_WALK_FORCE
player.groundDrag = sim.DEFAULT_GROUND_DRAG
player.walkDrag = sim.DEFAULT_WALK_DRAG
x := sim.DerivX{
x = [3]iFP{0.0,FOUR_METER,0.0},
v = [3]iFP{0.0,0.0,0.0},
a = [3]iFP{0.0,0.0,0.0},
}
cx := clt.DerivXFloat{
x = [3]f32{0.0,0.0,0.0},
v = [3]f32{0.0,0.0,0.0},
a = [3]f32{0.0,0.0,0.0},
}
bb := sim.AABB{
solid = true,
min = [3]cm.iFP{-Div(METER,TWO_METER),-METER,-Div(METER,TWO_METER)},
max = [3]cm.iFP{Div(METER,TWO_METER),METER,Div(METER,TWO_METER)}
}
sim.entities[0]^[index] = x
clt.clientEntities^[index] = cx
sim.entityAABB[0]^[index] = bb
sim.entityCollisionCallback[0]^[index] = sim.RigidInelasticCollision
sim.entityGravity[0]^[index] = true
// Add Octree to funcuntion procs
AddEntityToNode(index,FindEnclosingNode(bb,x.x,&octree))
}
PlayerMove :: proc() {
using cm
if commandBuffer^ == {} do return
kinematics, ok := player^.kinematics[player^.id].?
if !ok do return
vecLen: i64 = 0
if Command.MOVE_FOWARDS in commandBuffer^ {
kinematics.a += VecMul(player.foward,player.walkForce)
vecLen += 1
}
if Command.MOVE_BACKWARDS in commandBuffer^ {
kinematics.a -= VecMul(player.foward,player.walkForce)
vecLen += 1
}
if Command.STRAFE_LEFT in commandBuffer^ {
kinematics.a -= VecMul(player.rightward,player.walkForce)
vecLen += 1
}
if Command.STRAFE_RIGHT in commandBuffer^ {
kinematics.a += VecMul(player.rightward,player.walkForce)
vecLen += 1
}
if Command.JUMP in commandBuffer^ {
kinematics.v += VecMul(sim.UP, FloatToInt(10.0))
}
if vecLen == 2 {
kinematics.a = VecDiv(kinematics.a,SQRT2)
}
player^.kinematics[player^.id] = kinematics
}
PlayerDrag :: proc() {
using cm
kinematics, ok := player^.kinematics[player^.id].?
if !ok do return
kinematics.a -= VecMul(kinematics.v,player.walkDrag)
if Command.MOVE_FOWARDS not_in commandBuffer && Command.MOVE_BACKWARDS in commandBuffer {
force := VecMul( player.foward, Mul(player.groundDrag , DotProduct(kinematics.v,player.foward)) )
if DotProduct(kinematics.v,player.foward) > 0 do kinematics.a -= force
}
if Command.MOVE_BACKWARDS not_in commandBuffer && Command.MOVE_FOWARDS in commandBuffer {
force := VecMul( player.foward, Mul(player.groundDrag , DotProduct(kinematics.v,player.foward)) )
if DotProduct(kinematics.v,player.foward) < 0 do kinematics.a -= force
}
if Command.STRAFE_RIGHT not_in commandBuffer && Command.STRAFE_LEFT in commandBuffer {
force := VecMul( player.rightward, Mul(player.groundDrag , DotProduct(kinematics.v,player.rightward)) )
if DotProduct(kinematics.v,player.rightward) > 0 do kinematics.a -= force
}
if Command.STRAFE_LEFT not_in commandBuffer && Command.STRAFE_RIGHT in commandBuffer {
force := VecMul( player.rightward, Mul(player.groundDrag , DotProduct(kinematics.v,player.rightward)) )
if DotProduct(kinematics.v,player.rightward) < 0 do kinematics.a -= force
}
player^.kinematics[player^.id] = kinematics
}
PlayerAlignToCamera :: proc(camFoward: ^[3]f32, player: ^Player) {
foward := ToIntVector(camFoward^)
player^.foward = foward
player.rightward = CrossProduct(foward,UP)
}
|