2018-03-18 03:35:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-12-30 00:51:20 +00:00
|
|
|
"image"
|
2018-03-18 03:35:03 +00:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
2020-03-20 00:03:03 +00:00
|
|
|
"sort"
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2020-03-19 22:24:21 +00:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/assetstore"
|
2019-12-31 01:55:58 +00:00
|
|
|
"code.ur.gs/lupine/ordoor/internal/ui"
|
2018-03-18 03:35:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gamePath = flag.String("game-path", "./orig", "Path to a WH40K: Chaos Gate installation")
|
2020-03-19 22:24:21 +00:00
|
|
|
gameMap = flag.String("map", "", "Name of a map, e.g., Chapter01")
|
2018-03-18 03:35:03 +00:00
|
|
|
)
|
|
|
|
|
2018-03-18 13:57:01 +00:00
|
|
|
type env struct {
|
2020-03-19 22:24:21 +00:00
|
|
|
assets *assetstore.AssetStore
|
|
|
|
area *assetstore.Map
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2018-03-28 01:15:35 +01:00
|
|
|
step int
|
2019-12-30 00:51:20 +00:00
|
|
|
state state
|
|
|
|
lastState state
|
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
type state struct {
|
|
|
|
zoom float64
|
|
|
|
origin image.Point
|
|
|
|
zIdx int
|
2018-03-18 13:57:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 03:35:03 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-03-19 22:24:21 +00:00
|
|
|
if *gamePath == "" || *gameMap == "" {
|
2018-03-18 03:35:03 +00:00
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-03-19 22:24:21 +00:00
|
|
|
assets, err := assetstore.New(*gamePath)
|
2018-03-18 03:35:03 +00:00
|
|
|
if err != nil {
|
2020-03-19 22:24:21 +00:00
|
|
|
log.Fatalf("Failed to scan root directory %v: %v", *gamePath, err)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:24:21 +00:00
|
|
|
area, err := assets.Map(*gameMap)
|
2018-03-18 13:57:01 +00:00
|
|
|
if err != nil {
|
2020-03-19 22:24:21 +00:00
|
|
|
log.Fatalf("Failed to load map %v: %v", *gameMap, err)
|
2018-03-28 01:15:35 +01:00
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
state := state{
|
|
|
|
zoom: 1.0,
|
2019-12-31 01:38:39 +00:00
|
|
|
origin: image.Point{0, 3000}, // FIXME: haxxx
|
2020-03-20 00:03:03 +00:00
|
|
|
zIdx: 1,
|
2019-12-30 00:51:20 +00:00
|
|
|
}
|
|
|
|
env := &env{
|
2020-03-19 22:24:21 +00:00
|
|
|
area: area,
|
|
|
|
assets: assets,
|
2019-12-30 00:51:20 +00:00
|
|
|
state: state,
|
|
|
|
lastState: state,
|
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2020-03-19 22:24:21 +00:00
|
|
|
win, err := ui.NewWindow("View Map " + *gameMap)
|
2018-03-18 03:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Couldn't create window: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
// TODO: click to view cell data
|
|
|
|
|
2019-12-31 01:38:39 +00:00
|
|
|
win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(-64, +0))
|
|
|
|
win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(+64, +0))
|
|
|
|
win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, -64))
|
|
|
|
win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, +64))
|
2019-12-30 00:51:20 +00:00
|
|
|
win.OnMouseWheel(env.changeZoom)
|
|
|
|
|
2020-03-20 00:04:47 +00:00
|
|
|
for i := 0; i < 6; i++ {
|
2020-03-20 00:03:03 +00:00
|
|
|
win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i+1))
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
if err := win.Run(env.Update, env.Draw); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) Update() error {
|
|
|
|
if e.step == 0 || e.lastState != e.state {
|
|
|
|
log.Printf("zoom=%.2f zIdx=%v camPos=%#v", e.state.zoom, e.state.zIdx, e.state.origin)
|
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
e.lastState = e.state
|
|
|
|
e.step += 1
|
2018-03-28 01:15:35 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
return nil
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) Draw(screen *ebiten.Image) error {
|
2019-12-31 01:38:39 +00:00
|
|
|
// Bounds clipping
|
|
|
|
// http://www.java-gaming.org/index.php?topic=24922.0
|
|
|
|
// https://stackoverflow.com/questions/892811/drawing-isometric-game-worlds
|
|
|
|
// https://gamedev.stackexchange.com/questions/25896/how-do-i-find-which-isometric-tiles-are-inside-the-cameras-current-view
|
|
|
|
|
|
|
|
sw, sh := screen.Size()
|
|
|
|
|
2020-03-20 00:03:03 +00:00
|
|
|
topLeft := pixToCell(e.state.origin)
|
|
|
|
topLeft.X -= 1 // Otherwise we miss half a cell on alternate rows on the left
|
2019-12-31 01:38:39 +00:00
|
|
|
|
2020-03-20 00:03:03 +00:00
|
|
|
bottomRight := pixToCell(image.Pt(e.state.origin.X+sw, e.state.origin.Y+sh))
|
2019-12-31 01:38:39 +00:00
|
|
|
|
|
|
|
// X+Y is constant for all tiles in a column
|
|
|
|
// X-Y is constant for all tiles in a row
|
2020-03-20 00:03:03 +00:00
|
|
|
// However, the drawing order is odd unless we reorder explicitly.
|
|
|
|
toDraw := []image.Point{}
|
|
|
|
for a := topLeft.X + topLeft.Y; a <= bottomRight.X+bottomRight.Y; a++ {
|
|
|
|
for b := topLeft.X - topLeft.Y; b <= bottomRight.X-bottomRight.Y; b++ {
|
2019-12-31 01:38:39 +00:00
|
|
|
if b&1 != a&1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:03:03 +00:00
|
|
|
pt := image.Pt((a+b)/2, (a-b)/2)
|
2019-12-31 01:38:39 +00:00
|
|
|
|
2020-03-20 00:03:03 +00:00
|
|
|
if !pt.In(e.area.Rect) {
|
2019-12-31 01:38:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-03-20 00:03:03 +00:00
|
|
|
toDraw = append(toDraw, pt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(toDraw, func(i, j int) bool {
|
|
|
|
iPix := cellToPix(toDraw[i])
|
|
|
|
jPix := cellToPix(toDraw[j])
|
|
|
|
|
|
|
|
if iPix.Y < jPix.Y {
|
|
|
|
return true
|
|
|
|
}
|
2019-12-31 01:38:39 +00:00
|
|
|
|
2020-03-20 00:03:03 +00:00
|
|
|
if iPix.Y == jPix.Y {
|
|
|
|
return iPix.X < jPix.X
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, pt := range toDraw {
|
|
|
|
for z := 0; z <= e.state.zIdx; z++ {
|
|
|
|
if err := e.renderCell(pt.X, pt.Y, z, screen); err != nil {
|
|
|
|
return err
|
2018-03-25 12:11:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 23:02:24 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
return nil
|
2018-03-25 12:11:48 +01:00
|
|
|
}
|
2018-03-22 04:15:31 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
|
2020-03-21 00:56:35 +00:00
|
|
|
sprites, err := e.area.SpritesForCell(x, y, z)
|
2020-03-19 22:24:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-28 01:00:55 +01:00
|
|
|
}
|
2018-03-23 23:54:20 +00:00
|
|
|
|
2019-12-31 01:38:39 +00:00
|
|
|
iso := ebiten.GeoM{}
|
|
|
|
iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y))
|
|
|
|
|
2020-03-20 00:03:03 +00:00
|
|
|
pix := cellToPix(image.Pt(x, y))
|
|
|
|
iso.Translate(float64(pix.X), float64(pix.Y))
|
|
|
|
|
|
|
|
if e.step%30 == 0 {
|
|
|
|
// log.Printf("x=%v y=%v z=%v", pix.X-e.state.origin.X, pix.Y-e.state.origin.Y, z)
|
|
|
|
}
|
2019-12-31 01:38:39 +00:00
|
|
|
|
2018-03-25 12:11:48 +01:00
|
|
|
// Taking the Z index away *seems* to draw the object in the correct place.
|
|
|
|
// FIXME: There are some artifacts, investigate more
|
2019-12-31 01:38:39 +00:00
|
|
|
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
|
2018-03-23 23:54:20 +00:00
|
|
|
|
2019-12-31 01:38:39 +00:00
|
|
|
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
2018-03-25 00:36:23 +00:00
|
|
|
|
2020-03-21 00:56:35 +00:00
|
|
|
for _, spr := range sprites {
|
|
|
|
iso.Translate(float64(spr.XOffset), float64(spr.YOffset))
|
|
|
|
if err := screen.DrawImage(spr.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil {
|
2019-12-30 00:51:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-21 00:56:35 +00:00
|
|
|
iso.Translate(float64(-spr.XOffset), float64(-spr.YOffset))
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2018-03-25 12:49:36 +01:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
return nil
|
2018-03-25 12:49:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) changeOrigin(byX, byY int) func() {
|
|
|
|
return func() {
|
|
|
|
e.state.origin.X += byX
|
|
|
|
e.state.origin.Y += byY
|
|
|
|
}
|
2018-03-25 12:49:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) changeZoom(_, y float64) {
|
|
|
|
// Zoom in and out with the mouse wheel
|
|
|
|
e.state.zoom *= math.Pow(1.2, y)
|
2018-03-22 04:15:31 +00:00
|
|
|
}
|
2018-03-18 13:57:01 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
func (e *env) setZIdx(to int) func() {
|
|
|
|
return func() {
|
|
|
|
e.state.zIdx = to
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|
2019-12-30 00:51:20 +00:00
|
|
|
}
|
2018-03-18 03:35:03 +00:00
|
|
|
|
2019-12-31 01:38:39 +00:00
|
|
|
const (
|
|
|
|
cellWidth = 64
|
|
|
|
cellHeight = 64
|
2019-12-30 00:51:20 +00:00
|
|
|
)
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
// Doesn't take the camera or Z level into account
|
2020-03-20 00:03:03 +00:00
|
|
|
func cellToPix(pt image.Point) image.Point {
|
|
|
|
return image.Pt(
|
|
|
|
(pt.X-pt.Y)*cellWidth,
|
|
|
|
(pt.X+pt.Y)*cellHeight/2,
|
|
|
|
)
|
2019-12-30 00:51:20 +00:00
|
|
|
}
|
2018-03-23 01:27:38 +00:00
|
|
|
|
2019-12-30 00:51:20 +00:00
|
|
|
// Doesn't take the camera or Z level into account
|
2020-03-20 00:03:03 +00:00
|
|
|
func pixToCell(pt image.Point) image.Point {
|
|
|
|
return image.Pt(
|
|
|
|
pt.Y/cellHeight+pt.X/(cellWidth*2),
|
|
|
|
pt.Y/cellHeight-pt.X/(cellWidth*2),
|
|
|
|
)
|
2018-03-18 03:35:03 +00:00
|
|
|
}
|