-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (101 loc) · 2.77 KB
/
main.go
File metadata and controls
129 lines (101 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/martinlaursen97/sand/config"
"github.com/martinlaursen97/sand/core"
"github.com/martinlaursen97/sand/maths"
)
type Game struct {
world *core.World
brushSize int
paused bool
particleNum int
mouseClicked bool
prevCursor maths.Vector
currentCursor maths.Vector
}
func (g *Game) Update() error {
dt := 1.0 / ebiten.ActualTPS()
g.world.Update(dt)
cursorPositionX, cursorPositionY := ebiten.CursorPosition()
mouseClicked := ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
spacePressed := ebiten.IsKeyPressed(ebiten.KeySpace)
pickSand := ebiten.IsKeyPressed(ebiten.Key1)
pickWater := ebiten.IsKeyPressed(ebiten.Key2)
pickWall := ebiten.IsKeyPressed(ebiten.Key3)
pickEraser := ebiten.IsKeyPressed(ebiten.Key4)
_, dy := ebiten.Wheel()
if spacePressed {
g.world.Clear()
}
if dy > 0 {
g.brushSize += config.BrushSizeIncrement
}
if dy < 0 {
if g.brushSize-config.BrushSizeIncrement > 0 {
g.brushSize -= config.BrushSizeIncrement
} else {
g.brushSize = 1
}
}
if pickSand {
g.particleNum = 1
}
if pickWater {
g.particleNum = 2
}
if pickWall {
g.particleNum = 3
}
if pickEraser {
g.particleNum = 4
}
if mouseClicked {
g.prevCursor.X = g.currentCursor.X
g.prevCursor.Y = g.currentCursor.Y
g.currentCursor.X = float64(cursorPositionX)
g.currentCursor.Y = float64(cursorPositionY)
// Draw a line between the previous and current cursor position
core.TraversePathAndApplyFunc(
g.prevCursor,
g.currentCursor,
core.FunctionInput{
Func: g.world.DrawWithBrush,
Args: []interface{}{g.brushSize, g.particleNum},
},
)
} else {
g.currentCursor.X = float64(cursorPositionX)
g.currentCursor.Y = float64(cursorPositionY)
}
g.world.Reset()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
g.world.Draw(screen)
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %d", int(ebiten.ActualFPS())))
particleCount := g.world.GetParticleCount()
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Particles: %d", particleCount), 0, 15)
sandCount := g.world.GetSandParticleCount()
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Sand: %d", sandCount), 0, 30)
wallCount := g.world.GetWallParticleCount()
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Walls: %d", wallCount), 0, 45)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return config.ScreenWidth, config.ScreenHeight
}
func main() {
ebiten.SetWindowSize(config.ScreenWidth*2, config.ScreenHeight*2)
ebiten.SetWindowTitle("Sand")
game := &Game{}
game.world = core.NewWorld()
game.brushSize = 5
game.paused = false
game.particleNum = 2
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}