本文整理汇总了Golang中github.com/gdamore/tcell.Event.When方法的典型用法代码示例。如果您正苦于以下问题:Golang Event.When方法的具体用法?Golang Event.When怎么用?Golang Event.When使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gdamore/tcell.Event
的用法示例。
在下文中一共展示了Event.When方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandleEvent
func (o *ship) HandleEvent(ev tcell.Event) bool {
if o.dead {
return false
}
switch ev := ev.(type) {
case *EventSpriteAccelerate:
if ev.s != o.ship {
return false
}
vx, _ := o.ship.Velocity()
if vx >= 1.0 {
o.ship.SetFrame("RIGHT")
} else if vx <= -1.0 {
o.ship.SetFrame("LEFT")
} else {
o.ship.SetFrame("FWD")
}
case *EventSpriteMove:
// We don't let ship leave the map
x, y := o.ship.Position()
ox, oy := x, y
vx, vy := o.ship.Velocity()
w, h := o.level.Size()
if x < 0 {
x = 0
if vx < 0 {
vx = 0
}
} else if x >= w {
x = w - 1
if vx > 0 {
vx = 0
}
}
if y < 0 {
y = 0
if vy < 0 {
vy = 0
}
} else if y >= h {
y = h - 1
if vy > 0 {
vy = 0
}
}
if ox != x || oy != y {
o.ship.SetPosition(x, y)
o.ship.SetVelocity(vx, vy)
}
if y == 0 {
o.dead = true
o.level.HandleEvent(&EventLevelComplete{})
}
o.adjustView()
case *EventGravity:
now := ev.When()
if !o.lastgrav.IsZero() {
vx, vy := o.ship.Velocity()
frac := float64(now.Sub(o.lastgrav))
frac /= float64(time.Second)
vy += ev.Accel() * frac
o.ship.SetVelocity(vx, vy)
}
o.lastgrav = now
case *EventCollision:
switch ev.Collider().Layer() {
case LayerTerrain, LayerHazard, LayerShot:
o.destroy()
case LayerPad:
// if we're on the pad, and not too
// fast, then stay on the pad.
// TODO: probably the max velocity (4.0)
// should be tunable.
vx, vy := o.ship.Velocity()
x, y := o.ship.Position()
if vx == 0 && vy > 0 && vy < 4.0 {
y--
vy = 0
o.ship.SetPosition(x, y)
o.ship.SetVelocity(vx, vy)
o.launched = false
} else {
o.destroy()
}
}
case *EventTimesUp:
o.destroy()
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyLeft:
o.thrustLeft()
return true
case tcell.KeyRight:
o.thrustRight()
//.........这里部分代码省略.........