Exemple #1
0
func (l *Level) LocalThink(sys system.System, engine *cgf.Engine) {
	// Convert the mouse screen coordinates to paddleWidth, paddleHeight range.
	pixelX, pixelY := sys.GetCursorPos()
	pb := &l.LevelBlueprint.Players[0]
	transformedX := float64(pixelX-Config.WindowWidth/2) *
		(pb.PaddleAreaWidth / float64(Config.WindowWidth))
	transformedY := -float64(pixelY-Config.WindowHeight/2) *
		(pb.PaddleAreaHeight / float64(Config.WindowHeight))

	// Clip this to a valid range.
	p := &l.Paddle
	x := math.Max(-pb.PaddleAreaWidth/2+p.Width/2,
		math.Min(transformedX, pb.PaddleAreaWidth/2-p.Width/2))
	y := math.Max(-pb.PaddleAreaHeight/2+p.Height/2,
		math.Min(transformedY, pb.PaddleAreaHeight/2-p.Height/2))

	// Create the event.
	engine.ApplyEvent(&MovePlayerEvent{x, y})
}