Beispiel #1
0
func (v *Viewport) ScreenCoordinatesToTile(sx, sy int, config DisplayConfig) (float64, float64) {
	// I have never been more ashamed of code I have written
	w, h := float64(config.TileW), float64(config.TileH)
	fx, fy := float32(sx), float32(sy)
	var trans allegro.Transform
	trans.Identity()
	// Builds the viewport alignment matrix
	trans.Build(float32(-v.x), float32(-v.y), float32(v.xZoom), float32(v.yZoom),
		0)
	// Invert it to get back to pixel coordinates
	trans.Invert()
	// We need to translate back half a width to get to the pivot of the tiles
	trans.Translate(-float32(w/2), 0)

	x, y := trans.Apply(fx, fy)
	// Then we manually rotate it (because I'm bad at maths I guess)
	tx := float64(float64(y)*w-float64(x)*h) / (w * h)
	ty := float64(float64(y)*w+float64(x)*h) / (w * h)
	return tx, ty
}