Skip to content

Commit

Permalink
added island example
Browse files Browse the repository at this point in the history
  • Loading branch information
zfedoran committed Apr 28, 2022
1 parent ebd5d12 commit 98d2a4d
Show file tree
Hide file tree
Showing 43 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion example/README.md → examples/islands/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Example
# Island Example

You can run this example (assuming you have go 1.7) using the following commands.

Expand Down
Binary file added examples/islands/internal/input/tile-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/input/tile-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/islands/internal/output/wave.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions examples/islands/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"fmt"
"time"

"github.com/zfedoran/go-wfc/pkg/wfc"
)

// Use 2 color lookups to generate adjacency hash values
var constraintFn = wfc.GetConstraintFunc(2)

func collapseWave(tileset_folder, output_image string) {
// This is just a `[]image.Image`, you can use whatever loader function you'd like
images, err := wfc.LoadImageFolder(tileset_folder)
if err != nil {
panic(err)
}

// The random seed to use when collapsing the wave
// (given the same seed number, the Collapse() fn would generate the same state every time)
seed := int(time.Now().UnixNano())

width := 8
height := 8

// Setup the initialized state
wave := wfc.NewWithCustomConstraints(images, width, height, constraintFn)
wave.Initialize(seed)

water := wfc.GetConstraintFromHex("95282254")

// Top
for i := 0; i < width; i++ {
slot := wave.PossibilitySpace[i]
modules := make([]*wfc.Module, 0)
for _, m := range slot.Superposition {
if m.Adjacencies[wfc.Up] == water {
modules = append(modules, m)
}
}
slot.Superposition = modules
}

// Bottom
for i := 0; i < width; i++ {
slot := wave.PossibilitySpace[i+width*(height-1)]
modules := make([]*wfc.Module, 0)
for _, m := range slot.Superposition {
if m.Adjacencies[wfc.Down] == water {
modules = append(modules, m)
}
}
slot.Superposition = modules
}

// Left
for i := 0; i < height; i++ {
slot := wave.PossibilitySpace[i*width]
modules := make([]*wfc.Module, 0)
for _, m := range slot.Superposition {
if m.Adjacencies[wfc.Left] == water {
modules = append(modules, m)
}
}
slot.Superposition = modules
}

// Right
for i := 0; i < height; i++ {
slot := wave.PossibilitySpace[(i+1)*width-1]
modules := make([]*wfc.Module, 0)
for _, m := range slot.Superposition {
if m.Adjacencies[wfc.Right] == water {
modules = append(modules, m)
}
}
slot.Superposition = modules
}

// Collapse the wave function (make up to 100 attempts)
err = wave.Collapse(200)
if err != nil {
// don't panic here, we want to generate the image anyway
fmt.Printf("unable to generate: %v", err)
}

// Lets generate an image
output := wave.ExportImage()
output_file := fmt.Sprintf(output_image, seed)

wfc.SaveImage(output_file, output)
fmt.Printf("Image saved to: %s\n", output_file)
}

func printAdjacencyHashValues(input_tileset string) {
fmt.Printf("Adjacency hash values:\n\n")

images, err := wfc.LoadImageFolder(input_tileset)
if err != nil {
panic(err)
}

// We could use pretty table to do this, but this is just a demo and I don't
// want the extra dependency.

fmt.Println("|-------|----------|----------|")
fmt.Println("|Tile\t|Direction |Hash |")
fmt.Println("|-------|----------|----------|")
for i, img := range images {
for _, d := range wfc.Directions {
fmt.Printf("|%d\t|%s\t | %s | %dx%d\n", i, d.ToString(), constraintFn(img, d), img.Bounds().Max.X, img.Bounds().Max.Y)
}
fmt.Printf("|- - - -|- - - - - |- - - - - |\n")
}
fmt.Printf("|-------|----------|----------|\n\n")
}

func main() {
input_tileset := "./internal/input"
output_image := "./internal/output/%d.png"

// Print the adjacency hash values for the provided tileset.
printAdjacencyHashValues(input_tileset)

// Generate an image from the tileset.
collapseWave(input_tileset, output_image)
}
12 changes: 12 additions & 0 deletions examples/platformer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Platform Example

![sample](internal/output/wave.png?raw=true)

You can run this example (assuming you have go 1.7) using the following commands.

```bash
cd ..
go mod tidy
go run example/main.go
```

File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.

0 comments on commit 98d2a4d

Please sign in to comment.