Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ecs framework from lacking #35

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions cmd/rallymka/internal/ecs/manager.go

This file was deleted.

36 changes: 0 additions & 36 deletions cmd/rallymka/internal/ecs/renderer.go

This file was deleted.

27 changes: 27 additions & 0 deletions cmd/rallymka/internal/ecscomp/camera.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ecscomp

import (
"github.com/mokiat/gomath/sprec"
"github.com/mokiat/lacking/game/ecs"
"github.com/mokiat/lacking/render"
)

func SetCameraStand(entity *ecs.Entity, component *CameraStand) {
entity.SetComponent(CameraStandComponentID, component)
}

func GetCameraStand(entity *ecs.Entity) *CameraStand {
component := entity.Component(CameraStandComponentID)
if component == nil {
return nil
}
return component.(*CameraStand)
}

type CameraStand struct {
Target *ecs.Entity
AnchorPosition sprec.Vec3
AnchorDistance float32
CameraDistance float32
Camera *render.Camera
}
13 changes: 13 additions & 0 deletions cmd/rallymka/internal/ecscomp/identifiers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ecscomp

import (
"github.com/mokiat/lacking/game/ecs"
)

const (
PhysicsComponentID ecs.ComponentTypeID = iota
RenderComponentID
VehicleComponentID
PlayerControlComponentID
CameraStandComponentID
)
22 changes: 22 additions & 0 deletions cmd/rallymka/internal/ecscomp/physics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ecscomp

import (
"github.com/mokiat/lacking/game/ecs"
"github.com/mokiat/lacking/game/physics"
)

type Physics struct {
Body *physics.Body
}

func SetPhysics(entity *ecs.Entity, component *Physics) {
entity.SetComponent(PhysicsComponentID, component)
}

func GetPhysics(entity *ecs.Entity) *Physics {
component := entity.Component(PhysicsComponentID)
if component == nil {
return nil
}
return component.(*Physics)
}
17 changes: 17 additions & 0 deletions cmd/rallymka/internal/ecscomp/player.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ecscomp

import "github.com/mokiat/lacking/game/ecs"

func SetPlayerControl(entity *ecs.Entity, component *PlayerControl) {
entity.SetComponent(PlayerControlComponentID, component)
}

func GetPlayerControl(entity *ecs.Entity) *PlayerControl {
component := entity.Component(PlayerControlComponentID)
if component == nil {
return nil
}
return component.(*PlayerControl)
}

type PlayerControl struct{}
22 changes: 22 additions & 0 deletions cmd/rallymka/internal/ecscomp/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ecscomp

import (
"github.com/mokiat/lacking/game/ecs"
"github.com/mokiat/lacking/render"
)

func SetRender(entity *ecs.Entity, component *Render) {
entity.SetComponent(RenderComponentID, component)
}

func GetRender(entity *ecs.Entity) *Render {
component := entity.Component(RenderComponentID)
if component == nil {
return nil
}
return component.(*Render)
}

