Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event onTouched #68

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type eventSinkMgr struct {
allWhenIReceive *eventSink
allWhenSceneStart *eventSink
allWhenCloned *eventSink
allWhenTouched *eventSink
allWhenClick *eventSink
allWhenMoving *eventSink
allWhenTurning *eventSink
Expand All @@ -90,6 +91,7 @@ func (p *eventSinkMgr) reset() {
p.allWhenIReceive = nil
p.allWhenSceneStart = nil
p.allWhenCloned = nil
p.allWhenTouched = nil
p.allWhenClick = nil
p.allWhenMoving = nil
p.allWhenTurning = nil
Expand All @@ -102,6 +104,7 @@ func (p *eventSinkMgr) doDeleteClone(this interface{}) {
p.allWhenIReceive = p.allWhenIReceive.doDeleteClone(this)
p.allWhenSceneStart = p.allWhenSceneStart.doDeleteClone(this)
p.allWhenCloned = p.allWhenCloned.doDeleteClone(this)
p.allWhenTouched = p.allWhenTouched.doDeleteClone(this)
p.allWhenClick = p.allWhenClick.doDeleteClone(this)
p.allWhenMoving = p.allWhenMoving.doDeleteClone(this)
p.allWhenTurning = p.allWhenTurning.doDeleteClone(this)
Expand Down Expand Up @@ -134,6 +137,15 @@ func (p *eventSinkMgr) doWhenClick(this threadObj) {
})
}

func (p *eventSinkMgr) doWhenTouched(this threadObj, obj *Sprite) {
p.allWhenTouched.asyncCall(false, this, func(ev *eventSink) {
if debugEvent {
log.Println("==> onTouched", nameOf(this), obj.name)
}
ev.sink.(func(*Sprite))(obj)
})
}

func (p *eventSinkMgr) doWhenCloned(this threadObj, data interface{}) {
p.allWhenCloned.asyncCall(true, this, func(ev *eventSink) {
if debugEvent {
Expand Down Expand Up @@ -296,15 +308,15 @@ func (p *eventSinks) OnMsg__1(msg string, onMsg func()) {
}
}

func (p *eventSinks) OnScene__0(onScene func(name string)) {
func (p *eventSinks) OnAnyScene(onScene func(name string)) {
p.allWhenSceneStart = &eventSink{
prev: p.allWhenSceneStart,
pthis: p.pthis,
sink: onScene,
}
}

func (p *eventSinks) OnScene__1(name string, onScene func()) {
func (p *eventSinks) OnScene(name string, onScene func()) {
p.allWhenSceneStart = &eventSink{
prev: p.allWhenSceneStart,
pthis: p.pthis,
Expand Down
2 changes: 1 addition & 1 deletion game.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ func (p *Game) ChangeEffect(kind EffectKind, delta float64) {
panic("todo")
}

func (p *Game) ClearEffects() {
func (p *Game) ClearSoundEffects() {
panic("todo")
}

Expand Down
98 changes: 82 additions & 16 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ type Sprite struct {
isVisible bool
isCloned bool
isPenDown bool
//isDraggable bool
hasOnCloned bool
reserved1 bool

isStopped bool
isDying bool
reserved2 bool
reserved3 bool

hasOnTurning bool
hasOnMoving bool
hasOnCloned bool
hasOnTouched bool

gamer reflect.Value
}
Expand Down Expand Up @@ -155,14 +158,17 @@ func (p *Sprite) InitFrom(src *Sprite) {
p.isVisible = src.isVisible
p.isCloned = true
p.isPenDown = src.isPenDown
//p.isDraggable = src.isDraggable
p.reserved1 = false

p.isStopped = false
p.isDying = false
p.reserved2 = false
p.reserved3 = false

p.hasOnTurning = false
p.hasOnMoving = false
p.hasOnCloned = false
p.hasOnTouched = false
}

func applyFloat64(out *float64, in interface{}) {
Expand Down Expand Up @@ -252,6 +258,62 @@ func (p *Sprite) OnCloned__1(onCloned func()) {
})
}

func (p *Sprite) fireTouched(obj *Sprite) {
if p.hasOnTouched {
p.doWhenTouched(p, obj)
}
}

func (p *Sprite) OnTouched__0(onTouched func(obj *Sprite)) {
p.hasOnTouched = true
p.allWhenTouched = &eventSink{
prev: p.allWhenTouched,
pthis: p,
sink: onTouched,
cond: func(data interface{}) bool {
return data == p
},
}
}

func (p *Sprite) OnTouched__1(onTouched func()) {
p.OnTouched__0(func(*Sprite) {
onTouched()
})
}

func (p *Sprite) OnTouched__2(name string, onTouched func(obj *Sprite)) {
p.OnTouched__0(func(obj *Sprite) {
if obj.name == name {
onTouched(obj)
}
})
}

func (p *Sprite) OnTouched__3(name string, onTouched func()) {
p.OnTouched__2(name, func(*Sprite) {
onTouched()
})
}

func (p *Sprite) OnTouched__4(names []string, onTouched func(obj *Sprite)) {
p.OnTouched__0(func(obj *Sprite) {
name := obj.name
for _, v := range names {
if v == name {
onTouched(obj)
return
}
}
})
}

func (p *Sprite) OnTouched__5(names []string, onTouched func()) {
p.OnTouched__4(names, func(*Sprite) {
onTouched()
})
}

type MovingInfo struct {
OldX, OldY float64
NewX, NewY float64
Expand Down Expand Up @@ -907,31 +969,43 @@ func (p *Sprite) ChangeSize(delta float64) {

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

func (p *Sprite) SetEffect(kind EffectKind, val float64) {
panic("todo")
}

func (p *Sprite) ChangeEffect(kind EffectKind, delta float64) {
panic("todo")
}

func (p *Sprite) ClearGraphEffects() {
panic("todo")
}

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

type Color = color.RGBA

func (p *Sprite) TouchingColor(color Color) bool {
panic("todo")
}

// Touching func:
// Touching(spriteName[, animation])
// Touching(spriteName)
// Touching(sprite)
// Touching(spx.Mouse)
// Touching(spx.Edge)
// Touching(spx.EdgeLeft)
// Touching(spx.EdgeTop)
// Touching(spx.EdgeRight)
// Touching(spx.EdgeBottom)
func (p *Sprite) Touching(obj interface{}, ani ...string) bool {
func (p *Sprite) Touching(obj interface{}) bool {
if !p.isVisible || p.isDying {
return false
}
switch v := obj.(type) {
case string:
if o := p.g.touchingSpriteBy(p, v); o != nil {
if ani != nil {
o.execTouchingAni(ani[0])
}
o.fireTouched(p)
return true
}
return false
Expand All @@ -948,14 +1022,6 @@ func (p *Sprite) Touching(obj interface{}, ani ...string) bool {
panic("Touching: unexpected input")
}

func (p *Sprite) execTouchingAni(ani string) {
if ani == "die" {
p.Die()
} else {
p.Animate(ani)
}
}

func touchingSprite(dst, src *Sprite) bool {
if !src.isVisible || src.isDying {
return false
Expand Down
4 changes: 0 additions & 4 deletions test/Bananas/Monkey.spx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ onClick => {
onMoving mi => {
Bananas[1].changeXYpos mi.dx, mi.dy
}

onStart => {
testStartTick
}
4 changes: 4 additions & 0 deletions tutorial/05-Animation/Bullet.spx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
onTouched => {
destroy
}

onStart => {
for {
wait 0.3
Expand Down
2 changes: 1 addition & 1 deletion tutorial/05-Animation/SmallEnemy.spx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ onCloned => {
life := 5
for {
wait 0.05
if touching("Bullet", "die") {
if touching("Bullet") {
life--
if life == 0 {
die
Expand Down
4 changes: 4 additions & 0 deletions tutorial/09-AircraftWar/Bomb.spx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
onTouched => {
destroy
}

onCloned => {
setXYpos rand(-90, 90), 237
show
Expand Down
4 changes: 4 additions & 0 deletions tutorial/09-AircraftWar/Bullet.spx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
onTouched => {
destroy
}

onCloned => {
setXYpos MyAircraft.xpos, MyAircraft.ypos+5
show
Expand Down
2 changes: 1 addition & 1 deletion tutorial/09-AircraftWar/HugeEnemy.spx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ onCloned => {
if ypos < -200 {
destroy
}
if touching("Bullet", "die") {
if touching("Bullet") {
life--
if life == 0 {
addScore 50
Expand Down
2 changes: 1 addition & 1 deletion tutorial/09-AircraftWar/MiddleEnemy.spx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ onCloned => {
if ypos < -170 {
destroy
}
if touching("Bullet", "die") {
if touching("Bullet") {
life--
if life == 0 {
addScore 10
Expand Down
2 changes: 1 addition & 1 deletion tutorial/09-AircraftWar/MyAircraft.spx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ onMsg "battle", => {
broadcast "game over"
die
}
if touching("Bomb", "die") {
if touching("Bomb") {
bombs++
}
}
Expand Down
2 changes: 1 addition & 1 deletion tutorial/09-AircraftWar/SmallEnemy.spx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ onCloned => {
if ypos < -170 {
destroy
}
if touching("Bullet", "die") {
if touching("Bullet") {
life--
if life == 0 {
addScore 5
Expand Down