2019-10-08 17:54:23 +02:00
|
|
|
// Copyright 2019 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// This demo is inspired by the xscreensaver 'squirals'.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2020-06-07 14:36:46 +02:00
|
|
|
"log"
|
2019-10-08 17:54:23 +02:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2019-10-08 17:54:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
width = 800
|
|
|
|
height = 600
|
|
|
|
scale = 1
|
|
|
|
numOfSquirals = width / 32
|
|
|
|
)
|
|
|
|
|
|
|
|
type palette struct {
|
|
|
|
name string
|
|
|
|
colors []color.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
background = color.Black
|
|
|
|
|
|
|
|
palettes = []palette{
|
|
|
|
{
|
|
|
|
name: "sand dunes",
|
|
|
|
colors: []color.Color{
|
|
|
|
color.RGBA{0xF2, 0x74, 0x05, 0xFF}, // #F27405
|
|
|
|
color.RGBA{0xD9, 0x52, 0x04, 0xFF}, // #D95204
|
|
|
|
color.RGBA{0x40, 0x18, 0x01, 0xFF}, // #401801
|
|
|
|
color.RGBA{0xA6, 0x2F, 0x03, 0xFF}, // #A62F03
|
|
|
|
color.RGBA{0x73, 0x2A, 0x19, 0xFF}, // #732A19
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mono desert sand",
|
|
|
|
colors: []color.Color{
|
|
|
|
color.RGBA{0x7F, 0x6C, 0x52, 0xFF}, // #7F6C52
|
|
|
|
color.RGBA{0xFF, 0xBA, 0x58, 0xFF}, // #FFBA58
|
|
|
|
color.RGBA{0xFF, 0xD9, 0xA5, 0xFF}, // #FFD9A5
|
|
|
|
color.RGBA{0x7F, 0x50, 0x0F, 0xFF}, // #7F500F
|
|
|
|
color.RGBA{0xCC, 0xAE, 0x84, 0xFF}, // #CCAE84
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "land sea gradient",
|
|
|
|
colors: []color.Color{
|
|
|
|
color.RGBA{0x00, 0xA2, 0xE8, 0xFF}, // #00A2E8
|
|
|
|
color.RGBA{0x67, 0xA3, 0xF5, 0xFF}, // #67A3F5
|
|
|
|
color.RGBA{0xFF, 0xFF, 0xD5, 0xFF}, // #FFFFD5
|
|
|
|
color.RGBA{0xDD, 0xE8, 0x0C, 0xFF}, // #DDE80C
|
|
|
|
color.RGBA{0x74, 0x9A, 0x0D, 0xFF}, // #749A0D
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
|
2019-10-08 17:54:23 +02:00
|
|
|
// blocker is an arbitrary color used to prevent the
|
|
|
|
// squirals from leaving the canvas.
|
|
|
|
blocker = color.RGBA{0, 0, 0, 254}
|
|
|
|
|
|
|
|
// dirCycles defines by offset which direction a squiral
|
|
|
|
// should try next for the two cases:
|
|
|
|
// clockwise:
|
|
|
|
// 1. try to turn right (index+1)
|
|
|
|
// 2. try to go straight (index+0)
|
|
|
|
// 3. try to turn left (index+3)
|
|
|
|
// counter-clockwise:
|
|
|
|
// 1. try to turn left (index+3)
|
|
|
|
// 2. try to go straight (index+0)
|
|
|
|
// 3. try to turn right (index+1)
|
|
|
|
dirCycles = [2][3]int{
|
2019-10-11 18:09:42 +02:00
|
|
|
{1, 0, 3}, // cw
|
|
|
|
{3, 0, 1}, // ccw
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// dirs contains vectors for the directions: east, south, west, north
|
|
|
|
// in the specified order.
|
2019-10-11 18:09:42 +02:00
|
|
|
dirs = [4]vec2{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
|
2019-10-08 17:54:23 +02:00
|
|
|
|
|
|
|
// neighbors defines neighboring cells depending on the moving
|
|
|
|
// direction of the squiral:
|
|
|
|
// index of 0 -> squiral moves vertically,
|
|
|
|
// index of 1 -> squiral moves horizontally.
|
|
|
|
// These neighbors are tested for "collisions" during simulation.
|
|
|
|
neighbors = [2][2]vec2{
|
2019-10-11 18:09:42 +02:00
|
|
|
{{0, 1}, {0, -1}}, // east, west
|
|
|
|
{{1, 0}, {-1, 0}}, // south, north
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type vec2 struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
type squiral struct {
|
|
|
|
speed int
|
|
|
|
pos vec2
|
|
|
|
dir int
|
|
|
|
rot int
|
|
|
|
col color.Color
|
|
|
|
dead bool
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
func (s *squiral) spawn(game *Game) {
|
2019-10-08 17:54:23 +02:00
|
|
|
s.dead = false
|
|
|
|
|
|
|
|
rx := rand.Intn(width-4) + 2
|
|
|
|
ry := rand.Intn(height-4) + 2
|
|
|
|
|
|
|
|
for dx := -2; dx <= 2; dx++ {
|
|
|
|
for dy := -2; dy <= 2; dy++ {
|
|
|
|
tx, ty := rx+dx, ry+dy
|
2021-07-21 17:15:30 +02:00
|
|
|
if game.auto.colorMap[tx][ty] != background {
|
2019-10-08 17:54:23 +02:00
|
|
|
s.dead = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.speed = rand.Intn(5) + 1
|
|
|
|
s.pos.x = rx
|
|
|
|
s.pos.y = ry
|
|
|
|
s.dir = rand.Intn(4)
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
game.colorCycle = (game.colorCycle + 1) % len(palettes[game.selectedPalette].colors)
|
|
|
|
s.col = palettes[game.selectedPalette].colors[game.colorCycle]
|
2019-10-08 17:54:23 +02:00
|
|
|
|
|
|
|
s.rot = rand.Intn(2)
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
func (s *squiral) step(game *Game) {
|
2019-10-08 17:54:23 +02:00
|
|
|
if s.dead {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
x, y := s.pos.x, s.pos.y // shorthands
|
|
|
|
|
|
|
|
change := rand.Intn(1000)
|
|
|
|
if change < 2 {
|
|
|
|
// On 0.2% of iterations, switch rotation direction.
|
|
|
|
s.rot = (s.rot + 1) % 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. try to advance the spiral in its rotation
|
|
|
|
// direction (clockwise or counter-clockwise).
|
|
|
|
for _, next := range dirCycles[s.rot] {
|
|
|
|
dir := (s.dir + next) % 4
|
|
|
|
off := dirs[dir]
|
|
|
|
// Peek all targets by priority.
|
|
|
|
target := vec2{
|
|
|
|
x: x + off.x,
|
|
|
|
y: y + off.y,
|
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
if game.auto.colorMap[target.x][target.y] == background {
|
2019-10-08 17:54:23 +02:00
|
|
|
// If the target is free we need to also check the
|
|
|
|
// surrounding cells.
|
|
|
|
|
|
|
|
// a. Test if next cell in direction dir does not have
|
|
|
|
// the same color as this squiral.
|
|
|
|
ntarg := vec2{
|
|
|
|
x: target.x + off.x,
|
|
|
|
y: target.y + off.y,
|
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
if game.auto.colorMap[ntarg.x][ntarg.y] == s.col {
|
2019-10-08 17:54:23 +02:00
|
|
|
// If this has the same color, we cannot go into this direction,
|
|
|
|
// to avoid ugly blocks of equal color.
|
|
|
|
continue // try next direction
|
|
|
|
}
|
|
|
|
|
|
|
|
// b. Test all outer fields for the color of the
|
|
|
|
// squiral itself.
|
|
|
|
horivert := dir % 2
|
|
|
|
xtarg := vec2{}
|
|
|
|
set := true
|
|
|
|
for _, out := range neighbors[horivert] {
|
|
|
|
xtarg.x = target.x + out.x
|
|
|
|
xtarg.y = target.y + out.y
|
|
|
|
|
|
|
|
// If one of the outer targets equals the squiral's
|
|
|
|
// color, again continue with next direction.
|
2021-07-21 17:15:30 +02:00
|
|
|
if game.auto.colorMap[xtarg.x][xtarg.y] == s.col {
|
2019-10-08 17:54:23 +02:00
|
|
|
// If this is not free we cannot go into this direction.
|
|
|
|
set = false
|
|
|
|
break // try next direction
|
|
|
|
}
|
|
|
|
|
|
|
|
xtarg.x = ntarg.x + out.x
|
|
|
|
xtarg.y = ntarg.y + out.y
|
|
|
|
|
|
|
|
// If one of the outer targets equals the squiral's
|
|
|
|
// color, again continue with next direction.
|
2021-07-21 17:15:30 +02:00
|
|
|
if game.auto.colorMap[xtarg.x][xtarg.y] == s.col {
|
2019-10-08 17:54:23 +02:00
|
|
|
// If this is not free we cannot go into this direction.
|
|
|
|
set = false
|
|
|
|
break // try next direction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if set {
|
|
|
|
s.pos = target
|
|
|
|
s.dir = dir
|
|
|
|
// 2. set the color of this squiral to its
|
|
|
|
// current position.
|
2021-07-21 17:15:30 +02:00
|
|
|
game.setpix(s.pos, s.col)
|
2019-10-08 17:54:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.dead = true
|
|
|
|
}
|
|
|
|
|
|
|
|
type automaton struct {
|
|
|
|
squirals [numOfSquirals]squiral
|
|
|
|
colorMap [width][height]color.Color
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
func (au *automaton) init(game *Game) {
|
2019-10-08 17:54:23 +02:00
|
|
|
// Init the test grid with color (0,0,0,0) and the borders of
|
|
|
|
// it with color(0,0,0,254) as a blocker color, so the squirals
|
|
|
|
// cannot escape the scene.
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
if x == 0 || x == width-1 || y == 0 || y == height-1 {
|
2021-07-21 17:15:30 +02:00
|
|
|
au.colorMap[x][y] = blocker
|
2019-10-08 17:54:23 +02:00
|
|
|
} else {
|
2021-07-21 17:15:30 +02:00
|
|
|
au.colorMap[x][y] = background
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < numOfSquirals; i++ {
|
2021-07-21 17:15:30 +02:00
|
|
|
au.squirals[i].spawn(game)
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
func (a *automaton) step(game *Game) {
|
2019-10-08 17:54:23 +02:00
|
|
|
for i := 0; i < numOfSquirals; i++ {
|
2021-07-21 17:15:30 +02:00
|
|
|
for s := 0; s < a.squirals[i].speed; s++ {
|
|
|
|
a.squirals[i].step(game)
|
|
|
|
if a.squirals[i].dead {
|
|
|
|
a.squirals[i].spawn(game)
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2021-07-21 17:15:30 +02:00
|
|
|
}
|
2019-10-08 17:54:23 +02:00
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
type Game struct {
|
|
|
|
selectedPalette int
|
|
|
|
colorCycle int
|
|
|
|
canvas *ebiten.Image
|
|
|
|
auto automaton
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
func NewGame() *Game {
|
|
|
|
g := &Game{
|
|
|
|
canvas: ebiten.NewImage(width, height),
|
|
|
|
}
|
|
|
|
g.canvas.Fill(background)
|
|
|
|
g.auto.init(g)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) setpix(xy vec2, col color.Color) {
|
|
|
|
g.canvas.Set(xy.x, xy.y, col)
|
|
|
|
g.auto.colorMap[xy.x][xy.y] = col
|
|
|
|
}
|
2020-06-07 14:36:46 +02:00
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2019-10-08 17:54:23 +02:00
|
|
|
reset := false
|
|
|
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyB) {
|
|
|
|
if background == color.White {
|
|
|
|
background = color.Black
|
|
|
|
} else {
|
|
|
|
background = color.White
|
|
|
|
}
|
|
|
|
reset = true
|
|
|
|
} else if inpututil.IsKeyJustPressed(ebiten.KeyT) {
|
2021-07-21 17:15:30 +02:00
|
|
|
g.selectedPalette = (g.selectedPalette + 1) % len(palettes)
|
2019-10-08 17:54:23 +02:00
|
|
|
reset = true
|
|
|
|
} else if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
|
|
|
reset = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if reset {
|
2021-07-21 17:15:30 +02:00
|
|
|
g.canvas.Fill(background)
|
|
|
|
g.auto.init(g)
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
g.auto.step(g)
|
2019-10-08 17:54:23 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2021-07-21 17:15:30 +02:00
|
|
|
screen.DrawImage(g.canvas, nil)
|
2019-10-08 17:54:23 +02:00
|
|
|
ebitenutil.DebugPrintAt(
|
|
|
|
screen,
|
2022-07-17 04:25:45 +02:00
|
|
|
fmt.Sprintf("TPS: %0.2f, FPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()),
|
2019-10-08 17:54:23 +02:00
|
|
|
1, 0,
|
|
|
|
)
|
|
|
|
ebitenutil.DebugPrintAt(
|
|
|
|
screen,
|
|
|
|
"[r]: respawn",
|
|
|
|
1, 16,
|
|
|
|
)
|
|
|
|
ebitenutil.DebugPrintAt(
|
|
|
|
screen,
|
|
|
|
"[b]: toggle background (white/black)",
|
|
|
|
1, 32,
|
|
|
|
)
|
|
|
|
ebitenutil.DebugPrintAt(
|
|
|
|
screen,
|
2021-07-21 17:15:30 +02:00
|
|
|
fmt.Sprintf("[t]: cycle theme (current: %s)", palettes[g.selectedPalette].name),
|
2019-10-08 17:54:23 +02:00
|
|
|
1, 48,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return width, height
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:54:23 +02:00
|
|
|
func main() {
|
2022-07-17 04:25:45 +02:00
|
|
|
ebiten.SetTPS(250)
|
2020-06-07 14:36:46 +02:00
|
|
|
ebiten.SetWindowSize(width*scale, height*scale)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Squirals (Ebitengine Demo)")
|
2021-07-21 17:15:30 +02:00
|
|
|
if err := ebiten.RunGame(NewGame()); err != nil {
|
2020-06-07 14:36:46 +02:00
|
|
|
log.Fatal(err)
|
2019-10-08 17:54:23 +02:00
|
|
|
}
|
|
|
|
}
|