-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
151 lines (121 loc) · 3.87 KB
/
render.go
File metadata and controls
151 lines (121 loc) · 3.87 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/rafibayer/ants-again/spatial"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)
const (
screenW = 700
screenH = 700
)
func (g *Game) Draw(screen *ebiten.Image) {
g.ui.Draw(screen)
drawScreenSpace(g, screen)
drawWorldSpace(g)
screen.DrawImage(g.world, g.cameraOpts())
g.frameCount++
}
func (g *Game) cameraOpts() *ebiten.DrawImageOptions {
// translate op for drawing world in screen space with pan and zoom
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-g.camX, -g.camY) // Pan (world space)
op.GeoM.Translate(-screenW/2, -screenH/2) // Move pivot to center of screen
op.GeoM.Scale(g.zoom, g.zoom) // Zoom
op.GeoM.Translate(screenW/2, screenH/2) // Move pivot back
return op
}
func (g *Game) screenToWorldSpace(x float64, y float64) (float64, float64) {
inv := g.cameraOpts()
inv.GeoM.Invert()
return inv.GeoM.Apply(x, y)
}
func drawScreenSpace(g *Game, screen *ebiten.Image) {
stats := g.Stats()
ebitenutil.DebugPrint(screen, fmt.Sprintf("%+v", stats))
}
// drawCalls should be ordered from back to front.
func drawWorldSpace(g *Game) {
// has to be first because of use of "writePixels"
g.drawPheromones()
// g.naiveDrawPheromones()
g.drawAnts()
g.drawFood()
g.drawHills()
g.drawObstacles()
// game world bounding box
vector.StrokeRect(g.world, 0, 0, GAME_SIZE, GAME_SIZE, 5, color.White, false)
}
func (g *Game) drawAnts() {
const DEBUG_SENSOR_RATIO = 100
for i, ant := range g.ants {
// debug sensor radius
if g.params.DebugDrawSensorRange && i%DEBUG_SENSOR_RATIO == 0 {
vector.StrokeCircle(g.world, float32(ant.X), float32(ant.Y), float32(g.params.PheromoneSenseRadius), 2.0, WHITE, false)
}
tail := ant.Add(ant.dir.Normalize().Mul(-5))
c := GREEN
if ant.state == RETURN {
c = LILAC
}
vector.StrokeLine(g.world, float32(ant.X), float32(ant.Y), float32(tail.X), float32(tail.Y), 2, c, false)
}
}
func (g *Game) drawFood() {
for food := range g.food.PointsIter() {
c := Fade(BROWN, float32(food.amount)/float32(FOOD_START))
vector.FillRect(g.world, float32(food.X), float32(food.Y), ANT_FOOD_RADIUS, ANT_FOOD_RADIUS, c, false)
}
}
func (g *Game) drawHills() {
for hill := range g.hills.PointsIter() {
vector.FillCircle(g.world, float32(hill.X), float32(hill.Y), ANT_HILL_RADIUS, WHITE, false)
}
}
func (g *Game) drawPheromones() {
// Clear buffer to black (or background color)
for i := range g.px {
g.px[i] = 0
}
writePheromones := func(ph spatial.Spatial[*Pheromone], color color.RGBA) {
for pher := range ph.PointsIter() {
// Fade color by pheromone amount (0..1)
c := Fade(color, pher.amount)
x := int(pher.X)
y := int(pher.Y)
if x < 0 || x >= GAME_SIZE || y < 0 || y >= GAME_SIZE {
continue
}
idx := 4 * (y*GAME_SIZE + x)
g.px[idx+0] = c.R
g.px[idx+1] = c.G
g.px[idx+2] = c.B
g.px[idx+3] = 255
}
}
writePheromones(g.foragingPheromone, DARK_GREEN)
writePheromones(g.returningPheromone, DARK_LILAC)
// Write the pixel buffer to the ebiten.Image once
g.world.WritePixels(g.px)
}
func (g *Game) naiveDrawPheromones() {
for pher := range g.foragingPheromone.PointsIter() {
c := Fade(DARK_GREEN, pher.amount)
vector.FillRect(g.world, float32(pher.X), float32(pher.Y), 3.0, 3.0, c, false)
}
for pher := range g.returningPheromone.PointsIter() {
c := Fade(DARK_LILAC, pher.amount)
vector.FillRect(g.world, float32(pher.X), float32(pher.Y), 3.0, 3.0, c, false)
}
}
func (g *Game) drawObstacles() {
for obs := range g.obstacles.PointsIter() {
// obstacles position represented by top left of square
vector.FillRect(g.world, float32(obs.X), float32(obs.Y), OBSTACLE_HASH_CELL_SIZE, OBSTACLE_HASH_CELL_SIZE, GRAY, false)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return screenW, screenH
}