kelindar / tile Goto Github PK
View Code? Open in Web Editor NEWTile is a 2D grid engine, built with data and cache friendly ways, includes pathfinding and observers.
License: MIT License
Tile is a 2D grid engine, built with data and cache friendly ways, includes pathfinding and observers.
License: MIT License
Hi,
this is possibly a dumb question, but I'm not getting it yet :) Let's say I define grid this way:
type Cell struct {
sprite string
}
grid := tile.NewGridOf[Cell](width, height)
When iterating over the grid, like this:
grid.Each(func(point tile.Point, t tile.Tile[Cell]) {
// t.Value() returns an int16 which is 0 in all tiles, no Cell ???
})
Of course I can create another map of cells like this:
cells := make(map[tile.Point]Cell, width*height)
grid.Each(func(point tile.Point, t tile.Tile[Cell]) {
cells[point] = &Cell{"...."}
})
But that way I'd have two grids, the one provided by the tile module and my "shadow" grid holding the cell data (where I'd put things like sprites, properties etc).
What am I doing wrong?
Thanks in advance,
Tom
I noticed that when using the Within
on the grid to iterate through values of all tiles it's slower than when iterating by simply using At
. Here are benchmarks I quickly wrote:
Benchmark testing the performance of the built-in Within
:
func BenchmarkGridWithin(b *testing.B) {
// Generate map
theMap := tile.NewGridOf[tileData](900, 900)
theMap.Each(func(pos point, currentTile tile.Tile[tileData]) {
currentTile.Write(getRandomSliceIndex(tileImages))
})
// Benchmark
chunkSize := 9
for i := 0; i < b.N; i++ {
pos := point{X: int16((i * 9) % 900), Y: int16((i * 45) % 900)}
theMap.Within(pos, pos.Add(point{X: chunkSize * 3, Y: chunkSize * 3}), func(tilePos point, currentTile tile.Tile[tileData]) {
value := currentTile.Value()
_ = tileImages[int(value)]
})
}
}
Benchmark using At
where whole chunk is scanned with a simple double for loop:
func BenchmarkGridAtScan(b *testing.B) {
// Generate map
theMap := tile.NewGridOf[tileData](900, 900)
theMap.Each(func(pos point, currentTile tile.Tile[tileData]) {
currentTile.Write(getRandomSliceIndex(tileImages))
})
// Benchmark
chunkSize := 9
for i := 0; i < b.N; i++ {
pos := point{X: int16((i * 9) % 900), Y: int16((i * 45) % 900)}
for x := pos.X; x < pos.X+chunkSize*3; x++ {
for y := pos.Y; y < pos.Y+chunkSize*3; y++ {
currentTile, ok := theMap.At(x, y)
if !ok {
continue
}
value := currentTile.Value()
_ = tileImages[int(value)]
}
}
}
}
A benchmark that tries to get values using At
but working on a page at a time:
func BenchmarkGridAtPage(b *testing.B) {
// Generate map
theMap := tile.NewGridOf[tileData](900, 900)
theMap.Each(func(pos point, currentTile tile.Tile[tileData]) {
currentTile.Write(getRandomSliceIndex(tileImages))
})
// Benchmark
chunkSize := 9
for i := 0; i < b.N; i++ {
pos := point{X: int16((i * 9) % 900), Y: int16((i * 45) % 900)}
// Iterate through pages
for x := pos.X; x < pos.X+chunkSize*3; x += 3 {
for y := pos.Y; y < pos.Y+chunkSize*3; y += 3 {
// Get all tiles in a page
for tileX := x; tileX < x+3; tileX++ {
for tileY := y; tileY < y+3; tileY++ {
currentTile, ok := theMap.At(tileX, tileY)
if !ok {
continue
}
value := currentTile.Value()
_ = tileImages[int(value)]
}
}
}
}
}
}
Results show that At
is significantly faster than Within
. Also accessing tile's values page-at-a-time increases performance by only โ0.82%:
goos: windows
goarch: amd64
pkg: tile-game
cpu: 12th Gen Intel(R) Core(TM) i9-12900KF
BenchmarkGridWithin-24 531561 2251 ns/op
BenchmarkGridAtScan-24 656180 1826 ns/op
BenchmarkGridAtPage-24 661200 1811 ns/op
I'm not sure if that's intended behavior but something tells me there's an opportunity for adding an optimized values getter.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.