doodle/pkg/level/chunk_test.go
Noah Petherbridge 8965a7d86a Doodads: Crumbly Floor, Start Flag & State Blocks
Add new doodads:

* Start Flag: place this in a level to set the spawn point of the player
  character. If no flag is found, the player spawns at 0,0 in the top
  corner of the map. Only use one Start Flag per level, otherwise the
  player will randomly spawn at one of them.
* Crumbly Floor: a solid floor that begins to shake and then fall apart
  after a moment when a mobile character steps on it. The floor respawns
  after 5 seconds.
* State Blocks: blue and orange blocks that toggle between solid and
  pass-thru whenever a State Button is activated.
* State Button: a solid "ON/OFF" block that toggles State Blocks back
  and forth when touched. Only activates if touched on the side or bottom;
  acts as a solid floor when walked on from the top.

New features for doodad scripts:

* Actor scripts: call SetMobile(true) to mark an actor as a mobile mob
  (i.e. player character or enemy). Other doodads can check if the actor
  colliding with them IsMobile so they don't activate if placed too close
  to other (non-mobile) doodads in a level. The Blue and Red Azulians
  are the only mobile characters so far.
* Message.Broadcast allows sending a pub/sub message out to ALL doodads
  in the level, instead of only to linked doodads as Message.Publish does.
  This is used for the State Blocks to globally communicate on/off status
  without needing to link them all together manually.
2019-12-30 18:13:28 -08:00

307 lines
5.7 KiB
Go

package level_test
import (
"fmt"
"testing"
"git.kirsle.net/apps/doodle/pkg/level"
"git.kirsle.net/go/render"
)
// Test the high level Chunker.
func TestChunker(t *testing.T) {
c := level.NewChunker(1000)
// Test swatches.
var (
grey = &level.Swatch{
Name: "solid",
Color: render.Grey,
}
)
type testCase struct {
name string
run func() error
}
tests := []testCase{
testCase{
name: "Access a pixel on the blank map and expect an error",
run: func() error {
p := render.NewPoint(65535, -214564545)
_, err := c.Get(p)
if err == nil {
return fmt.Errorf("unexpected success getting point %s", p)
}
return nil
},
},
testCase{
name: "Set a pixel",
run: func() error {
// Set a point.
p := render.NewPoint(100, 200)
err := c.Set(p, grey)
if err != nil {
return fmt.Errorf("unexpected error getting point %s: %s", p, err)
}
return nil
},
},
testCase{
name: "Verify the set pixel",
run: func() error {
p := render.NewPoint(100, 200)
px, err := c.Get(p)
if err != nil {
return err
}
if px != grey {
return fmt.Errorf("pixel at %s not the expected color:\n"+
"Expected: %s\n"+
" Got: %s",
p,
grey,
px,
)
}
return nil
},
},
testCase{
name: "Verify the neighboring pixel is unset",
run: func() error {
p := render.NewPoint(101, 200)
_, err := c.Get(p)
if err == nil {
return fmt.Errorf("unexpected success getting point %s", p)
}
return nil
},
},
testCase{
name: "Delete the set pixel",
run: func() error {
p := render.NewPoint(100, 200)
err := c.Delete(p)
if err != nil {
return err
}
return nil
},
},
testCase{
name: "Verify the deleted pixel is unset",
run: func() error {
p := render.NewPoint(100, 200)
_, err := c.Get(p)
if err == nil {
return fmt.Errorf("unexpected success getting point %s", p)
}
return nil
},
},
testCase{
name: "Delete a pixel that didn't exist",
run: func() error {
p := render.NewPoint(-100, -100)
err := c.Delete(p)
if err == nil {
return fmt.Errorf("unexpected success deleting point %s", p)
}
return nil
},
},
}
for _, test := range tests {
if err := test.run(); err != nil {
t.Errorf("Failed: %s\n%s", test.name, err)
}
}
}
// Test the map chunk accessor.
func TestMapAccessor(t *testing.T) {
a := level.NewMapAccessor()
_ = a
// Test action types
var (
Get = "Get"
Set = "Set"
Delete = "Delete"
)
// Test swatches.
var (
red = &level.Swatch{
Name: "fire",
Color: render.Red,
}
)
type testCase struct {
Action string
P render.Point
S *level.Swatch
Expect *level.Swatch
Err bool // expect error
}
tests := []testCase{
// Get a random point and expect to fail.
testCase{
Action: Get,
P: render.NewPoint(128, 128),
Err: true,
},
// Set a point.
testCase{
Action: Set,
S: red,
P: render.NewPoint(1024, 2048),
},
// Verify it exists.
testCase{
Action: Get,
P: render.NewPoint(1024, 2048),
Expect: red,
},
// A neighboring point does not exist.
testCase{
Action: Get,
P: render.NewPoint(1025, 2050),
Err: true,
},
// Delete a pixel that doesn't exist.
testCase{
Action: Delete,
P: render.NewPoint(1987, 2006),
Err: true,
},
// Delete one that does.
testCase{
Action: Delete,
P: render.NewPoint(1024, 2048),
},
// Verify gone
testCase{
Action: Get,
P: render.NewPoint(1024, 2048),
Err: true,
},
}
for _, test := range tests {
var px *level.Swatch
var err error
switch test.Action {
case Get:
px, err = a.Get(test.P)
case Set:
err = a.Set(test.P, test.S)
case Delete:
err = a.Delete(test.P)
}
if err != nil && !test.Err {
t.Errorf("unexpected error from %s %s: %s", test.Action, test.P, err)
continue
} else if err == nil && test.Err {
t.Errorf("didn't get error when we expected from %s %s", test.Action, test.P)
continue
}
if test.Action == Get {
if px != test.Expect {
t.Errorf("didn't get expected result\n"+
"Expected: %s\n"+
" Got: %s\n",
test.Expect,
px,
)
}
}
}
}
// Test the ChunkCoordinate function.
func TestChunkCoordinates(t *testing.T) {
c := level.NewChunker(1000)
type testCase struct {
In render.Point
Expect render.Point
}
tests := []testCase{
testCase{
In: render.NewPoint(0, 0),
Expect: render.NewPoint(0, 0),
},
testCase{
In: render.NewPoint(128, 128),
Expect: render.NewPoint(0, 0),
},
testCase{
In: render.NewPoint(1024, 128),
Expect: render.NewPoint(1, 0),
},
testCase{
In: render.NewPoint(3600, 1228),
Expect: render.NewPoint(3, 1),
},
testCase{
In: render.NewPoint(-100, -1),
Expect: render.NewPoint(-1, -1),
},
testCase{
In: render.NewPoint(-950, 100),
Expect: render.NewPoint(-1, 0),
},
testCase{
In: render.NewPoint(-1001, -856),
Expect: render.NewPoint(-2, -1),
},
testCase{
In: render.NewPoint(-3600, -4800),
Expect: render.NewPoint(-4, -5),
},
}
for _, test := range tests {
actual := c.ChunkCoordinate(test.In)
if actual != test.Expect {
t.Errorf(
"Failed ChunkCoordinate conversion:\n"+
" Input: %s\n"+
"Expected: %s\n"+
" Got: %s",
test.In,
test.Expect,
actual,
)
}
}
}
func TestZeroChunkSize(t *testing.T) {
c := &level.Chunker{}
coord := c.ChunkCoordinate(render.NewPoint(1200, 3600))
if !coord.IsZero() {
t.Errorf("ChunkCoordinate didn't fail with a zero chunk size!")
}
}