Skip to content

Commit

Permalink
update control
Browse files Browse the repository at this point in the history
  • Loading branch information
Z2Y committed Sep 9, 2020
1 parent 58e130d commit c568b13
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 31 deletions.
Binary file added assets/building/building.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions assets/building/building.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<TextureAtlas imagePath="building.png" width="511" height="1200">
<SubTexture name="building_1.png" x="1" y="686" width="246" height="246"/>
<SubTexture name="building_2.png" x="1" y="1" width="348" height="416"/>
<SubTexture name="building_3.png" x="249" y="686" width="197" height="279"/>
<SubTexture name="building_4.png" x="1" y="419" width="275" height="265"/>
<SubTexture name="building_5.png" x="1" y="1102" width="148" height="97"/>
<SubTexture name="building_6.png" x="207" y="967" width="177" height="147"/>
<SubTexture name="building_7.png" x="386" y="967" width="124" height="133"/>
<SubTexture name="building_8.png" x="1" y="934" width="204" height="166"/>
<SubTexture name="building_9.png" x="278" y="419" width="232" height="254"/>
<SubTexture name="building_10.png" x="351" y="130" width="150" height="132"/>
<SubTexture name="building_11.png" x="351" y="1" width="158" height="127"/>
<SubTexture name="building_12.png" x="386" y="1102" width="97" height="91"/>
<SubTexture name="building_13.png" x="351" y="264" width="149" height="131"/>
</TextureAtlas>
7 changes: 6 additions & 1 deletion city/core/action/auto.go → city/core/control/auto.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package control

import (
"math/rand"
Expand All @@ -20,6 +20,7 @@ type ActionEntity struct {
common.SpaceComponent
WalkComponent

Offset engo.Point
ActionState
}

Expand Down Expand Up @@ -112,6 +113,10 @@ func (s *ActionState) Update(dt float32) {
}
}

func (s *ActionState) Finished() bool {
return s.elapsed >= s.duration
}

func (s *ActionState) Next() {
s.Code = rand.Intn(2)

Expand Down
93 changes: 86 additions & 7 deletions city/core/control.go → city/core/control/control.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package core
package control

import (
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
"github.com/Z2Y/trpgo/city/core"
"github.com/Z2Y/trpgo/city/core/input"
)

const (
UpButton = "up"
LeftButton = "left"
RightButton = "right"
DownButton = "down"
)

type ControlSystem struct {
grid *WorldSystem
grid *core.WorldSystem
hero *ActionEntity

camera *common.CameraSystem
touchHandler *input.TouchHandler
Expand Down Expand Up @@ -66,21 +75,90 @@ func (c *ControlSystem) setWorldCamera() {
Incremental: false})
}

func (c *ControlSystem) setupButton() {
engo.Input.RegisterButton(UpButton, engo.KeyW, engo.KeyArrowUp)
engo.Input.RegisterButton(LeftButton, engo.KeyA, engo.KeyArrowLeft)
engo.Input.RegisterButton(RightButton, engo.KeyD, engo.KeyArrowRight)
engo.Input.RegisterButton(DownButton, engo.KeyS, engo.KeyArrowDown)
}

func (c *ControlSystem) Remove(basic ecs.BasicEntity) {
// do nothing
c.hero = nil
}

