ichigo/main.go

160 lines
6.3 KiB
Go
Raw Normal View History

2021-07-23 13:12:54 +10:00
package main
import (
2021-08-27 12:04:01 +10:00
"flag"
2021-08-26 11:31:39 +10:00
"image"
"image/color"
2021-07-25 14:15:39 +10:00
_ "image/png"
2021-07-23 13:12:54 +10:00
"log"
2021-08-25 16:46:30 +10:00
"os"
2021-08-25 16:54:22 +10:00
"runtime"
2021-07-23 13:12:54 +10:00
"drjosh.dev/gurgle/engine"
2021-08-05 12:26:41 +10:00
"drjosh.dev/gurgle/game"
2021-07-23 13:12:54 +10:00
"github.com/hajimehoshi/ebiten/v2"
)
2021-08-27 12:04:01 +10:00
var rewriteLevel1 = flag.Bool("rewrite_level_1", false, "Rewrites level1.gobz from an inbuilt struct literal")
2021-07-23 13:12:54 +10:00
func main() {
2021-08-27 12:04:01 +10:00
flag.Parse()
2021-08-05 14:36:07 +10:00
ebiten.SetWindowResizable(true)
ebiten.SetWindowSize(640, 480)
2021-07-25 14:15:39 +10:00
ebiten.SetWindowTitle("TODO")
2021-08-27 12:04:01 +10:00
if *rewriteLevel1 {
2021-08-26 11:31:39 +10:00
writeLevel1()
}
g := &engine.Game{
2021-08-05 14:36:07 +10:00
ScreenHeight: 240,
ScreenWidth: 320,
2021-08-20 16:46:26 +10:00
Root: &engine.Scene{
2021-08-02 12:16:10 +10:00
ID: "root",
2021-07-30 16:11:01 +10:00
Components: []interface{}{
2021-08-08 22:07:55 +10:00
&engine.Camera{
ID: "game_camera",
2021-08-25 15:04:38 +10:00
Scene: &engine.SceneRef{Path: "assets/level1.gobz"},
2021-08-08 22:07:55 +10:00
},
2021-08-27 14:16:12 +10:00
&engine.DebugToast{ID: "toast", Pos: image.Pt(0, 15)},
2021-07-30 16:11:01 +10:00
engine.PerfDisplay{},
},
2021-07-23 13:12:54 +10:00
},
2021-07-25 15:03:10 +10:00
}
2021-08-27 13:56:50 +10:00
if err := g.LoadAndPrepare(game.Assets); err != nil {
log.Fatalf("Loading/preparing error: %v", err)
2021-08-20 16:23:10 +10:00
}
2021-07-25 15:03:10 +10:00
2021-08-26 10:03:11 +10:00
if runtime.GOOS != "js" {
// Run a repl on the console.
go g.REPL(os.Stdin, os.Stdout, game.Assets)
}
2021-08-25 16:46:30 +10:00
// ... while the game also runs
if err := ebiten.RunGame(g); err != nil {
2021-07-23 14:59:49 +10:00
log.Fatalf("Game error: %v", err)
2021-07-23 13:12:54 +10:00
}
}
2021-08-26 11:31:39 +10:00
// writeLevel1 dumps a test level into level1.gobz
func writeLevel1() {
redTileAnim := &engine.Anim{Frames: []engine.AnimFrame{
{Frame: 3, Duration: 12},
{Frame: 4, Duration: 12},
{Frame: 5, Duration: 12},
{Frame: 6, Duration: 12},
}}
greenTileAnim := &engine.Anim{Frames: []engine.AnimFrame{
{Frame: 0, Duration: 16},
{Frame: 1, Duration: 16},
{Frame: 2, Duration: 16},
}}
denseTiles := [][]engine.Tile{
{engine.StaticTile(9), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.StaticTile(9)},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.AnimatedTile{Animer: redTileAnim.Copy()}, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.AnimatedTile{Animer: redTileAnim.Copy()}, nil, nil, nil},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, engine.AnimatedTile{Animer: greenTileAnim.Copy()}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, nil, engine.AnimatedTile{Animer: redTileAnim.Copy()}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.AnimatedTile{Animer: greenTileAnim.Copy()}, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, engine.AnimatedTile{Animer: greenTileAnim.Copy()}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.AnimatedTile{Animer: greenTileAnim.Copy()}, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, engine.AnimatedTile{Animer: redTileAnim.Copy()}, nil, nil, nil, nil, nil, nil, engine.StaticTile(9), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, nil, engine.StaticTile(9), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.StaticTile(9), nil, nil, nil},
{nil, nil, nil, nil, engine.StaticTile(9), engine.StaticTile(9), engine.StaticTile(9), nil, nil, nil, engine.StaticTile(9), nil, nil, nil, nil, nil, nil, nil, nil, nil},
{engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.AnimatedTile{Animer: redTileAnim.Copy()}, engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8), engine.StaticTile(8)},
{engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.AnimatedTile{Animer: redTileAnim.Copy()}, engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.AnimatedTile{Animer: greenTileAnim.Copy()}, engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7)},
{engine.StaticTile(9), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(9)},
}
tiles := make(map[image.Point]engine.Tile)
for j, row := range denseTiles {
for i, tile := range row {
if tile == nil {
continue
}
tiles[image.Pt(i, j)] = tile
}
}
level1 := &engine.Scene{
ID: "level_1",
Bounds: engine.Bounds(image.Rect(-32, -32, 320+32, 240+32)),
Components: []interface{}{
&engine.Fill{
Color: color.Gray{100},
ZOrder: 0,
},
&engine.Billboard{
ID: "bg_image",
Parallax: 0.5,
ZOrder: 1,
Pos: image.Pt(-160, -120),
Src: engine.ImageRef{Path: "assets/space.png"},
},
&engine.Tilemap{
2021-08-26 13:54:22 +10:00
ID: "terrain",
ZOrder: 2,
Map: tiles,
Sheet: engine.Sheet{
CellSize: image.Pt(16, 16),
Src: engine.ImageRef{Path: "assets/boxes.png"},
},
2021-08-26 11:31:39 +10:00
},
&engine.SolidRect{
ID: "ceiling",
Bounds: engine.Bounds(image.Rect(0, -1, 320, 0)),
},
&engine.SolidRect{
ID: "left_wall",
Bounds: engine.Bounds(image.Rect(-1, 0, 0, 240)),
},
&engine.SolidRect{
ID: "right_wall",
Bounds: engine.Bounds(image.Rect(320, 0, 321, 240)),
},
&game.Awakeman{
CameraID: "game_camera",
ToastID: "toast",
Sprite: engine.Sprite{
Actor: engine.Actor{
CollisionDomain: "level_1",
Pos: image.Pt(100, 100),
Size: image.Pt(8, 16),
},
ZOrder: 3,
FrameOffset: image.Pt(-1, 0),
2021-08-26 13:57:19 +10:00
Sheet: engine.Sheet{
CellSize: image.Pt(10, 16),
Src: engine.ImageRef{Path: "assets/aw.png"},
},
2021-08-26 11:31:39 +10:00
},
},
},
}
2021-08-26 11:38:55 +10:00
if err := engine.SaveGobz(level1, "game/assets/level1.gobz"); err != nil {
2021-08-26 11:31:39 +10:00
log.Fatalf("Couldn't save level1.gobz: %v", err)
}
}