all repos — engine.git @ 70d5a5f53436c4848616132f2ff47c4036adc224

Unnamed repository; edit this file 'description' to name the repository.

src/simulation/simulation.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
package simulation

import "core:time"
import "core:fmt"
import cm "../common"

UP :: [3]cm.iFP{0,cm.METER,0}
DEFAULT_WALK_FORCE : cm.iFP : 21474836480
DEFAULT_WALK_DRAG : cm.iFP : 2147483648
DEFAULT_GROUND_DRAG : cm.iFP : 21474836480
GRAVITY : cm.iFP : -21045340160
ENTITY_LIMIT : int : 2048
MAX_SNAPSHOTS : int : 3

Stage :: enum {
	NON_DETERMINISTIC,
	DETERMINISTIC,
	COLLISION,
}

Start :: proc() {
	MakeComponentList(&entities)
	MakeComponentList(&entityDerivX)
	MakeComponentList(&entityAABB)
	MakeComponentList(&entityCollisionCallback)
	MakeComponentList(&entityGravity)
	InitOctree()
	worldClock = cm.Clock {
		frames = 0,
		period = 15 * time.Millisecond,
		lastTick = time.tick_now()
	}
}

End :: proc() {
	DestroyComponentList(&entities)
	DestroyComponentList(&entityDerivX)
	DestroyComponentList(&entityAABB)
	DestroyComponentList(&entityCollisionCallback)
	DestroyComponentList(&entityGravity)
	DestroyOctree()
}

MakeComponentList :: proc(list: ^[MAX_SNAPSHOTS]^[dynamic]$T ) {
	for i in 0..<MAX_SNAPSHOTS {
		list[i]^ = make(type_of(list[i]^),0,ENTITY_LIMIT)
	}
}

DestroyComponentList :: proc(list: ^[MAX_SNAPSHOTS]^[dynamic]$T ) {
	for i in 0..<MAX_SNAPSHOTS {
		delete(list[i]^)
	}
}

Update :: proc(clk: ^cm.Clock) {
	fmt.printfln("--------------------------------------------%i", clk.frames)
	delta := DoubleToInt(time.duration_seconds(time.tick_since(clk.lastTick)))
	ResetForces(entityDerivX[0])

	//Non-Deterministic Forces
	PlayerAlignToCamera(&cameraFoward,&player)
	PlayerMove(&player,&commandBuffer)
	PlayerDrag(&player, commandBuffer)
	commandBuffer = {}

	//Deterministic Forces
	ApplyGravity(entityDerivX[0], entityGravity[0])

	//Collisions
	MoveCast(delta,entityDerivX[0], entityAABB[0], entityCollisionCallback[0],&octree)

	//Apply Forces
	KinematicUpdate(delta, entityDerivX[0], entityAABB[0], &octree)
	UpdateClient(entityDerivX[0], &clientEntityDerivX)

}

UpdateClient :: proc(entities: ^[dynamic]Maybe(DerivX), clientEntities: ^[dynamic]Maybe(DerivXFloat)) {
	for e,i in clientEntities^ {
		cEnt, ok1 := e.?
		ent, ok2 := entities[i].?
		if !(ok1 && ok2) do continue
		cEnt.x = ToFloatVector(ent.x)
		cEnt.v = ToFloatVector(ent.v)
		cEnt.a = ToFloatVector(ent.a)
		clientEntities^[i] = cEnt
	}
}

AvailableSlot :: proc() -> (index: int, found: bool) {
	for e,i in entities[0] {
		if e == false {
			entities[0][i] = true
			return i, true
		}
	}
	return -1, false
}