From 8c4bf4f75f5e9f425af552e09d216db98593b53d Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sat, 30 May 2020 23:42:01 +0100 Subject: [PATCH] Add basic world object and tests --- pkg/rovegame/world.go | 32 ++++++++++++++++++++++++++++++++ pkg/rovegame/world_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 pkg/rovegame/world.go create mode 100644 pkg/rovegame/world_test.go diff --git a/pkg/rovegame/world.go b/pkg/rovegame/world.go new file mode 100644 index 0000000..3b78cc9 --- /dev/null +++ b/pkg/rovegame/world.go @@ -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 +} diff --git a/pkg/rovegame/world_test.go b/pkg/rovegame/world_test.go new file mode 100644 index 0000000..c8124ad --- /dev/null +++ b/pkg/rovegame/world_test.go @@ -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") + } +}