Compare commits

..

2 Commits

Author SHA1 Message Date
903ddba2ac Selected cursor chrome 2020-04-18 12:23:03 +01:00
b191ba2a94 Simplify bounds clipping a tiny bit 2020-04-18 11:44:05 +01:00
3 changed files with 71 additions and 42 deletions

View File

@@ -22,6 +22,17 @@ type IsoPt struct {
func (s *Scenario) Update(screenX, screenY int) error { func (s *Scenario) Update(screenX, screenY int) error {
s.tick += 1 s.tick += 1
x, y := ebiten.CursorPosition()
screenPos := CartPt{
X: float64(s.Viewpoint.X + x),
Y: float64(s.Viewpoint.Y + y),
}
s.selectedCell = screenPos.ToISO()
// TODO: zoom support will need a camera
// FIXME: adjust for Z level
return nil return nil
} }
@@ -33,13 +44,15 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
sw, sh := screen.Size() sw, sh := screen.Size()
topLeft := CartPt{X: float64(s.Viewpoint.X), Y: float64(s.Viewpoint.Y)}.ToISO() topLeft := CartPt{
topLeft.X -= 5 // Ensure we paint to every visible section of the screeen. X: float64(s.Viewpoint.X - 2*cellWidth), // Ensure all visible cells are rendered
topLeft.X -= 5 // FIXME: haxxx Y: float64(s.Viewpoint.Y - 2*cellHeight),
}.ToISO()
bottomRight := CartPt{X: float64(s.Viewpoint.X + sw), Y: float64(s.Viewpoint.Y + sh)}.ToISO() bottomRight := CartPt{
bottomRight.X += 5 X: float64(s.Viewpoint.X + sw + 2*cellHeight),
bottomRight.Y += 5 Y: float64(s.Viewpoint.Y + sh + 5*cellHeight), // Z dimension requires it
}.ToISO()
// X+Y is constant for all tiles in a column // X+Y is constant for all tiles in a column
// X-Y is constant for all tiles in a row // X-Y is constant for all tiles in a row
@@ -79,7 +92,7 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
counter := map[string]int{} counter := map[string]int{}
for _, pt := range toDraw { for _, pt := range toDraw {
for z := 0; z <= s.ZIdx; z++ { for z := 0; z <= s.ZIdx; z++ {
if err := s.renderCell(pt, z, screen, counter); err != nil { if err := s.renderCell(int(pt.X), int(pt.Y), z, screen, counter); err != nil {
return err return err
} }
} }
@@ -87,26 +100,53 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
//log.Printf("%#+v", counter) //log.Printf("%#+v", counter)
return nil // Finally, draw cursor chrome
} spr, err := s.specials.Sprite(0)
func (s *Scenario) renderCell(pos IsoPt, z int, screen *ebiten.Image, counter map[string]int) error {
sprites, err := s.area.SpritesForCell(int(pos.X), int(pos.Y), z)
if err != nil { if err != nil {
return err return err
} }
iso := ebiten.GeoM{} op := ebiten.DrawImageOptions{}
iso.Translate(-float64(s.Viewpoint.X), -float64(s.Viewpoint.Y)) op.GeoM = s.geoForCoords(int(s.selectedCell.X), int(s.selectedCell.Y), 0)
op.GeoM.Translate(-cellWidthHalf, -cellHeightHalf)
pix := pos.ToCart() if err := screen.DrawImage(spr.Image, &op); err != nil {
iso.Translate(pix.X, pix.Y) return err
}
sx, sy := op.GeoM.Apply(0, 0)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("(%.0f,%.0f)", s.selectedCell.X, s.selectedCell.Y), int(sx), int(sy))
return nil
}
func (s *Scenario) geoForCoords(x, y, z int) ebiten.GeoM {
geo := ebiten.GeoM{}
geo.Translate(-float64(s.Viewpoint.X), -float64(s.Viewpoint.Y))
pix := IsoPt{X: float64(x), Y: float64(y)}.ToCart()
geo.Translate(pix.X, pix.Y)
// Taking the Z index away *seems* to draw the object in the correct place. // Taking the Z index away *seems* to draw the object in the correct place.
// FIXME: There are some artifacts, investigate more // FIXME: There are some artifacts, investigate more
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index geo.Translate(0.0, -float64(z*48.0)) // offset for Z index
return geo
}
func (s *Scenario) renderCell(x, y, z int, screen *ebiten.Image, counter map[string]int) error {
sprites, err := s.area.SpritesForCell(x, y, z)
if err != nil {
return err
}
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor // TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
iso := s.geoForCoords(x, y, z)
// FIXME: this fixed offset is found in jungtil.obj. Drawing with it
// means we put everywhere where the iso->pix conversion expects, but
// it's a bit nasty. Is there a better way?
iso.Translate(-209, -332)
for _, spr := range sprites { for _, spr := range sprites {
// if _, ok := counter[spr.ID]; !ok { // if _, ok := counter[spr.ID]; !ok {
@@ -115,20 +155,11 @@ func (s *Scenario) renderCell(pos IsoPt, z int, screen *ebiten.Image, counter ma
// counter[spr.ID] = counter[spr.ID] + 1 // counter[spr.ID] = counter[spr.ID] + 1
op := ebiten.DrawImageOptions{GeoM: iso} op := ebiten.DrawImageOptions{GeoM: iso}
// FIXME: this fixed offset is found in jungtil.obj. Drawing with it op.GeoM.Translate(float64(spr.Rect.Min.X), float64(spr.Rect.Min.Y))
// means we put everywhere where the iso->pix conversion expects, but
// it's a bit nasty. Is there a better way?
op.GeoM.Translate(float64(spr.Rect.Min.X-209), float64(spr.Rect.Min.Y-322))
if err := screen.DrawImage(spr.Image, &op); err != nil { if err := screen.DrawImage(spr.Image, &op); err != nil {
return err return err
} }
if z == 0 {
x, y := op.GeoM.Apply(0, 0)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("(%d,%d)", int(pos.X), int(pos.Y)), int(x), int(y))
}
} }
return nil return nil

View File

@@ -1,10 +1,6 @@
package scenario package scenario
import ( import (
// "image"
"github.com/hajimehoshi/ebiten"
"code.ur.gs/lupine/ordoor/internal/maps" "code.ur.gs/lupine/ordoor/internal/maps"
) )
@@ -14,12 +10,6 @@ type CellPoint struct {
} }
func (s *Scenario) CellAtCursor() (maps.Cell, CellPoint) { func (s *Scenario) CellAtCursor() (maps.Cell, CellPoint) {
x, y := ebiten.CursorPosition() cell := s.area.Cell(int(s.selectedCell.X), int(s.selectedCell.Y), 0)
screenPos := CartPt{X: float64(s.Viewpoint.X + x), Y: float64(s.Viewpoint.Y + y)} return cell, CellPoint{IsoPt: s.selectedCell, Z: 0}
isoPos := screenPos.ToISO()
// Convert to cell coordinates.
// TODO: zoom support will need a camera
// FIXME: adjust for Z level
return s.area.Cell(int(isoPos.X), int(isoPos.Y), 0), CellPoint{IsoPt: isoPos, Z: s.ZIdx}
} }

View File

@@ -9,10 +9,12 @@ import (
) )
type Scenario struct { type Scenario struct {
area *assetstore.Map area *assetstore.Map
specials *assetstore.Object
tick int tick int
turn int turn int
selectedCell IsoPt
// All these must be modified by user actions somehow. // All these must be modified by user actions somehow.
// TODO: extract into the idea of a viewport passed to Update / Draw somehow? // TODO: extract into the idea of a viewport passed to Update / Draw somehow?
@@ -27,6 +29,11 @@ func NewScenario(assets *assetstore.AssetStore, name string) (*Scenario, error)
return nil, err return nil, err
} }
specials, err := assets.Object("specials") // FIXME: should this be hardcoded?
if err != nil {
return nil, err
}
// Eager load sprites. TODO: do we really want to do this? // Eager load sprites. TODO: do we really want to do this?
if err := area.LoadSprites(); err != nil { if err := area.LoadSprites(); err != nil {
return nil, fmt.Errorf("Eager-loading sprites failed: %v", err) return nil, fmt.Errorf("Eager-loading sprites failed: %v", err)
@@ -34,6 +41,7 @@ func NewScenario(assets *assetstore.AssetStore, name string) (*Scenario, error)
out := &Scenario{ out := &Scenario{
area: area, area: area,
specials: specials,
Viewpoint: image.Pt(0, 3000), // FIXME: haxxx Viewpoint: image.Pt(0, 3000), // FIXME: haxxx
} }