Skip to content

Commit

Permalink
Merge pull request #10 from cbeimers113/dev-patch-7
Browse files Browse the repository at this point in the history
Dev patch 7
  • Loading branch information
cbeimers113 authored Jun 7, 2023
2 parents 31b476e + c377089 commit 502db0f
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 68 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ go run main.go

### Current Features:

* Left click and drag to rotate around map
* Right click and drag to pan
* Scroll wheel to zoom in and out
* Right click a tile to plant a plant
* Plants will slowly grow

* Controls and info section on HUD
* Randomly generated hextile map
* Water that flows down the map's topography
* Plants which will grow on tiles over time
4 changes: 3 additions & 1 deletion game/creature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ func OnLeftClickCreature(creature *Entity) {

// Update a creature
func UpdateCreature(creature *Entity) {

if !IsPaused {

}
}
2 changes: 1 addition & 1 deletion game/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (entity *Entity) InfoString() (infoString string) {
infoString += fmt.Sprintf("type=%s\n", tileData.Type.Name)
infoString += fmt.Sprintf("temperature=%s\n", tileData.Temperature)
infoString += fmt.Sprintf("water level=%s\n", tileData.WaterLevel)
infoString += fmt.Sprintf("elevation=%.2f\n", entity.GetElevation())
infoString += fmt.Sprintf("elevation=%s\n", entity.GetElevation())
infoString += fmt.Sprintf("planted=%t\n", tileData.Planted)
}
case Plant:
Expand Down
12 changes: 10 additions & 2 deletions game/game.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package game

import (
"math/rand"
"time"

"github.com/g3n/engine/app"
Expand All @@ -20,11 +21,15 @@ var Scene *core.Node
var Cam *camera.Camera
var Win *window.GlfwWindow

// This determines if everything in the simulation is frozen, including the player
var IsFrozen bool

// This determines if the simulation physics are paused, but the player can still interact with the simulation
var IsPaused bool

// Set whether the game is paused
func SetPaused(paused bool) {
IsPaused = paused
IsFrozen = paused

if Win != nil {
switch paused {
Expand Down Expand Up @@ -72,11 +77,14 @@ func Run() {
var tickThreshold float32 = 1000 / float32(SimSpeed)
var deltaTime float32 = 0

// Seed the PRNG
rand.Seed(time.Now().UnixNano())

Application.Run(func(renderer *renderer.Renderer, duration time.Duration) {
Application.Gls().Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
renderer.Render(Scene, Cam)

if !IsPaused {
if !IsFrozen {
// TPS counter
deltaTime += float32(duration.Milliseconds())
if deltaTime >= tickThreshold {
Expand Down
24 changes: 23 additions & 1 deletion game/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var exitButton *gui.Button
// Simulation view components
var simCursor *gui.Image
var infoLabel *gui.Label
var pausedLabel *gui.Label

// Tile context menu components
var tileInfoLabel *gui.Label
Expand All @@ -49,18 +50,30 @@ func infoText() (txt string) {
txt += "Controls:\n"
txt += "WASD to move\n"
txt += "ESC to open menu\n"
txt += "Space to toggle simulation\n"
txt += "\n"
txt += "Left click a tile to add water\n"
txt += "Right click a tile to try to add a plant\n"

// Append the WAILA (what am I looking at?) data
if LookingAt != nil {
txt += "\n"
txt += LookingAt.InfoString()
}

txt += fmt.Sprintf("\nTotal volume of water:%.2f\n", TotalWaterVolume())
txt += fmt.Sprintf("\nTotal water volume=%s\n", TotalWaterVolume)

return
}

// Update the "Simulation Running/Paused" status
func pausedStatus() string {
return fmt.Sprintf("Simulation %s", map[bool]string{
true: "Paused",
false: "Running",
}[IsPaused])
}

// Load the gui
func LoadGui() {
gui.Manager().Set(Scene)
Expand Down Expand Up @@ -160,16 +173,25 @@ func registerSimulationView() {
infoLabel.SetUserData(SimulationView)
Scene.Add(infoLabel)

pausedLabel = gui.NewLabel(pausedStatus())
pausedLabel.SetPosition((float32(width)-pausedLabel.ContentWidth())/2, 0)
pausedLabel.SetUserData(SimulationView)
Scene.Add(pausedLabel)

SetPaused(false)
},

Close: func() {
Scene.Remove(simCursor)
Scene.Remove(infoLabel)
Scene.Remove(pausedLabel)
},

Refresh: func() {
width, _ := Application.GetSize()
infoLabel.SetText(infoText())
pausedLabel.SetText(pausedStatus())
pausedLabel.SetPosition((float32(width)-pausedLabel.ContentWidth())/2, 0)
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions game/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func KeyDown(evname string, ev interface{}) {

switch kev.Key {
case window.KeyEscape:
if IsPaused {
if IsFrozen {
Views[SimulationView].Open(true)
} else {
Views[MainMenu].Open(true)
Expand All @@ -26,6 +26,8 @@ func KeyDown(evname string, ev interface{}) {
PlayerMoveX = 1
case window.KeyA:
PlayerMoveX = -1
case window.KeySpace:
IsPaused = !IsPaused
}
}

Expand Down Expand Up @@ -57,7 +59,7 @@ func KeyHold(evname string, ev interface{}) {
func MouseDown(evname string, ev interface{}) {
me := ev.(*window.MouseEvent)

if !IsPaused && LookingAt != nil {
if !IsFrozen && LookingAt != nil {
switch LookingAt.Type {
case Tile:
switch me.Button {
Expand Down
2 changes: 1 addition & 1 deletion game/plant.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (plant *Entity) growPlant(plantData *PlantData) {

// Perform per-frame updates to a plant
func UpdatePlant(plant *Entity) {
if plantData, ok := plant.UserData().(*PlantData); ok {
if plantData, ok := plant.UserData().(*PlantData); ok && !IsPaused {
plant.growPlant(plantData)
}
}
8 changes: 7 additions & 1 deletion game/quantity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Unit string
const Celcius Unit = "°C"
const Litre Unit = "L"
const Gram Unit = "g"
const Metre Unit = "m"

// Represents an amount of an element
type Quantity struct {
Expand All @@ -29,7 +30,12 @@ func (q *Quantity) String() (str string) {
return
}

// Convert from litres to cubic metres
// Convert from litres to cubic metres (dimensions of one tile is 1 cubic metre)
func LitresToCubicMetres(litres float32) float32 {
return litres / 1000
}

// Convert from cubic metres to litres (dimensions of one tile is 1 cubic metre)
func CubicMetresToLitres(cubicMetres float32) float32 {
return cubicMetres * 1000
}
Loading

0 comments on commit 502db0f

Please sign in to comment.