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 the ui package from lacking #37

Merged
merged 3 commits into from
Jul 21, 2021
Merged
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
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# MacOS
.DS_Store

# Build
/target

# Assets
/assets
/assets
51 changes: 51 additions & 0 deletions cmd/rallymka/internal/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package internal

import (
"github.com/mokiat/lacking/game/graphics"
"github.com/mokiat/lacking/ui"
co "github.com/mokiat/lacking/ui/component"
"github.com/mokiat/lacking/ui/mat"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/game"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/global"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/store"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/ui/intro"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/ui/play"
)

func BootstrapApplication(window *ui.Window, gfxEngine graphics.Engine, gameController *game.Controller) {
co.Initialize(window, co.New(co.StoreProvider, func() {
co.WithData(co.StoreProviderData{
Entries: []co.StoreProviderEntry{
co.NewStoreProviderEntry(store.ApplicationReducer()),
},
})

co.WithChild("app", co.New(Application, func() {
co.WithContext(global.Context{
GFXEngine: gfxEngine,
GameController: gameController,
})
}))
}))
}

type ApplicationData = mat.SwitchData

var Application = co.Connect(co.ShallowCached(co.Define(func(props co.Properties) co.Instance {
return co.New(mat.Switch, func() {
co.WithData(props.Data())

co.WithChild("intro", co.New(intro.View, func() {}))
co.WithChild("play", co.New(play.View, func() {}))
})

})), co.ConnectMapping{
Data: func(props co.Properties) interface{} {
var appStore store.Application
co.InjectStore(&appStore)

return ApplicationData{
VisibleChildIndex: appStore.MainViewIndex,
}
},
})
170 changes: 104 additions & 66 deletions cmd/rallymka/internal/game/controller.go
Original file line number Diff line number Diff line change
@@ -9,58 +9,88 @@ import (
"github.com/mokiat/lacking/game/graphics"
"github.com/mokiat/lacking/game/physics"
"github.com/mokiat/lacking/resource"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/game/loading"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/game/simulation"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/ecssys"
)

