Skip to content

Commit

Permalink
Add rotation based on TMX flipping flag (#780)
Browse files Browse the repository at this point in the history
* add rotation based on TMX flip

* add rotation to example
  • Loading branch information
Jan Steinke authored Aug 22, 2021
1 parent dbed6c3 commit f3cbd80
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
2 changes: 2 additions & 0 deletions common/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,6 @@ type Tile struct {
Image *Texture
Drawables []Drawable
Animation *Animation
// Rotation of the Tile in degrees
Rotation float32
}
17 changes: 17 additions & 0 deletions common/tmx_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func (l *Level) unpackTiles(x, y, w, h int, d []tmx.Data) []*Tile {
X: float32(x),
Y: float32(y),
}))
tile.Rotation = convertFlipToRotation(t.Flipping)
ret = append(ret, tile)
l.pointMap[mapPoint{X: x, Y: y}] = tile
switch l.RenderOrder {
Expand Down Expand Up @@ -303,6 +304,7 @@ func (l *Level) unpackTiles(x, y, w, h int, d []tmx.Data) []*Tile {
X: float32(x),
Y: float32(y),
}))
tile.Rotation = convertFlipToRotation(t.Flipping)
ret = append(ret, tile)
l.pointMap[mapPoint{X: x, Y: y}] = tile
switch l.RenderOrder {
Expand Down Expand Up @@ -384,6 +386,21 @@ func (l *Level) tileFromGID(gid uint32, pt engo.Point) *Tile {
return ret
}

func convertFlipToRotation(flipping uint32) float32 {
flip_h := (flipping % tmx.HorizontalFlipFlag) != 0
flip_v := (flipping & tmx.VerticalFlipFlag) != 0
flip_d := (flipping & tmx.DiagonalFlipFlag) != 0
rotation := float32(0.0)
if flip_d {
rotation = 90
}
if flip_h && flip_v {
rotation += 180
}

return rotation
}

func getProperties(props []tmx.Property) []Property {
ret := make([]Property, 0)
for _, p := range props {
Expand Down
40 changes: 40 additions & 0 deletions common/tmx_level_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import "testing"

func TestConvertFlipToRotation(t *testing.T) {
tests := []struct {
name string
input uint32
expected float32
}{
{
name: "no flipping should be 0º rotation",
input: 0,
expected: 0,
},
{
name: "flipping diagonally should be 90º rotation",
input: 2684354560,
expected: 90,
},
{
name: "flipping horizontally and vertically should be 180º rotation",
input: 3221225472,
expected: 180,
},
{
name: "flipping diagonally, horizontally and vertically should be 270º rotation",
input: 1610612736,
expected: 270,
},
}

for _, test := range tests {
actual := convertFlipToRotation(test.input)

if actual != test.expected {
t.Errorf("%s expected=%f ; got=%f", test.name, test.expected, actual)
}
}
}
13 changes: 6 additions & 7 deletions demos/adventure/adventure.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ type Tile struct {
}

func (*DefaultScene) Preload() {

// Load character model
engo.Files.Load(model)

Expand Down Expand Up @@ -222,7 +221,6 @@ func (scene *DefaultScene) Setup(u engo.Updater) {

for _, tileLayer := range levelData.TileLayers {
for _, tileElement := range tileLayer.Tiles {

if tileElement.Image != nil {
tile := &Tile{BasicEntity: ecs.NewBasic()}
tile.RenderComponent = common.RenderComponent{
Expand All @@ -231,9 +229,13 @@ func (scene *DefaultScene) Setup(u engo.Updater) {
}
tile.SpaceComponent = common.SpaceComponent{
Position: tileElement.Point,
Width: 0,
Height: 0,
Width: tileElement.Width(),
Height: tileElement.Height(),
}
// Rotation of tile around the center
center := tile.Center()
tile.Rotation = tileElement.Rotation
tile.SetCenter(center)

if tileLayer.Name == "grass" {
tile.RenderComponent.SetZIndex(0)
Expand All @@ -250,7 +252,6 @@ func (scene *DefaultScene) Setup(u engo.Updater) {

for _, imageLayer := range levelData.ImageLayers {
for _, imageElement := range imageLayer.Images {

if imageElement.Image != nil {
tile := &Tile{BasicEntity: ecs.NewBasic()}
tile.RenderComponent = common.RenderComponent{
Expand Down Expand Up @@ -401,7 +402,6 @@ func (s *SpeedSystem) Remove(basic ecs.BasicEntity) {
}

func (s *SpeedSystem) Update(dt float32) {

for _, e := range s.entities {
speed := engo.GameWidth() * dt
e.SpaceComponent.Position.X = e.SpaceComponent.Position.X + speed*e.SpeedComponent.Point.X
Expand All @@ -422,7 +422,6 @@ func (s *SpeedSystem) Update(dt float32) {
e.SpaceComponent.Position.X = widthLimit
}
}

}

type controlEntity struct {
Expand Down
10 changes: 5 additions & 5 deletions demos/adventure/assets/example.tmx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="25" height="25" tilewidth="32" tileheight="32" nextobjectid="7">
<map version="1.5" tiledversion="1.7.1" orientation="orthogonal" renderorder="right-down" width="25" height="25" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="7">
<tileset firstgid="1" name="ts1" tilewidth="32" tileheight="32" tilecount="72" columns="12">
<image source="grass-tiles-2-small.png" width="384" height="192"/>
</tileset>
<tileset firstgid="73" name="ts3" tilewidth="32" tileheight="32" tilecount="64" columns="8">
<image source="tree2-final.png" width="256" height="256"/>
</tileset>
<layer name="grass" width="25" height="25">
<layer id="1" name="grass" width="25" height="25">
<data encoding="base64" compression="zlib">
eJztVcsOwzAIS3OH39nu+/9/Wic1kuXYJNp63AHRIoIxj+RorfVTDpBOenyzfVfHKXlp/kd7p/9hU/7ufJUv2wNifGzPBQ/MZ5zLy654VHV1vIZv0NkDsDpgj3Mh4jk+6FthBMXnWmCuiofD4Hpmm3k/IH83ly4+96OK0QGr6ofijTmjKB/sedUPjoUxcVbVjOM8VPu1mu8UeMOOfXHnMTeeI8c725zbas8dZ7WnYXxVPVTfsAaMkcLu7j+15ylE7Zea2YqHwlG+7l59bfJw9w/vdba5Prs8nCT5/MKjesf4rXB+iLUzB99qN88rHnfoO3nsvOd/HrV+A9pgE7E=
eJztVUkOgzAMDLmi+MpT2ns/wtN4Sp/WVMLSdDR2opZjD5bB2J6Ml7CUUmqXBaSS9me2z+rWxU7N72iv9O425R/FZ+dle4Mcb9t9wAPP43F22hWPrK4RL/dtFLsAVgVsj2siX8QHfTMMzLeV8uRa4FkVjwiD6+k+a8fw2BucP5rLKD/3I8tRASvrh+KN/FGUD/Y86wfncun1P3p9DqP4btv7tx1nYrRfo/n2muJuuB37EsUjX54j5r2Wz1nG+NGeR5x5Tzf4ruqjeHDfsAaMYcIe3X9qz02I2i81sxkPhaN8o3v1Mckjun9M4Ee7N+IRiZHPLzyy/xj/KyI/xJqZg291NM8jHlfoK3nM/M//PHL9AvxTF38=
</data>
</layer>
<layer name="trees" width="25" height="25">
<layer id="2" name="trees" width="25" height="25">
<data encoding="base64" compression="zlib">
eJzt1VUOAkEURNFhebi7u7u7y7K5O+jX0xNCCJWcP5JXH12DZX0mbnjghQ9+BBBEyKEbYUQQRQxxJJBEyqF+aWSQRQ55FFBEyaF+ZVRQRQ11NNBES3FD2q+NDrrooY8Bhhgpbkj7jTHBFDPMscASK8UNab81Nthihz0OOOKkuCHtd8YFV9xwxwNPvBQ3TPpJY9JPGpfGb79969KYbF0ak61LY7J1aexuXeet2N2Szluxu6X/W5HH5H9Bml/57kryBi05aII=
</data>
</layer>
<imagelayer name="clouds">
<imagelayer id="3" name="clouds">
<image source="cloud.png" width="400" height="218"/>
</imagelayer>
</map>

0 comments on commit f3cbd80

Please sign in to comment.