Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Action system and speed fixes (#7)
Browse files Browse the repository at this point in the history
* Add .idea and macOS junk to gitignore

* Update modules

* Scale speed system using global scale

* Rename character system to action and fix speed

* Break character code out of system to util
* Rename system to action to reflect this
* Fix speed system by using delta only once
  • Loading branch information
eth0net committed Jan 16, 2022
2 parents 6795e99 + f83b29f commit c4397c6
Show file tree
Hide file tree
Showing 24 changed files with 670 additions and 317 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Tiled session data can cause weird issues
*.tiled-session

# build data
bin

# macOS junk
.DS_Store

# JetBrains IDE stuff
.idea
31 changes: 11 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@ go 1.16

require (
github.com/EngoEngine/ecs v1.0.5
github.com/EngoEngine/engo v1.0.6-0.20210314213613-b700def02163
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-gl/gl v0.0.0-20210315015930-ae072cafe09d // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210311203641-62640a716d48 // indirect
github.com/go-gl/mathgl v1.0.0 // indirect
github.com/hajimehoshi/go-mp3 v0.3.1 // indirect
github.com/hajimehoshi/oto v0.7.1 // indirect
github.com/jfreymuth/oggvorbis v1.0.3 // indirect
github.com/srwiley/oksvg v0.0.0-20210209000435-a757b9cbd472 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/veandco/go-sdl2 v0.4.5 // indirect
github.com/vulkan-go/vulkan v0.0.0-20201213112254-a536091a798a // indirect
golang.org/x/exp v0.0.0-20210220032938-85be41e4509f // indirect
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
golang.org/x/mobile v0.0.0-20210220033013-bdb1ca9a1e08 // indirect
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 // indirect
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa // indirect
golang.org/x/text v0.3.5 // indirect
github.com/EngoEngine/engo v1.0.6
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec // indirect
github.com/hajimehoshi/oto v1.0.1 // indirect
github.com/srwiley/oksvg v0.0.0-20211120171407-1837d6608d8c // indirect
github.com/veandco/go-sdl2 v0.4.10 // indirect
golang.org/x/exp v0.0.0-20211221223016-e29036178569 // indirect
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace github.com/EngoEngine/engo => github.com/eth0net/engo v1.0.6-0.20210314213613-b700def02163
476 changes: 418 additions & 58 deletions go.sum

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions scenes/game/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
"github.com/eth0net/magicgame/assets"
"github.com/eth0net/magicgame/systems/character"
"github.com/eth0net/magicgame/systems/action"
"github.com/eth0net/magicgame/util"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func (g *Scene) Setup(u engo.Updater) {
}
speedSystem.Level = tilemap.Level

player, err := character.NewCharacter(character.NewCharacterOptions{
player, err := util.NewCharacter(util.NewCharacterOptions{
Position: engo.Point{X: 800, Y: 600},
SpritesheetURL: spritesheetURL,
CellWidth: 32,
Expand All @@ -67,7 +67,7 @@ func (g *Scene) Setup(u engo.Updater) {
player.CollisionComponent.Main = 1
player.ControlComponent.Enabled = true

npc, err := character.NewCharacter(character.NewCharacterOptions{
npc, err := util.NewCharacter(util.NewCharacterOptions{
Position: engo.Point{X: 800, Y: 568},
SpritesheetURL: "spritesheets/Female 24-1.png",
CellWidth: 32,
Expand All @@ -80,10 +80,10 @@ func (g *Scene) Setup(u engo.Updater) {
}
npc.CollisionComponent.Main = 1
npc.CollisionComponent.Group = 1
npc.CharacterComponent.Schedule = character.Schedule{
Actions: []character.Action{
{Type: character.ActWalkTo, Point: engo.Point{X: 900, Y: 600}},
{Type: character.ActWalkTo, Point: engo.Point{X: 700, Y: 600}},
npc.ActionComponent.Schedule = action.Schedule{
Actions: []action.Action{
{Type: action.ActWalkTo, Point: engo.Point{X: 900, Y: 600}},
{Type: action.ActWalkTo, Point: engo.Point{X: 700, Y: 600}},
},
Loop: true,
}
Expand All @@ -98,7 +98,7 @@ func (g *Scene) Setup(u engo.Updater) {
g.World.AddSystemInterface(renderSystem, renderable, nil)
g.World.AddSystemInterface(animationSystem, animationable, nil)
g.World.AddSystemInterface(collisionSystem, collisionable, nil)
g.World.AddSystemInterface(characterSystem, characterable, nil)
g.World.AddSystemInterface(actionSystem, actionable, nil)
g.World.AddSystemInterface(controlSystem, controlable, nil)
g.World.AddSystemInterface(speedSystem, speedable, nil)
g.World.AddSystem(entityScroller)
Expand Down
6 changes: 3 additions & 3 deletions scenes/game/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package game

import (
"github.com/EngoEngine/engo/common"
"github.com/eth0net/magicgame/systems/character"
"github.com/eth0net/magicgame/systems/action"
"github.com/eth0net/magicgame/systems/control"
"github.com/eth0net/magicgame/systems/speed"
)
Expand All @@ -12,7 +12,7 @@ var (
animationSystem = &common.AnimationSystem{}
collisionSystem = &common.CollisionSystem{Solids: 1}
renderSystem = &common.RenderSystem{}
characterSystem = &character.CharacterSystem{}
actionSystem = &action.ActionSystem{}
controlSystem = &control.ControlSystem{}
speedSystem = &speed.SpeedSystem{}
)
Expand All @@ -22,7 +22,7 @@ var (
animationable *common.Animationable
collisionable *common.Collisionable
renderable *common.Renderable
characterable *character.Characterable
actionable *action.Actionable
controlable *control.Controlable
speedable *speed.Speedable
)
32 changes: 16 additions & 16 deletions systems/character/action.go → systems/action/action.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package character
package action

import (
"github.com/EngoEngine/engo"
Expand All @@ -7,43 +7,43 @@ import (
// ActionType indicates the type of an Action.
type ActionType int

// ActionTypes available for use in Character Schedules.
// ActionTypes available for use in Schedules.
const (
// ActStop stops Character movement, preserving the direction.
// ActStop stops movement, preserving the direction.
ActStop ActionType = iota

// ActTurn turns the Character to face the direction
// indicated by Point and stops Character movement.
// ActTurn turns the Entity to face the direction
// indicated by Point and stops movement.
ActTurn

// ActWalk makes the Character walk in the direction
// ActWalk makes the Entity walk in the direction
// indicated by Point until Duration has passed.
ActWalk

// ActRun makes the Character run in the direction
// ActRun makes the Entity run in the direction
// indicated by Point until Duration has passed.
ActRun

// ActTeleport teleports the Character in the direction
// ActTeleport teleports the Entity in the direction
// indicated by Point until Duration has passed.
ActTeleport

// ActTurnTo turns the Character to face the location
// ActTurnTo turns the Entity to face the location
// indicated by Point and updates the SpeedComponent.
ActTurnTo

// ActWalkTo makes the Character walk to the location indicated by Point.
// ActWalkTo walks the Entity to the location indicated by Point.
ActWalkTo

// ActRunTo makes the Character run to the location indicated by Point.
// ActRunTo runs the Entity to the location indicated by Point.
ActRunTo

// ActTeleportTo teleports the Character to the location indicated by Point.
// ActTeleportTo teleports the Entity to the location indicated by Point.
ActTeleportTo

// Other ideas:
// FollowPath
// FollowCharacter
// FollowEntity
// FollowSpaceComponent
// Interact
// Attack
Expand All @@ -53,10 +53,10 @@ const (
// Anything else
)

// An Action defines a single act for a Character.
// Actions are used to create Character Schedules.
// An Action defines a single act for a Entity.
// Actions are used to create Schedules.
type Action struct {
// Type determines what to do to the Character
// Type determines what to do to the Entity
// and how to interpret the other Action fields.
Type ActionType

Expand Down
11 changes: 11 additions & 0 deletions systems/action/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package action

// ActionComponent marks entities for use with a ActionSystem.
type ActionComponent struct {
Schedule
}

// GetActionComponent provides type safe access to ActionComponent.
func (ac *ActionComponent) GetActionComponent() *ActionComponent {
return ac
}
14 changes: 14 additions & 0 deletions systems/action/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package action

// Animation names for Entities to implement,
// used by ActionSystem to trigger Animations.
const (
AnimationMoveUp string = "MoveUp"
AnimationMoveDown string = "MoveDown"
AnimationMoveLeft string = "MoveLeft"
AnimationMoveRight string = "MoveRight"
AnimationStopUp string = "StopUp"
AnimationStopDown string = "StopDown"
AnimationStopLeft string = "StopLeft"
AnimationStopRight string = "StopRight"
)
31 changes: 9 additions & 22 deletions systems/character/entity.go → systems/action/entity.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package character
package action

import (
"github.com/EngoEngine/ecs"
Expand All @@ -7,17 +7,17 @@ import (
"github.com/eth0net/magicgame/systems/speed"
)

type characterEntity struct {
type actionEntity struct {
*ecs.BasicEntity
*common.AnimationComponent
*common.SpaceComponent
*CharacterComponent
*ActionComponent
*speed.SpeedComponent
}

// setAnimation sets the characterEntity Animation
// setAnimation sets the actionEntity Animation
// using the current state of the SpeedComponent.
func (ce *characterEntity) setAnimation() {
func (ce *actionEntity) setAnimation() {
point := ce.GetSpeedComponent().Point
currentAnimation := ce.AnimationComponent.CurrentAnimation
if currentAnimation == nil {
Expand Down Expand Up @@ -62,10 +62,10 @@ func (ce *characterEntity) setAnimation() {
}
}

// runSchedule executes code on the characterEntity
// runSchedule executes code on the actionEntity
// according to the current Action for the Schedule
// and then updates the Schedule accordingly.
func (ce *characterEntity) runSchedule(dt float32) {
func (ce *actionEntity) runSchedule(dt float32) {
actionPtr := ce.CurrentAction()
if actionPtr == nil {
return
Expand All @@ -79,43 +79,35 @@ func (ce *characterEntity) runSchedule(dt float32) {
case ActStop:
ce.SpeedComponent.Point = engo.Point{}
actComplete = true
break

case ActTurn:
ce.SpeedComponent.Point = action.Point
ce.setAnimation()
ce.SpeedComponent.Point = engo.Point{}
ce.setAnimation()
actComplete = true
break

case ActWalk:
vector, _ := action.Point.Normalize()
vector.MultiplyScalar(dt)

engo.Mailbox.Dispatch(speed.SpeedMessage{
BasicEntity: ce.BasicEntity,
Point: vector,
})
actComplete = true
break

case ActRun:
vector, _ := action.Point.Normalize()
vector.MultiplyScalar(dt * 2)

vector.MultiplyScalar(2)
engo.Mailbox.Dispatch(speed.SpeedMessage{
BasicEntity: ce.BasicEntity,
Point: vector,
})
actComplete = true
break

case ActTeleport:
vector := action.Point.MultiplyScalar(dt)
ce.SpaceComponent.Position.Add(*vector)
actComplete = true
break

case ActTurnTo:
direction := action.Point.Subtract(ce.SpaceComponent.Position)
Expand All @@ -124,39 +116,34 @@ func (ce *characterEntity) runSchedule(dt float32) {
ce.SpeedComponent.Point = engo.Point{}
ce.setAnimation()
actComplete = ce.SpeedComponent.Point == action.Point
break

case ActWalkTo:
vector := action.Point
vector.Subtract(ce.SpaceComponent.Position)
vector, _ = vector.Normalize()
vector.MultiplyScalar(dt)
engo.Mailbox.Dispatch(speed.SpeedMessage{
BasicEntity: ce.BasicEntity,
Point: vector,
})
if ce.SpaceComponent.Position.Equal(action.Point) {
actComplete = true
}
break

case ActRunTo:
direction := action.Point.Subtract(ce.SpaceComponent.Position)
vector, _ := direction.Normalize()
vector.MultiplyScalar(dt * 2)
vector.MultiplyScalar(2)
engo.Mailbox.Dispatch(speed.SpeedMessage{
BasicEntity: ce.BasicEntity,
Point: vector,
})
if ce.SpaceComponent.Position.Equal(action.Point) {
actComplete = true
}
break

case ActTeleportTo:
ce.SpaceComponent.Position = action.Point
actComplete = true
break
}

ce.currentDuration += dt
Expand Down
Loading

0 comments on commit c4397c6

Please sign in to comment.