275 lines
6.6 KiB
Go
275 lines
6.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"image"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/conv"
|
|
"code.ur.gs/lupine/ordoor/internal/data"
|
|
"code.ur.gs/lupine/ordoor/internal/maps"
|
|
"code.ur.gs/lupine/ordoor/internal/sets"
|
|
"code.ur.gs/lupine/ordoor/internal/ui"
|
|
)
|
|
|
|
var (
|
|
gamePath = flag.String("game-path", "./orig", "Path to a WH40K: Chaos Gate installation")
|
|
mapFile = flag.String("map", "", "Prefix path to a .map file, e.g. ./orig/Maps/Chapter01.MAP")
|
|
txtFile = flag.String("txt", "", "Prefix path to a .txt file, e.g. ./orig/Maps/Chapter01.txt")
|
|
)
|
|
|
|
type env struct {
|
|
gameMap *maps.GameMap
|
|
set *sets.MapSet
|
|
objects map[string]*conv.Object
|
|
|
|
step int
|
|
state state
|
|
lastState state
|
|
}
|
|
|
|
type state struct {
|
|
zoom float64
|
|
origin image.Point
|
|
zIdx int
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
if *gamePath == "" || *mapFile == "" || *txtFile == "" {
|
|
flag.Usage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
gameMap, err := maps.LoadGameMapByFiles(*mapFile, *txtFile)
|
|
if err != nil {
|
|
log.Fatalf("Couldn't load map file: %v", err)
|
|
}
|
|
|
|
setFile := filepath.Join(*gamePath, "Sets", gameMap.MapSetFilename())
|
|
log.Println(setFile)
|
|
mapSet, err := sets.LoadSet(setFile)
|
|
if err != nil {
|
|
log.Fatalf("Couldn't load set file %s: %v", setFile, err)
|
|
}
|
|
|
|
objects := make([]*conv.Object, 0, len(mapSet.Palette))
|
|
for _, name := range mapSet.Palette {
|
|
objFile := filepath.Join(*gamePath, "Obj", name+".obj")
|
|
rawObj, err := data.LoadObject(objFile)
|
|
if err != nil {
|
|
log.Fatalf("Failed to load %s: %v", name, err)
|
|
}
|
|
|
|
rawObj.Name = name
|
|
|
|
obj, err := conv.ConvertObject(rawObj, name)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
objects = append(objects, obj)
|
|
}
|
|
|
|
state := state{
|
|
zoom: 1.0,
|
|
origin: image.Point{0, 3000}, // FIXME: haxxx
|
|
}
|
|
env := &env{
|
|
gameMap: gameMap,
|
|
set: mapSet,
|
|
objects: conv.MapByName(objects),
|
|
state: state,
|
|
lastState: state,
|
|
}
|
|
|
|
win, err := ui.NewWindow("View Map " + *mapFile)
|
|
if err != nil {
|
|
log.Fatal("Couldn't create window: %v", err)
|
|
}
|
|
|
|
// TODO: click to view cell data
|
|
|
|
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))
|
|
win.OnMouseWheel(env.changeZoom)
|
|
|
|
for i := 0; i <= 6; i++ {
|
|
win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i))
|
|
}
|
|
|
|
if err := win.Run(env.Update, env.Draw); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
e.lastState = e.state
|
|
e.step += 1
|
|
|
|
return nil
|
|
}
|
|
|
|
func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, error) {
|
|
// There seems to be an active bit that hides many sins
|
|
if !ref.IsActive() {
|
|
return nil, nil
|
|
}
|
|
|
|
if ref.Index() >= len(palette) {
|
|
return nil, fmt.Errorf("Palette too small: %v requested", ref.Index())
|
|
}
|
|
|
|
name := palette[ref.Index()]
|
|
|
|
obj := e.objects[name]
|
|
if obj == nil {
|
|
return nil, fmt.Errorf("Failed to find surface sprite %#v -> %q", ref, name)
|
|
}
|
|
|
|
if ref.Sprite() >= len(obj.Sprites) {
|
|
return nil, fmt.Errorf("Out-of-index sprite %v requested for %v", ref.Sprite(), name)
|
|
}
|
|
|
|
return obj.Sprites[ref.Sprite()], nil
|
|
}
|
|
|
|
func (e *env) Draw(screen *ebiten.Image) error {
|
|
// 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()
|
|
|
|
topLeftX, topLeftY := pixToCell(
|
|
float64(e.state.origin.X),
|
|
float64(e.state.origin.Y),
|
|
)
|
|
topLeftX -= 1 // Otherwise we miss half a cell on alternate rows on the left
|
|
|
|
bottomRightX, bottomRightY := pixToCell(
|
|
float64(e.state.origin.X+sw),
|
|
float64(e.state.origin.Y+sh),
|
|
)
|
|
|
|
// X+Y is constant for all tiles in a column
|
|
// X-Y is constant for all tiles in a row
|
|
for a := int(topLeftX + topLeftY); a <= int(bottomRightX+bottomRightY); a++ {
|
|
for b := int(topLeftX - topLeftY); b <= int(bottomRightX-bottomRightY); b++ {
|
|
if b&1 != a&1 {
|
|
continue
|
|
}
|
|
|
|
x := (a + b) / 2
|
|
y := (a - b) / 2
|
|
|
|
if x < int(e.gameMap.MinWidth) || x >= int(e.gameMap.MaxWidth) ||
|
|
y < int(e.gameMap.MinLength) || y >= int(e.gameMap.MaxLength) {
|
|
continue
|
|
}
|
|
|
|
for z := 0; z <= e.state.zIdx; z++ {
|
|
e.renderCell(x, y, z, screen)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
|
|
var sprites []*conv.Sprite
|
|
cell := e.gameMap.Cells.At(x, y, z)
|
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Surface); err != nil {
|
|
log.Printf("%v %v %v surface: %v", x, y, z, err)
|
|
} else if spr != nil {
|
|
sprites = append(sprites, spr)
|
|
}
|
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Center); err != nil {
|
|
log.Printf("%v %v %v center: %v", x, y, z, err)
|
|
} else if spr != nil {
|
|
sprites = append(sprites, spr)
|
|
}
|
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Left); err != nil {
|
|
log.Printf("%v %v %v left: %v", x, y, z, err)
|
|
} else if spr != nil {
|
|
sprites = append(sprites, spr)
|
|
}
|
|
|
|
if spr, err := e.getSprite(e.set.Palette, cell.Right); err != nil {
|
|
log.Printf("%v %v %v right: %v", x, y, z, err)
|
|
} else if spr != nil {
|
|
sprites = append(sprites, spr)
|
|
}
|
|
|
|
iso := ebiten.GeoM{}
|
|
iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y))
|
|
|
|
fx, fy := cellToPix(float64(x), float64(y))
|
|
iso.Translate(fx, fy)
|
|
|
|
// Taking the Z index away *seems* to draw the object in the correct place.
|
|
// FIXME: There are some artifacts, investigate more
|
|
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
|
|
|
|
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
|
|
|
for _, sprite := range sprites {
|
|
if err := screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (e *env) changeOrigin(byX, byY int) func() {
|
|
return func() {
|
|
e.state.origin.X += byX
|
|
e.state.origin.Y += byY
|
|
}
|
|
}
|
|
|
|
func (e *env) changeZoom(_, y float64) {
|
|
// Zoom in and out with the mouse wheel
|
|
e.state.zoom *= math.Pow(1.2, y)
|
|
}
|
|
|
|
func (e *env) setZIdx(to int) func() {
|
|
return func() {
|
|
e.state.zIdx = to
|
|
}
|
|
}
|
|
|
|
const (
|
|
cellWidth = 64
|
|
cellHeight = 64
|
|
)
|
|
|
|
// Doesn't take the camera or Z level into account
|
|
func cellToPix(x, y float64) (float64, float64) {
|
|
return (x - y) * cellWidth, (x + y) * cellHeight / 2.0
|
|
}
|
|
|
|
// Doesn't take the camera or Z level into account
|
|
func pixToCell(x, y float64) (float64, float64) {
|
|
return y/cellHeight + x/(cellWidth*2.0), y/cellHeight - x/(cellWidth*2.0)
|
|
}
|