entity: physics rewrite

This commit is contained in:
2022-02-01 23:48:39 +08:00
parent 25872b257d
commit 37ee76e9fc
9 changed files with 201 additions and 256 deletions

View File

@@ -3,27 +3,26 @@ package entity
import (
"time"
"edgaru089.ml/go/gl01/internal/util"
"edgaru089.ml/go/gl01/internal/util/itype"
"edgaru089.ml/go/gl01/internal/world"
)
// EntityBehaviour describes the behaviour of a type of entity with the same Name.
// It should hold no data of its own.
type EntityBehaviour interface {
// Name returns the type Name of the behaviour.
Name() string
// Hitbox gets the size of the hitbox of the entity.
// Hitbox gets the hitbox(s) of the entity.
//
// The hitbox is positioned with the position coordinates pointing to
// the center of the bottom of the box.
Hitbox(pos itype.Vec3d, dataset itype.Dataset) itype.Vec3d
// The hitbox is in entity-local coordinates, originating from the position.
Hitbox(pos itype.Vec3d, dataset itype.Dataset) []itype.Boxd
// EyeHeight gets the height of the eye of the entity.
EyeHeight(pos itype.Vec3d, dataset itype.Dataset) float64
// Update is called on every frame.
// It should be used to update physics.
// It should be used to update the behaviour of the entity.
Update(pos itype.Vec3d, dataset itype.Dataset, world *world.World, deltaTime time.Duration)
}
@@ -45,14 +44,14 @@ func RegisterEntityBehaviour(b EntityBehaviour) bool {
// Entity is a struct holding a Behaviour, a Position and a Speed.
type Entity struct {
b EntityBehaviour
pos, speed itype.Vec3d // pos has a origin of the **center of the bottom** of the hitbox
pos, speed itype.Vec3d // pos should have a origin of the **center of the bottom** of the hitbox
ds itype.Dataset
name string // a shortcut to b.Name(), the typename
// physics stuff
onGround bool
worldbox []itype.Boxd
hp []itype.Vec3d
}
func NewEntity(typename string, pos itype.Vec3d) *Entity {
@@ -84,16 +83,16 @@ func (e *Entity) SetSpeed(speed itype.Vec3d) {
e.speed = speed
}
func (e *Entity) Hitbox() itype.Vec3d {
func (e *Entity) Hitbox() []itype.Boxd {
return e.b.Hitbox(e.pos, e.ds)
}
func (e *Entity) Accelerate(x, y, z float64) {
/*e.speed[0] += x
e.speed[0] += x
e.speed[1] += y
e.speed[2] += z*/
vec := itype.Vec3d{x, y, z}
e.speed = util.BunnyhopAccelerate(vec, e.speed, vec.Length(), 8)
e.speed[2] += z
/*vec := itype.Vec3d{x, y, z}
e.speed = util.BunnyhopAccelerate(vec, e.speed, vec.Length(), 8)*/
}
func (e *Entity) OnGround() bool {