func (c *ControlSystem) Update(float32) {
func (c *ControlSystem) Add(entity *ActionEntity) {
c.hero = entity
}

func (c *ControlSystem) Update(dt float32) {
for {
ok := c.touchHandler.Update()
c.update()
if c.hero != nil {
c.updateHero(dt)
c.followHero()
}
if !ok {
break
}
}
}

func (c *ControlSystem) update() {
func (c *ControlSystem) updateHero(dt float32) {
speed := engo.Point{X: 0, Y: 0}
if engo.Input.Button(UpButton).Down() {
speed.Y -= 1
}
if engo.Input.Button(LeftButton).Down() {
speed.X -= 1
}
if engo.Input.Button(RightButton).Down() {
speed.X += 1
}
if engo.Input.Button(DownButton).Down() {
speed.Y += 1
}

preState := c.hero.ActionState.Code
c.hero.ActionState.elapsed += dt

if speed.X != c.hero.ActionState.Speed.X || speed.Y != c.hero.ActionState.Speed.Y {
if speed.X == 0 && speed.Y == 0 {
c.hero.ActionState.Code = ActionIdle
} else {
c.hero.ActionState.Code = ActionWalking
}
c.hero.ActionState.elapsed = 0
c.hero.ActionState.duration = 0.2
c.hero.ActionState.Speed = speed
}

switch c.hero.ActionState.Code {
case ActionIdle:
if preState != ActionIdle {
engo.Mailbox.Dispatch(ActionMessage{BasicEntity: &c.hero.BasicEntity, State: c.hero.ActionState})
}
case ActionWalking:
if c.hero.ActionState.elapsed == 0 {
engo.Mailbox.Dispatch(ActionMessage{BasicEntity: &c.hero.BasicEntity, State: c.hero.ActionState})
}
}
}

func (c *ControlSystem) followHero() {
engo.Mailbox.Dispatch(common.CameraMessage{Axis: common.XAxis,
Value: c.hero.SpaceComponent.Position.X + c.hero.Offset.X,
Incremental: false})
engo.Mailbox.Dispatch(common.CameraMessage{Axis: common.YAxis,
Value: c.hero.SpaceComponent.Position.Y + c.hero.Offset.Y,
Incremental: false})

if engo.Input.Mouse.ScrollY != 0 {
engo.Mailbox.Dispatch(common.CameraMessage{Axis: common.ZAxis, Value: (engo.Input.Mouse.ScrollY * c.ZoomSpeed), Incremental: true})
}

}

func (c *ControlSystem) TrackCamera() {
var (
mouseX = engo.Input.Mouse.X // *c.camera.Z() + (c.camera.X()-(engo.GameWidth()/2)*c.camera.Z()+(engo.ResizeXOffset/2))/engo.GetGlobalScale().X
mouseY = engo.Input.Mouse.Y // *c.camera.Z() + (c.camera.Y()-(engo.GameHeight()/2)*c.camera.Z()+(engo.ResizeYOffset/2))/engo.GetGlobalScale().Y
Expand Down Expand Up @@ -120,13 +198,14 @@ func (c *ControlSystem) New(w *ecs.World) {
switch sys := system.(type) {
case *common.CameraSystem:
c.camera = sys
case *WorldSystem:
case *core.WorldSystem:
c.grid = sys
}
}

if c.grid != nil && c.camera != nil {
c.setWorldCamera()
}
c.setupButton()
c.touchHandler = input.NewTouchHandler()
}
5 changes: 2 additions & 3 deletions city/core/action/walk.go → city/core/control/walk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package control

import (
"github.com/EngoEngine/ecs"
Expand Down Expand Up @@ -78,8 +78,7 @@ func (s *WalkSystem) Remove(basic ecs.BasicEntity) {

func (s *WalkSystem) Update(dt float32) {
for _, e := range s.entities {
speed := engo.GameWidth() * dt
nextPosition := engo.Point{X: e.SpaceComponent.Position.X + speed*e.WalkComponent.Point.X, Y: e.SpaceComponent.Position.Y + speed*e.WalkComponent.Point.Y}
nextPosition := engo.Point{X: e.SpaceComponent.Position.X + e.WalkComponent.Point.X, Y: e.SpaceComponent.Position.Y + e.WalkComponent.Point.Y}

if nextPosition.Within(s.land) {
e.SpaceComponent.Position = nextPosition
Expand Down
25 changes: 25 additions & 0 deletions city/core/gridsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (w *WorldSystem) LoadWorldMap(worldMap *WorldMap) {
}

w.generateTrees(height_map, worldMap.xLen, worldMap.yLen)
w.generateBuildings(height_map, worldMap.xLen, worldMap.yLen)
}

func (w *WorldSystem) generateTrees(height_map *mapgenerator.HeightMap, width, height int) {
Expand Down Expand Up @@ -103,6 +104,30 @@ func (w *WorldSystem) generateTrees(height_map *mapgenerator.HeightMap, width, h
}
}

func (w *WorldSystem) generateBuildings(height_map *mapgenerator.HeightMap, width, height int) {
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
code := rand.Intn(1000)
if code < len(Buildings) && (height_map.Value(x, y) >= 10 && height_map.Value(x, y) < 30) {
space := &common.SpaceComponent{
Position: engo.Point{X: float32(x*(gridSize/2) + y*gridSize/2), Y: float32(y*gridSize/4 - x*gridSize/4)},
Width: gridSize,
Height: gridSize,
}
render := &common.RenderComponent{
Drawable: Entitys[Buildings[code]].Drawable,
Scale: Entitys[Buildings[code]].Scale,
StartZIndex: 2,
}
ground := w.ground[x][y]
if ground != nil {
ground.SubEntites = append(ground.SubEntites, &Grid{BasicEntity: ecs.NewBasic(), RenderComponent: render, SpaceComponent: space})
}
}
}
}
}

func (w *WorldSystem) Size() (float32, float32) {
return w.width, w.height
}
Expand Down
20 changes: 15 additions & 5 deletions city/core/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ var SampleWorldMap = WorldMap{
}

var (
Entitys = map[int]*common.RenderComponent{}
Lands = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Waters = []int{13}
Sand = []int{12}
Trees = []int{101, 102, 103, 104, 105}
Entitys = map[int]*common.RenderComponent{}
Lands = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
Waters = []int{13}
Sand = []int{12}
Trees = []int{101, 102, 103, 104, 105}
Buildings = []int{201, 202, 203, 204, 205}
)

func RegistRenderComponent(code int, d *common.RenderComponent) {
Expand All @@ -53,4 +54,13 @@ func InitRenderComponents() {
RegistRenderComponent(104, &common.RenderComponent{Scale: treeScale, Drawable: asset.LoadedSubSprite("foliagePack_007.png")})
RegistRenderComponent(105, &common.RenderComponent{Scale: treeScale, Drawable: asset.LoadedSubSprite("foliagePack_008.png")})
RegistRenderComponent(106, &common.RenderComponent{Scale: treeScale, Drawable: asset.LoadedSubSprite("foliagePack_012.png")})

buildingScale := engo.Point{X: 0.5, Y: 0.5}
RegistRenderComponent(201, &common.RenderComponent{Scale: buildingScale, Drawable: asset.LoadedSubSprite("building_1.png")})
RegistRenderComponent(202, &common.RenderComponent{Scale: buildingScale, Drawable: asset.LoadedSubSprite("building_2.png")})
RegistRenderComponent(203, &common.RenderComponent{Scale: buildingScale, Drawable: asset.LoadedSubSprite("building_3.png")})
RegistRenderComponent(204, &common.RenderComponent{Scale: buildingScale, Drawable: asset.LoadedSubSprite("building_4.png")})
RegistRenderComponent(205, &common.RenderComponent{Scale: buildingScale, Drawable: asset.LoadedSubSprite("building_5.png")})
RegistRenderComponent(206, &common.RenderComponent{Scale: buildingScale, Drawable: asset.LoadedSubSprite("building_6.png")})

}
42 changes: 42 additions & 0 deletions city/core/ui/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ui

import (
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
)

type Image struct {
UIBasic

Texture *common.Texture
Position engo.Point
Width float32
Height float32
}

func NewImage(img Image) *Image {

img.BasicEntity = ecs.NewBasic()

img.SpaceComponent = common.SpaceComponent{
Position: img.Position,
Width: img.Width,
Height: img.Height,
}

img.RenderComponent = common.RenderComponent{
Drawable: img.Texture,
Scale: engo.Point{X: img.Width / img.Texture.Width(), Y: img.Height / img.Texture.Height()},
}

img.SetShader(common.HUDShader)
img.SetZIndex(UILayerIndex)

return &img
}

func (img *Image) SetPosition(pos engo.Point) {
img.Position = pos
img.SpaceComponent.Position = pos
}
2 changes: 1 addition & 1 deletion city/core/ui/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func SetDefaultFont(URL string) {
fnt := &common.Font{
URL: URL,
FG: color.White,
Size: 32,
Size: 24,
}
err := fnt.CreatePreloaded()

Expand Down
1 change: 1 addition & 0 deletions city/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func Start(width, height int) {
MobileHeight: height,
FPSLimit: 120,
ScaleOnResize: true,
NotResizable: true,
GlobalScale: engo.Point{X: gameScale, Y: gameScale},
}

Expand Down
Loading

0 comments on commit c568b13

Please sign in to comment.