diff --git a/sprite.go b/sprite.go index b894715..4c709be 100644 --- a/sprite.go +++ b/sprite.go @@ -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) @@ -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") } @@ -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 { diff --git a/test/TurnTogether/Banana.spx b/test/TurnTogether/Banana.spx new file mode 100644 index 0000000..a3f46b5 --- /dev/null +++ b/test/TurnTogether/Banana.spx @@ -0,0 +1,3 @@ +Monkey.onTurning ti => { + turn ti +} diff --git a/test/TurnTogether/Banana2.spx b/test/TurnTogether/Banana2.spx new file mode 100644 index 0000000..c25b35b --- /dev/null +++ b/test/TurnTogether/Banana2.spx @@ -0,0 +1,3 @@ +Monkey.onTurning ti => { + turn ti.dir +} diff --git a/test/TurnTogether/Monkey.spx b/test/TurnTogether/Monkey.spx new file mode 100644 index 0000000..4ae3e1f --- /dev/null +++ b/test/TurnTogether/Monkey.spx @@ -0,0 +1,7 @@ +onKey KeyUp, => { + turn -30 +} + +onKey KeyDown, => { + turn 30 +} diff --git a/test/TurnTogether/index.gmx b/test/TurnTogether/index.gmx new file mode 100644 index 0000000..5416aab --- /dev/null +++ b/test/TurnTogether/index.gmx @@ -0,0 +1,7 @@ +var ( + Banana Banana + Banana2 Banana2 + Monkey Monkey +) + +run "res", {Title: "Turn Together (by Go+)"} diff --git a/test/TurnTogether/res/index.json b/test/TurnTogether/res/index.json new file mode 100644 index 0000000..4ae0208 --- /dev/null +++ b/test/TurnTogether/res/index.json @@ -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 + } + ] +} \ No newline at end of file diff --git a/test/TurnTogether/res/sprites/Banana/index.json b/test/TurnTogether/res/sprites/Banana/index.json new file mode 100644 index 0000000..112aaa3 --- /dev/null +++ b/test/TurnTogether/res/sprites/Banana/index.json @@ -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 +} \ No newline at end of file diff --git a/test/TurnTogether/res/sprites/Banana2/index.json b/test/TurnTogether/res/sprites/Banana2/index.json new file mode 100644 index 0000000..112aaa3 --- /dev/null +++ b/test/TurnTogether/res/sprites/Banana2/index.json @@ -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 +} \ No newline at end of file diff --git a/test/TurnTogether/res/sprites/Monkey/index.json b/test/TurnTogether/res/sprites/Monkey/index.json new file mode 100644 index 0000000..d5d886d --- /dev/null +++ b/test/TurnTogether/res/sprites/Monkey/index.json @@ -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 +} \ No newline at end of file