Remove a dodgy optimization and allow mouse clicks to output cell position

This commit is contained in:
2018-03-25 12:49:36 +01:00
parent 50d316b5eb
commit b6dcfafb6d

View File

@@ -134,11 +134,6 @@ func (e *env) getSprite(palette []string, ref maps.ObjRef) *conv.Sprite {
return &obj.Sprites[ref.Frame()]
}
var (
cellWidth = 64.0 // I think, anyway
cellLength = 64.0
)
// TODO: build all the sprites in the set into a single spritesheet so we can
// use pixel.Batch
func (s *state) present(pWin *pixelgl.Window) {
@@ -176,10 +171,7 @@ func (s *state) renderCell(x, y, z int, pWin *pixelgl.Window) {
cell := s.env.gameMap.Cells.At(x, y, z)
// Try optimizing zero surfaces out, they tend to be? all-transparent
if cell.Surface.Index() != 0 {
sprites = append(sprites, s.env.getSprite(s.env.set.Palette, cell.Surface))
}
sprites = append(sprites, s.env.getSprite(s.env.set.Palette, cell.Surface))
sprites = append(
sprites,
@@ -193,13 +185,7 @@ func (s *state) renderCell(x, y, z int, pWin *pixelgl.Window) {
fX := float64(x - z)
fY := float64(y - z)
xPos := fX * cellWidth
yPos := fY * cellLength
// The rotation translates the rectangular coordinates to diamond
// ones \o/
orig := pixel.V(xPos, yPos)
iso := pixel.V(orig.X-orig.Y, (orig.X+orig.Y)/2.0)
iso := s.cellToPix(pixel.V(fX, fY))
for _, sprite := range sprites {
if sprite != nil {
@@ -208,6 +194,27 @@ func (s *state) renderCell(x, y, z int, pWin *pixelgl.Window) {
}
}
var (
cellWidth = 64.0
cellHeight = 64.0
)
// Doesn't take the camera or Z level into account
func (s *state) cellToPix(cell pixel.Vec) pixel.Vec {
return pixel.V(
(cell.X-cell.Y)*cellWidth,
(cell.X+cell.Y)*cellHeight/2.0,
)
}
// Doesn't take the camera or Z level into account
func (s *state) pixToCell(pix pixel.Vec) pixel.Vec {
return pixel.V(
pix.Y/cellHeight+pix.X/(cellWidth*2.0),
pix.Y/cellHeight-pix.X/(cellWidth*2.0),
)
}
func (s *state) runStep(pWin *pixelgl.Window) *state {
newState := *s
newState.handleKeys(pWin)
@@ -216,6 +223,17 @@ func (s *state) runStep(pWin *pixelgl.Window) *state {
}
func (s *state) handleKeys(pWin *pixelgl.Window) {
// Do this first to avoid taking the below mutations into account
// FIXME: this suggests we should pass the next state into here and
// modify it instead
if pWin.JustPressed(pixelgl.MouseButton1) {
cell := s.pixToCell(s.cam.Unproject(pWin.MousePosition()))
if s.zIdx != 0 {
log.Printf("WARNING: z-index not yet taken into account")
}
log.Printf("X=%v Y=%v, zIdx=%v", cell.X, cell.Y, s.zIdx)
}
if pWin.Pressed(pixelgl.KeyLeft) {
s.camPos.X -= 64
}