type View interface {
Load(window app.Window, cb func())
Unload(window app.Window)
func NewController(gfxEngine graphics.Engine) *Controller {
gfxWorker := async.NewWorker(1024)
registry := resource.NewRegistry(resource.FileLocator{}, gfxEngine, gfxWorker)
return &Controller{
gfxEngine: gfxEngine,
physicsEngine: physics.NewEngine(),
ecsEngine: ecs.NewEngine(),
gfxWorker: gfxWorker,
registry: registry,

lastFrameTime: time.Now(),
}
}

type Controller struct {
app.NopController

window app.Window
gfxEngine graphics.Engine
physicsEngine *physics.Engine
ecsEngine *ecs.Engine
gfxWorker *async.Worker
registry *resource.Registry

Open(window app.Window)
Close(window app.Window)
lastFrameTime time.Time
freezeFrame bool

width int
height int

gfxScene graphics.Scene
physicsScene *physics.Scene
ecsScene *ecs.Scene

OnKeyboardEvent(window app.Window, event app.KeyboardEvent) bool
renderSystem *ecssys.Renderer
vehicleSystem *ecssys.VehicleSystem
cameraStandSystem *ecssys.CameraStandSystem

Update(window app.Window, elapsedSeconds float32)
Render(window app.Window, width, height int)
camera graphics.Camera
}

func NewController(gfxEngine graphics.Engine) *Controller {
gfxWorker := async.NewWorker(1024)
physicsEngine := physics.NewEngine()
ecsEngine := ecs.NewEngine()
func (c *Controller) Registry() *resource.Registry {
return c.registry
}

locator := resource.FileLocator{}
registry := resource.NewRegistry(locator, gfxEngine, gfxWorker)
func (c *Controller) GFXWorker() *async.Worker {
return c.gfxWorker
}

return &Controller{
gfxEngine: gfxEngine,
gfxWorker: gfxWorker,
registry: registry,
func (c *Controller) GFXEngine() graphics.Engine {
return c.gfxEngine
}

loadingView: loading.NewView(gfxEngine),
simulationView: simulation.NewView(gfxEngine, physicsEngine, ecsEngine, registry, gfxWorker),
func (c *Controller) GFXScene() graphics.Scene {
return c.gfxScene
}

lastFrameTime: time.Now(),
}
func (c *Controller) PhysicsScene() *physics.Scene {
return c.physicsScene
}

type Controller struct {
app.NopController
func (c *Controller) ECSScene() *ecs.Scene {
return c.ecsScene
}

window app.Window
gfxEngine graphics.Engine
gfxWorker *async.Worker
registry *resource.Registry
func (c *Controller) RenderSystem() *ecssys.Renderer {
return c.renderSystem
}

activeView View
loadingView View
simulationView View
func (c *Controller) VehicleSystem() *ecssys.VehicleSystem {
return c.vehicleSystem
}

lastFrameTime time.Time
width int
height int
func (c *Controller) CameraStandSystem() *ecssys.CameraStandSystem {
return c.cameraStandSystem
}

func (c *Controller) Camera() graphics.Camera {
return c.camera
}

func (c *Controller) OnCreate(window app.Window) {
@@ -69,8 +99,15 @@ func (c *Controller) OnCreate(window app.Window) {

c.gfxEngine.Create()

c.loadingView.Load(window, c.onLoadingAvailable)
c.simulationView.Load(window, c.onSimulationAvailable)
c.gfxScene = c.gfxEngine.CreateScene()
c.physicsScene = c.physicsEngine.CreateScene(0.015)
c.ecsScene = c.ecsEngine.CreateScene()

c.camera = c.gfxScene.CreateCamera()

c.renderSystem = ecssys.NewRenderer(c.ecsScene)
c.vehicleSystem = ecssys.NewVehicleSystem(c.ecsScene)
c.cameraStandSystem = ecssys.NewCameraStandSystem(c.ecsScene)
}

func (c *Controller) OnResize(window app.Window, width, height int) {
@@ -86,50 +123,51 @@ func (c *Controller) OnKeyboardEvent(window app.Window, event app.KeyboardEvent)
window.Close()
return true
}
if c.activeView != nil {
return c.activeView.OnKeyboardEvent(window, event)
if event.Code == app.KeyCodeF {
switch event.Type {
case app.KeyboardEventTypeKeyDown:
c.freezeFrame = true
return true
case app.KeyboardEventTypeKeyUp:
c.freezeFrame = false
return true
}
}
return false
return c.vehicleSystem.OnKeyboardEvent(event)
}

func (c *Controller) OnRender(window app.Window) {
c.gfxWorker.ProcessTryMultiple(10)

currentTime := time.Now()
elapsedTime := currentTime.Sub(c.lastFrameTime)
elapsedSeconds := float32(currentTime.Sub(c.lastFrameTime).Seconds())
c.lastFrameTime = currentTime

if c.activeView != nil {
c.activeView.Update(window, float32(elapsedTime.Seconds()))
c.activeView.Render(window, c.width, c.height)
if !c.freezeFrame {
var gamepad *app.GamepadState
if state, ok := window.GamepadState(0); ok {
gamepad = &state
}

c.physicsScene.Update(elapsedSeconds)
c.vehicleSystem.Update(elapsedSeconds, gamepad)
c.renderSystem.Update()
c.cameraStandSystem.Update(elapsedSeconds, gamepad)

c.gfxScene.Render(graphics.NewViewport(0, 0, c.width, c.height), c.camera)
}

window.Invalidate() // force redraw
}

func (c *Controller) OnDestroy(window app.Window) {
c.changeView(nil)
c.renderSystem = nil
c.vehicleSystem = nil
c.cameraStandSystem = nil

c.loadingView.Unload(window)
c.simulationView.Unload(window)
c.ecsScene.Delete()
c.physicsScene.Delete()
c.gfxScene.Delete()

c.gfxEngine.Destroy()
}

func (c *Controller) onLoadingAvailable() {
c.changeView(c.loadingView)
}

func (c *Controller) onSimulationAvailable() {
c.changeView(c.simulationView)
}

func (c *Controller) changeView(view View) {
if c.activeView != nil {
c.activeView.Close(c.window)
}
c.activeView = view
if c.activeView != nil {
c.activeView.Open(c.window)
}
}
54 changes: 0 additions & 54 deletions cmd/rallymka/internal/game/loading/view.go

This file was deleted.

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

import (
"github.com/mokiat/lacking/game/graphics"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/game"
)

type Context struct {
GFXEngine graphics.Engine
GameController *game.Controller
}
41 changes: 41 additions & 0 deletions cmd/rallymka/internal/store/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package store

import (
co "github.com/mokiat/lacking/ui/component"
"github.com/mokiat/rally-mka/cmd/rallymka/internal/scene"
)

const (
ViewIntro int = iota
ViewPlay
)

func ApplicationReducer() (co.Reducer, interface{}) {
return func(store *co.Store, action interface{}) interface{} {
var value Application
store.Inject(&value)

switch action := action.(type) {
case ChangeViewAction:
value.MainViewIndex = action.ViewIndex
case SetGameDataAction:
value.GameData = action.GameData
}
return value
}, Application{
MainViewIndex: ViewIntro,
}
}

type Application struct {
MainViewIndex int
GameData *scene.Data
}

type ChangeViewAction struct {
ViewIndex int
}

type SetGameDataAction struct {
GameData *scene.Data
}
Loading