zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

main.go (2619B)


      1 package main
      2 
      3 import (
      4 	"errors"
      5 	"fmt"
      6 	"github.com/hajimehoshi/ebiten/v2"
      7 	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
      8 	"github.com/hajimehoshi/ebiten/v2/inpututil"
      9 	_ "image/png"
     10 	"log"
     11 	"os"
     12 )
     13 
     14 const (
     15 	assetsDir  = "assets/"
     16 	screenW    = 1280
     17 	screenH    = 480
     18 	scale      = 2
     19 	cameraStep = 10
     20 )
     21 
     22 type Game struct {
     23 	Inited      bool
     24 	Map         *Gamemap
     25 	PressedKeys []ebiten.Key
     26 	Cam         Camera
     27 	Hero        Character
     28 }
     29 
     30 func (g *Game) Init() error {
     31 	if g.Inited {
     32 		return errors.New("Game is already initialized")
     33 	}
     34 
     35 	// init base map
     36 	g.Map = &Gamemap{}
     37 	var err error = g.Map.Init(assetsDir + "map0.json")
     38 	if err != nil {
     39 		return err
     40 	}
     41 
     42 	g.Cam.Init(screenW/scale, screenH/scale, g.Map.RealWidth, g.Map.RealHeight)
     43 
     44 	g.Hero.Init(assetsDir + "protag.png")
     45 	// @todo stub
     46 	g.Hero.Move(200, 200)
     47 
     48 	return nil
     49 }
     50 
     51 func (g *Game) Update() error {
     52 	g.PressedKeys = inpututil.AppendPressedKeys(g.PressedKeys[:0])
     53 	for _, key := range g.PressedKeys {
     54 		if key == ebiten.KeyQ {
     55 			os.Exit(0)
     56 		}
     57 
     58 		if key == ebiten.KeyArrowLeft {
     59 			g.Cam.ChangeX(-cameraStep)
     60 		}
     61 		if key == ebiten.KeyArrowRight {
     62 			g.Cam.ChangeX(cameraStep)
     63 		}
     64 		if key == ebiten.KeyArrowUp {
     65 			g.Cam.ChangeY(-cameraStep)
     66 		}
     67 		if key == ebiten.KeyArrowDown {
     68 			g.Cam.ChangeY(cameraStep)
     69 		}
     70 
     71 	}
     72 
     73 	return nil
     74 }
     75 
     76 func (g *Game) Draw(screen *ebiten.Image) {
     77 	// iterating through tiles
     78 	for _, tiles := range g.Map.Tiles {
     79 		// iterating through layers on the tile
     80 		for _, tile := range tiles {
     81 			if tile.IsOnScreen(&g.Cam, g.Map) {
     82 				// render options for that tile are being copied into opts so the cam
     83 				// movement transformations are not dragged back into the tile itself.
     84 				opts := *tile.Options
     85 				opts.GeoM.Translate(-g.Cam.x, -g.Cam.y)
     86 				screen.DrawImage(tile.Image, &opts)
     87 			}
     88 		}
     89 	}
     90 	// @todo character is being rendered on top of the map for the time being
     91 	opts := *g.Hero.Options                 // preserving original draw options like above
     92 	opts.GeoM.Translate(-g.Cam.x, -g.Cam.y) // camera compensation
     93 	screen.DrawImage(g.Hero.Image, &opts)
     94 
     95 	// debug info
     96 	ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f, screen: %v", ebiten.CurrentTPS(), g.Cam))
     97 }
     98 
     99 func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
    100 	return screenW / scale, screenH / scale
    101 }
    102 
    103 func main() {
    104 	var err error
    105 	ebiten.SetWindowSize(screenW, screenH)
    106 	ebiten.SetWindowTitle("Zorldo")
    107 	ebiten.SetMaxTPS(60)
    108 	ebiten.SetRunnableOnUnfocused(true)
    109 
    110 	var game *Game = &Game{}
    111 	if err = game.Init(); err != nil {
    112 		log.Fatal(err)
    113 	}
    114 
    115 	if err = ebiten.RunGame(game); err != nil {
    116 		log.Fatal(err)
    117 	}
    118 }