Skip to content

Commit

Permalink
sprite.Turn(ti *spx.TurningInfo)
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Nov 1, 2021
1 parent 5c88b75 commit 9b42917
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 0 deletions.
16 changes: 16 additions & 0 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ func (p *Sprite) Heading() float64 {
// Turn(degree)
// Turn(spx.Left)
// Turn(spx.Right)
// Turn(ti *spx.TurningInfo)
func (p *Sprite) Turn(val interface{}) {
if debugInstr {
log.Println("Turn", p.name, val)
Expand All @@ -711,6 +712,9 @@ func (p *Sprite) Turn(val interface{}) {
delta = float64(v)
case float64:
delta = v
case *TurningInfo:
p.doTurnTogether(v) // don't animate
return
default:
panic("Turn: unexpected input")
}
Expand Down Expand Up @@ -757,6 +761,18 @@ func (p *Sprite) setDirection(dir float64, change bool) {
p.direction = normalizeDirection(dir)
}

func (p *Sprite) doTurnTogether(ti *TurningInfo) {
/*
x’ = x0 + cos * (x-x0) - sin * (y-y0)
y’ = y0 + sin * (x-x0) + cos * (y-y0)
*/
x0, y0 := ti.Obj.x, ti.Obj.y
dir := ti.Dir()
sin, cos := math.Sincos(dir * (math.Pi / 180))
p.x, p.y = x0+cos*(p.x-x0)+sin*(p.y-y0), y0-sin*(p.x-x0)+cos*(p.y-y0)
p.direction = normalizeDirection(p.direction + dir)
}

// -----------------------------------------------------------------------------

func (p *Sprite) Size() float64 {
Expand Down
3 changes: 3 additions & 0 deletions test/TurnTogether/Banana.spx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Monkey.onTurning ti => {
turn ti
}
3 changes: 3 additions & 0 deletions test/TurnTogether/Banana2.spx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Monkey.onTurning ti => {
turn ti.dir
}
7 changes: 7 additions & 0 deletions test/TurnTogether/Monkey.spx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
onKey KeyUp, => {
turn -30
}

onKey KeyDown, => {
turn 30
}
7 changes: 7 additions & 0 deletions test/TurnTogether/index.gmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var (
Banana Banana
Banana2 Banana2
Monkey Monkey
)

run "res", {Title: "Turn Together (by Go+)"}
32 changes: 32 additions & 0 deletions test/TurnTogether/res/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"costumes": [
{
"bitmapResolution": 1,
"name": "backdrop1",
"path": "../../res/lake.jpg",
"x": 240,
"y": 180
}
],
"currentCostumeIndex": 0,
"zorder": [
{
"type": "sprite",
"target": "Banana",
"x": -100,
"y": -21
},
{
"type": "sprite",
"target": "Banana2",
"x": 80,
"y": -81
},
{
"type": "sprite",
"target": "Monkey",
"x": 0,
"y": 50
}
]
}
16 changes: 16 additions & 0 deletions test/TurnTogether/res/sprites/Banana/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"costumeSet": {
"bitmapResolution": 1,
"faceRight": 180,
"path": "../../../../res/banana.png",
"nx": 58
},
"currentCostumeIndex": 0,
"heading": 90,
"isDraggable": false,
"rotationStyle": "normal",
"size": 1,
"visible": true,
"x": -100,
"y": -21
}
16 changes: 16 additions & 0 deletions test/TurnTogether/res/sprites/Banana2/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"costumeSet": {
"bitmapResolution": 1,
"faceRight": 180,
"path": "../../../../res/banana.png",
"nx": 58
},
"currentCostumeIndex": 0,
"heading": 90,
"isDraggable": false,
"rotationStyle": "normal",
"size": 1,
"visible": true,
"x": -100,
"y": -21
}
16 changes: 16 additions & 0 deletions test/TurnTogether/res/sprites/Monkey/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"costumeSet": {
"bitmapResolution": 1,
"faceRight": 180,
"path": "../../../../res/monkey.png",
"nx": 96
},
"currentCostumeIndex": 0,
"heading": 90,
"isDraggable": false,
"rotationStyle": "normal",
"size": 1,
"visible": true,
"x": 100,
"y": 50
}

0 comments on commit 9b42917

Please sign in to comment.