type Render struct {
Renderable *render.Renderable
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package ecs
package ecscomp

import (
"github.com/mokiat/gomath/sprec"
"github.com/mokiat/lacking/game/ecs"
"github.com/mokiat/lacking/game/physics"
"github.com/mokiat/lacking/game/physics/solver"
"github.com/mokiat/lacking/render"
)

type Entity struct {
Physics *PhysicsComponent
Render *RenderComponent
Vehicle *Vehicle
CameraStand *CameraStand
PlayerControl *PlayerControl
func SetVehicle(entity *ecs.Entity, component *Vehicle) {
entity.SetComponent(VehicleComponentID, component)
}

type PhysicsComponent struct {
Body *physics.Body
}

type RenderComponent struct {
Renderable *render.Renderable
}

type PlayerControl struct {
func GetVehicle(entity *ecs.Entity) *Vehicle {
component := entity.Component(VehicleComponentID)
if component == nil {
return nil
}
return component.(*Vehicle)
}

type Vehicle struct {
Expand All @@ -47,11 +40,3 @@ type Wheel struct {
AccelerationVelocity float32
DecelerationVelocity float32
}

type CameraStand struct {
Target *Entity
AnchorPosition sprec.Vec3
AnchorDistance float32
CameraDistance float32
Camera *render.Camera
}
45 changes: 45 additions & 0 deletions cmd/rallymka/internal/ecssys/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ecssys

import (
"github.com/mokiat/gomath/sprec"
"github.com/mokiat/lacking/game/ecs"
"github.com/mokiat/lacking/render"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/ecscomp"
)

func NewRenderer(ecsScene *ecs.Scene, scene *render.Scene) *Renderer {
return &Renderer{
ecsScene: ecsScene,
scene: scene,
}
}

type Renderer struct {
ecsScene *ecs.Scene
scene *render.Scene
}

func (r *Renderer) Update() {
result := r.ecsScene.Find(ecs.
Having(ecscomp.PhysicsComponentID).
And(ecscomp.RenderComponentID))
defer result.Close()

for result.HasNext() {
entity := result.Next()

physicsComp := ecscomp.GetPhysics(entity)
body := physicsComp.Body

renderComp := ecscomp.GetRender(entity)
renderable := renderComp.Renderable

renderable.Matrix = sprec.TransformationMat4(
body.Orientation().OrientationX(),
body.Orientation().OrientationY(),
body.Orientation().OrientationZ(),
body.Position(),
)
r.scene.Layout().InvalidateRenderable(renderable)
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
package ecs
package ecssys

import (
"time"

"github.com/mokiat/gomath/sprec"
"github.com/mokiat/lacking/app"
"github.com/mokiat/lacking/game/ecs"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/ecscomp"
)

func NewCameraStandSystem(ecsManager *Manager) *CameraStandSystem {
func NewCameraStandSystem(ecsScene *ecs.Scene) *CameraStandSystem {
return &CameraStandSystem{
ecsManager: ecsManager,
ecsScene: ecsScene,
}
}

type CameraStandSystem struct {
ecsManager *Manager
ecsScene *ecs.Scene
}

func (s *CameraStandSystem) Update(elapsedTime time.Duration, gamepad *app.GamepadState) {
for _, entity := range s.ecsManager.Entities() {
if cameraStand := entity.CameraStand; cameraStand != nil {
s.updateCameraStand(cameraStand, elapsedTime, gamepad)
}
result := s.ecsScene.Find(ecs.Having(ecscomp.CameraStandComponentID))
defer result.Close()

for result.HasNext() {
entity := result.Next()
cameraStand := ecscomp.GetCameraStand(entity)
s.updateCameraStand(cameraStand, elapsedTime, gamepad)
}
}

func (s *CameraStandSystem) updateCameraStand(cameraStand *CameraStand, elapsedTime time.Duration, gamepad *app.GamepadState) {
func (s *CameraStandSystem) updateCameraStand(cameraStand *ecscomp.CameraStand, elapsedTime time.Duration, gamepad *app.GamepadState) {
var (
targetPhysicsComp = ecscomp.GetPhysics(cameraStand.Target)
targetRenderComp = ecscomp.GetRender(cameraStand.Target)
)

var targetPosition sprec.Vec3
switch {
case cameraStand.Target.Physics != nil:
targetPosition = cameraStand.Target.Physics.Body.Position()
case cameraStand.Target.Render != nil:
targetPosition = cameraStand.Target.Render.Renderable.Matrix.Translation()
case targetPhysicsComp != nil:
targetPosition = targetPhysicsComp.Body.Position()
case targetRenderComp != nil:
targetPosition = targetRenderComp.Renderable.Matrix.Translation()
}
// we use a camera anchor to achieve the smooth effect of a
// camera following the target
Expand Down
Loading