Add basic world object and tests
This commit is contained in:
parent
8a17fcf59f
commit
8c4bf4f75f
2 changed files with 56 additions and 0 deletions
32
pkg/rovegame/world.go
Normal file
32
pkg/rovegame/world.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package rovegame
|
||||||
|
|
||||||
|
// World describes a self contained universe and everything in it
|
||||||
|
type World struct {
|
||||||
|
instances []Instance
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instance describes a single entity or instance of an entity in the world
|
||||||
|
type Instance struct {
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWorld creates a new world object
|
||||||
|
func NewWorld() *World {
|
||||||
|
return &World{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds an instance to the game
|
||||||
|
func (w *World) CreateInstance() int {
|
||||||
|
// Simple ID to start with
|
||||||
|
id := len(w.instances)
|
||||||
|
|
||||||
|
// Initialise the instance
|
||||||
|
instance := Instance{
|
||||||
|
id: id,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the instance to the list
|
||||||
|
w.instances = append(w.instances, instance)
|
||||||
|
|
||||||
|
return id
|
||||||
|
}
|
24
pkg/rovegame/world_test.go
Normal file
24
pkg/rovegame/world_test.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package rovegame
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewWorld(t *testing.T) {
|
||||||
|
// Very basic for now, nothing to verify
|
||||||
|
world := NewWorld()
|
||||||
|
if world == nil {
|
||||||
|
t.Error("Failed to create world")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_CreateInstance(t *testing.T) {
|
||||||
|
world := NewWorld()
|
||||||
|
a := world.CreateInstance()
|
||||||
|
b := world.CreateInstance()
|
||||||
|
|
||||||
|
// Basic duplicate check
|
||||||
|
if a == b {
|
||||||
|
t.Errorf("Created identical instances")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue