Пример #1
0
func (o *hfence) HandleEvent(ev tcell.Event) bool {
	if o.dead {
		return false
	}
	switch ev := ev.(type) {
	case *EventCollision:
		switch ev.Collider().Layer() {
		case LayerPlayer:
			switch ev.Target() {
			case o.lemit:
				o.destroy(o.lemit, o.remit)
			case o.remit:
				o.destroy(o.remit, o.lemit)
			}
		case LayerShot:
			switch ev.Target() {
			case o.lemit:
				o.destroy(o.lemit, o.remit)
			case o.remit:
				o.destroy(o.remit, o.lemit)
			case o.beam:
				x, y, _, _ := ev.Collider().Bounds()
				props := GameObjectProps{}
				props.PropSetInt("x", x)
				props.PropSetInt("y", y)
				props.PropSetInt("count", 1)
				props.PropSetString("sprite", "TinyExplosion")
				MakeGameObject(o.level, "explosion", props)
			}
		}
	}
	return false
}
Пример #2
0
func (o *gantry) HandleEvent(ev tcell.Event) bool {
	s := o.sprite
	switch ev := ev.(type) {
	case *EventSpriteMove:
		x, _ := o.sprite.Position()
		if o.startx != 0 && o.startx-5 >= x {
			o.sprite.SetVelocity(0, 0)
		}
	case *EventCollision:
		switch ev.Collider().Layer() {
		case LayerShot, LayerPlayer:
			o.sprite.Hide()
			x, y, _, _ := s.Bounds()
			props := GameObjectProps{}
			props.PropSetInt("x", x)
			props.PropSetInt("y", y)
			props.PropSetInt("count", 2)
			MakeGameObject(o.level, "explosion", props)
		}
	case *EventLevelStart:
		o.sprite.SetVelocity(-3.0, 0)
		o.sprite.SetFrame("RETRACT")
		o.startx, _ = o.sprite.Position()
	}
	return false
}
Пример #3
0
func (o *alien1) HandleEvent(ev tcell.Event) bool {
	s := o.sprite
	switch ev := ev.(type) {
	case *EventSpriteAccelerate:
		if ev.s != s {
			return false
		}
		vx, _ := s.Velocity()
		if vx > 0 {
			s.SetFrame("F1")
		} else {
			s.SetFrame("R1")
		}
	case *EventCollision:
		switch ev.Collider().Layer() {
		case LayerTerrain, LayerHazard:
			x, y, _, _ := s.Bounds()
			vx, vy := s.Velocity()
			vx = -vx
			vy = -vy
			s.SetVelocity(vx, vy)
			if vx < 0 {
				x--
			} else if vx > 0 {
				x++
			}
			if vy < 0 {
				y--
			} else if vy > 0 {
				y++
			}
			s.SetPosition(x, y)
		case LayerShot, LayerPlayer:
			s.Hide()
			x, y, _, _ := s.Bounds()
			props := GameObjectProps{}
			props.PropSetInt("x", x)
			props.PropSetInt("y", y)
			props.PropSetInt("count", 1)
			MakeGameObject(o.level, "smexplosion", props)
			o.level.RemoveSprite(o.sprite)
		}
	}
	return false
}
Пример #4
0
func (o *bullet) HandleEvent(ev tcell.Event) bool {
	switch ev := ev.(type) {
	case *EventSpriteAccelerate:
		if ev.s != o.sprite {
			return false
		}

		vx, _ := o.sprite.Velocity()
		if vx > 0 {
			o.sprite.SetFrame("H")
		} else {
			o.sprite.SetFrame("V")
		}
	case *EventSpriteMove:
		if ev.s != o.sprite {
			return false
		}

		x, y, _, _ := o.sprite.Bounds()
		w, h := o.level.Size()
		if x < 0 || y < 0 || x >= w || y >= h {
			o.destroy()
		}
	case *EventCollision:
		switch ev.Collider().Layer() {

		case LayerTerrain, LayerHazard, LayerPlayer, LayerExplosion:
			// Impact with most solid objects removes the shot.
			// The impacted object is responsible for painting
			// any explosive effect.
			o.destroy()
		}
	case *EventAlarm:
		o.destroy()
	}
	return false
}
Пример #5
0
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()
			return true

		case tcell.KeyUp:
			o.thrustUp()
			return true

		case tcell.KeyDown:
			o.thrustDown()
			return true

		case tcell.KeyRune:
			switch ev.Rune() {
			case ' ':
				o.shoot()
				return true
			case 'j', 'J':
				o.thrustLeft()
				return true
			case 'k', 'K':
				o.thrustRight()
				return true
			case 'i', 'I':
				o.thrustUp()
				return true
			case 'm', 'M':
				o.thrustDown()
				return true
			}
		}
	case *tcell.EventResize:
		x, y := o.ship.Position()
		o.level.Center(x, y)
		o.adjustView()
	}
	return false
}