95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
var (
|
|
playerX, playerY = 0, 0
|
|
)
|
|
|
|
type Tile struct {
|
|
Sprite string
|
|
Overlay string
|
|
}
|
|
|
|
func tileAt(x int, y int) *Tile {
|
|
if x == playerX && y == playerY {
|
|
return &Tile{
|
|
Sprite: "sand_196.jpg",
|
|
Overlay: "gnome_196.png",
|
|
}
|
|
} else if (x == 5 && y == 5) || (x == 0 && y == 2) {
|
|
return &Tile{
|
|
Sprite: "sawdust_fir_196.jpg",
|
|
}
|
|
}
|
|
return &Tile{
|
|
Sprite: "sand_196.jpg",
|
|
}
|
|
}
|
|
|
|
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|
moveX := r.URL.Query().Get("moveX")
|
|
moveY := r.URL.Query().Get("moveY")
|
|
if moveX != "" && moveY != "" {
|
|
mx, err := strconv.Atoi(moveX)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
my, err := strconv.Atoi(moveY)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if mx >= -1 && my >= -1 && mx <= 1 && my <= 1 {
|
|
playerX, playerY = playerX+mx, playerY+my
|
|
}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`<!DOCTYPE html><html><head><title>Sapphire Forests</title><style type="text/css">* { margin: 0; padding: 0; line-height: 0; }</style><script src="/static/js/game.js"></script></head><body>`))
|
|
|
|
for y := playerY - 3; y <= playerY+3; y++ {
|
|
for x := playerX - 3; x <= playerX+3; x++ {
|
|
t := tileAt(x, y)
|
|
if t.Overlay != "" {
|
|
w.Write([]byte(`<div style="position: relative;display: inline-block;width: 108px;height: 108px;"><img src="/static/image/` + t.Sprite + `" width="108" height="108" style="position: absolute;left: 0; top: 0;width: 108px;height: 108px;"><img src="/static/image/` + t.Overlay + `" width="55" height="108" style="position: absolute;left: 0; top: 0;width: 55px;height: 108px;margin-left: 26px;"></div>`))
|
|
continue
|
|
}
|
|
|
|
var link string
|
|
if ((y == playerY-1) || (y == playerY) || (y == playerY+1)) && ((x == playerX-1) || (x == playerX) || (x == playerX+1)) {
|
|
link = `<a href="#" onclick="moveTo(` + strconv.Itoa(x-playerX) + `,` + strconv.Itoa(y-playerY) + `);return false">`
|
|
w.Write([]byte(link))
|
|
}
|
|
|
|
w.Write([]byte(`<img src="/static/image/` + t.Sprite + `" width="108" height="108">`))
|
|
|
|
if link != "" {
|
|
w.Write([]byte("</a>"))
|
|
}
|
|
}
|
|
w.Write([]byte("<br>\n"))
|
|
}
|
|
w.Write([]byte("</body></html>"))
|
|
}
|
|
|
|
func main() {
|
|
staticDir, err := fs.Sub(assetFS, "asset")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticDir))))
|
|
|
|
http.HandleFunc("/", handleIndex)
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|