Skip to content

Commit

Permalink
Center view on starting tile, add README content (#6)
Browse files Browse the repository at this point in the history
* center view on starting tile
* add screenshot and some text to readme
  • Loading branch information
mlange-42 authored Feb 23, 2024
1 parent c835b53 commit bb9736b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# Tiny World

A tiny, slow-paced world and colony building game.

Made with [Arche](https://github.com/mlange-42/arche) and [Ebitengine](https://github.com/hajimehoshi/ebiten).
Very early work in progress!

![Tiny World screenshot](https://github.com/mlange-42/tiny-world/assets/44003176/9d7b0314-3c29-4773-8670-7e3a9b0df74b)

## Usage

Currently, you need to clone the repository and run the game with [Go](https://go.dev):

```shell
git clone https://github.com/mlange-42/tiny-world.git
cd tiny-world
go run .
```
42 changes: 42 additions & 0 deletions game/render/center_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package render

import (
"image"

"github.com/mlange-42/arche/ecs"
"github.com/mlange-42/arche/generic"
"github.com/mlange-42/tiny-world/game/res"
)

// UI is a system to render the user interface.
type CenterView struct {
view generic.Resource[res.View]
screen generic.Resource[res.EbitenImage]
terrain generic.Resource[res.Terrain]

isInitialized bool
}

// InitializeUI the system
func (s *CenterView) InitializeUI(world *ecs.World) {
s.view = generic.NewResource[res.View](world)
s.screen = generic.NewResource[res.EbitenImage](world)
s.terrain = generic.NewResource[res.Terrain](world)
}

// UpdateUI the system
func (s *CenterView) UpdateUI(world *ecs.World) {
if !s.isInitialized {
view := s.view.Get()
screen := s.screen.Get()
terrain := s.terrain.Get()
view.Center(image.Point{terrain.Width() / 2, terrain.Height() / 2}, screen.Width, screen.Height)
s.isInitialized = true
}
}

// PostUpdateUI the system
func (s *CenterView) PostUpdateUI(world *ecs.World) {}

// FinalizeUI the system
func (s *CenterView) FinalizeUI(world *ecs.World) {}
16 changes: 16 additions & 0 deletions game/res/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ type View struct {
MouseOffset int
}

func NewView(tileWidth, tileHeight int) View {
return View{
TileWidth: tileWidth,
TileHeight: tileHeight,
Zoom: 1,
MouseOffset: tileHeight,
}
}

func (v *View) Center(cell image.Point, screenWidth, screenHeight int) {
pos := v.TileToGlobal(cell.X, cell.Y)
v.Zoom = 1
v.X = pos.X - screenWidth/2
v.Y = pos.Y - screenHeight/2
}

func (v *View) Offset() image.Point {
return image.Pt(int(float64(v.X)*v.Zoom), int(float64(v.Y)*v.Zoom))
}
Expand Down
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/mlange-42/tiny-world/game/terr"
)

const (
worldSize = 256
)

func main() {
ebiten.SetVsyncEnabled(true)
g := game.NewGame(model.New())
Expand All @@ -21,13 +25,13 @@ func main() {

ecs.AddResource(&g.Model.World, &g.Screen)

terrain := res.Terrain{Grid: res.NewGrid[terr.Terrain](100, 100)}
terrain := res.Terrain{Grid: res.NewGrid[terr.Terrain](worldSize, worldSize)}
ecs.AddResource(&g.Model.World, &terrain)

landUse := res.LandUse{Grid: res.NewGrid[terr.Terrain](100, 100)}
landUse := res.LandUse{Grid: res.NewGrid[terr.Terrain](worldSize, worldSize)}
ecs.AddResource(&g.Model.World, &landUse)

landUseEntities := res.LandUseEntities{Grid: res.NewGrid[ecs.Entity](100, 100)}
landUseEntities := res.LandUseEntities{Grid: res.NewGrid[ecs.Entity](worldSize, worldSize)}
ecs.AddResource(&g.Model.World, &landUseEntities)

selection := res.Selection{}
Expand All @@ -47,12 +51,7 @@ func main() {
ecs.AddResource(&g.Model.World, &hud)
ecs.AddResource(&g.Model.World, &ui)

view := res.View{
TileWidth: 48,
TileHeight: 24,
Zoom: 1,
MouseOffset: 24,
}
view := res.NewView(48, 24)
ecs.AddResource(&g.Model.World, &view)

production := res.Production{}
Expand Down Expand Up @@ -80,6 +79,7 @@ func main() {

// =========== UI Systems ===========

g.Model.AddUISystem(&render.CenterView{})
g.Model.AddUISystem(&render.Terrain{})
g.Model.AddUISystem(&render.UI{})

Expand Down

0 comments on commit bb9736b

Please sign in to comment.