Example #1
0
func (l *Level) HandleEvent(ev tcell.Event) bool {
	switch ev := ev.(type) {
	case *tcell.EventKey:
		switch ev.Key() {
		case tcell.KeyCtrlR:
			// secret reset button
			l.Reset()
		}
	case *tcell.EventMouse:

		offx, offy, _, _ := l.view.GetVisible()
		px1, py1, px2, py2 := l.view.GetPhysical()
		mx, my := ev.Position()
		if mx < px1 || mx > px2 || my < py1 || my > py2 {
			// outside our view
			return false
		}
		if ev.Buttons()&tcell.Button1 != 0 {
			l.stopped = true
			l.view.Center(offx+mx-px1, offy+my-py1)
		} else if ev.Buttons()&tcell.Button2 != 0 {
			l.Reset()
		} else if ev.Buttons()&tcell.Button3 != 0 {
			l.Reset()
		}
		return true

	case *EventPlayerDeath:
		l.game.HandleEvent(ev)
		l.ShowPress()
		return true

	case *EventLevelComplete:
		l.win = true
		l.game.HandleEvent(ev)
		l.ShowComplete()
		l.ShowPress()
		return true

	case *EventGameOver:
		x1, y1, x2, y2 := l.view.GetVisible()
		sprite := GetSprite("GameOver")
		sprite.SetLayer(LayerDialog)
		sprite.SetFrame("F0")
		sprite.SetPosition(x1+(x2-x1)/2, y1+(y2-y1)/2)
		l.manager.AddSprite(sprite)

	case *EventTimesUp:
		bw := GetSprite("Blastwave")
		bw.Resize(l.width, l.height)
		bw.ScheduleFrame("0", time.Now().Add(2*time.Second))
		l.AddSprite(bw)

		dur := time.Duration(0)
		for i := 0; i < 100; i++ {
			x, y := l.randomViewXY()
			sprite := GetSprite("Explosion")
			sprite.ScheduleFrame("0", time.Now().Add(dur))
			sprite.SetPosition(x, y)
			l.AddSprite(sprite)
			dur += time.Millisecond * 5

			for j := 0; j < 4; j++ {
				x, y = l.randomViewXY()
				sprite = GetSprite("SmallExplosion")
				sprite.SetPosition(x, y)
				sprite.ScheduleFrame("0", time.Now().Add(dur))
				l.AddSprite(sprite)
				dur += time.Millisecond * 50
			}
		}
	}
	if l.stopped {
		return false
	}

	return l.manager.HandleEvent(ev)
}