Created
September 9, 2014 11:18
-
-
Save paked/3fd440b1bbef0dcd8246 to your computer and use it in GitHub Desktop.
Component Based Game Engine Muckery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/veandco/go-sdl2/sdl" | |
"log" | |
"os" | |
) | |
type WindowDetails struct { | |
Title string | |
Width int | |
Height int | |
} | |
type Entity struct { | |
Components map[string]Component | |
} | |
func (e *Entity) Initialize() { | |
e.Components = make(map[string]Component) | |
} | |
func (e *Entity) Update() { | |
var component Component | |
for _, component = range e.Components { | |
component.Update() | |
component.Render() | |
} | |
} | |
func (e *Entity) AddComponent(component Component) { | |
e.Components[component.GetName()] = component | |
} | |
func (e *Entity) GetComponent(name string) Component { | |
var component Component | |
for _, component = range e.Components { | |
if component.GetName() == name { | |
return component | |
} | |
} | |
return component | |
} | |
type Component interface { | |
GetName() string | |
GetOwner() Entity | |
Update() | |
Render() | |
} | |
type BaseComponent struct { | |
owner string | |
} | |
func (bc BaseComponent) GetName() string { | |
return "Base Component" | |
} | |
func (bc BaseComponent) Update() { | |
} | |
func (bc BaseComponent) Render() { | |
// owner := bc.GetOwner() | |
// component := owner.GetComponent("Position") | |
// x, y := component.GetPosition() | |
// component.SetPosition(x+1, y+1) | |
// // log.Printf("%v %v", x, y) | |
// w, h := int32(100), int32(100) | |
// renderer.SetDrawColor(0, 255, 200, 255) | |
// renderer.DrawRect(&sdl.Rect{x, y, (x + w), y + h}) | |
} | |
func (bc BaseComponent) GetOwner() Entity { | |
return entityPool[bc.owner] | |
} | |
type PositionComponent struct { | |
owner string | |
x int32 | |
y int32 | |
} | |
func (pc *PositionComponent) GetPosition() (int32, int32) { | |
return pc.x, pc.y | |
} | |
func (pc *PositionComponent) SetPosition(x int32, y int32) { | |
pc.x = x | |
pc.y = y | |
} | |
func (pc PositionComponent) GetName() string { | |
return "Position" | |
} | |
func (pc PositionComponent) Update() { | |
// log.Printf("[Updating: %v]", pc.GetName()) | |
} | |
func (pc PositionComponent) Render() { | |
} | |
func (pc PositionComponent) GetOwner() Entity { | |
return entityPool[pc.owner] | |
} | |
var ( | |
window *sdl.Window | |
renderer *sdl.Renderer | |
entityPool = make(map[string]Entity) | |
) | |
func main() { | |
specs := WindowDetails{"Intrinsic", 960, 480} | |
window = sdl.CreateWindow(specs.Title, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, specs.Width, specs.Height, sdl.WINDOW_SHOWN) | |
if window == nil { | |
log.Fatalln("Unable to create window") | |
os.Exit(1) | |
} | |
renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) | |
if renderer == nil { | |
log.Fatalln("Unable to create renderer") | |
os.Exit(2) | |
} | |
entity := Entity{} | |
entity.Initialize() | |
entity.AddComponent(BaseComponent{"01"}) | |
entity.AddComponent(PositionComponent{"01", 100, 100}) | |
running := true | |
pc := entity.GetComponent("Position").(PositionComponent) | |
// pc.SetPosition(100, 200) | |
// x, y := pc.GetPosition() | |
// log.Printf("%v %v", x, y) | |
entityPool["01"] = entity | |
for running { | |
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { | |
switch event.(type) { | |
case *sdl.QuitEvent: | |
running = false | |
} | |
} | |
renderer.SetDrawColor(255, 0, 100, 255) | |
renderer.Clear() | |
entity.Update() | |
renderer.Present() | |
} | |
renderer.Destroy() | |
window.Destroy() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment