diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e22129 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/dist \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pkg/internal/entities/player.go b/pkg/internal/entities/player.go index 527ba65..26b7788 100644 --- a/pkg/internal/entities/player.go +++ b/pkg/internal/entities/player.go @@ -1,7 +1,7 @@ package entities import ( - "git.solsynth.dev/highland/codingland/pkg/internal/common" + "git.solsynth.dev/highland/codingland/pkg/internal/land" "github.com/veandco/go-sdl2/sdl" ) @@ -11,9 +11,30 @@ const ( ) type Player struct { - Position common.Vector2D - Size common.Vector2D - Velocity common.Vector2D + land.BaseObject + + Position land.Vector2D + Size land.Vector2D + Velocity land.Vector2D +} + +func (p *Player) Update() { + keys := sdl.GetKeyboardState() + if keys[sdl.SCANCODE_UP] == 1 || keys[sdl.SCANCODE_W] == 1 { + p.Move(0, -1) + } + if keys[sdl.SCANCODE_DOWN] == 1 || keys[sdl.SCANCODE_S] == 1 { + p.Move(0, 1) + } + if keys[sdl.SCANCODE_LEFT] == 1 || keys[sdl.SCANCODE_A] == 1 { + p.Move(-1, 0) + } + if keys[sdl.SCANCODE_RIGHT] == 1 || keys[sdl.SCANCODE_D] == 1 { + p.Move(1, 0) + } + + p.Move(0, 0) + p.BaseObject.Update() } func (p *Player) Move(deltaX, deltaY float64) { diff --git a/pkg/internal/land/object.go b/pkg/internal/land/object.go new file mode 100644 index 0000000..9bebab0 --- /dev/null +++ b/pkg/internal/land/object.go @@ -0,0 +1,29 @@ +package land + +import "github.com/veandco/go-sdl2/sdl" + +type Object interface { + Create() + Update() +} + +type DrawableObject interface { + Draw(*sdl.Renderer) +} + +type BaseObject struct { + Children []Object +} + +func (p *BaseObject) Create() {} + +func (p *BaseObject) Update() { + // Update each child + for _, child := range p.Children { + child.Update() + } +} + +func (p *BaseObject) AddChild(child Object) { + p.Children = append(p.Children, child) +} diff --git a/pkg/internal/land/root.go b/pkg/internal/land/root.go new file mode 100644 index 0000000..8ff3ce2 --- /dev/null +++ b/pkg/internal/land/root.go @@ -0,0 +1,17 @@ +package land + +import ( + "github.com/veandco/go-sdl2/sdl" +) + +type RootObject struct { + BaseObject +} + +func (p *RootObject) Draw(pen *sdl.Renderer) { + for _, child := range p.Children { + if drawableChild, ok := child.(DrawableObject); ok { + drawableChild.Draw(pen) + } + } +} diff --git a/pkg/internal/common/vector2d.go b/pkg/internal/land/vector2d.go similarity index 72% rename from pkg/internal/common/vector2d.go rename to pkg/internal/land/vector2d.go index 92ab136..0eecfb7 100644 --- a/pkg/internal/common/vector2d.go +++ b/pkg/internal/land/vector2d.go @@ -1,4 +1,4 @@ -package common +package land type Vector2D struct { X, Y float64 diff --git a/pkg/main.go b/pkg/main.go index a3c3ae0..261211b 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -1,8 +1,8 @@ package main import ( - "git.solsynth.dev/highland/codingland/pkg/internal/common" "git.solsynth.dev/highland/codingland/pkg/internal/entities" + "git.solsynth.dev/highland/codingland/pkg/internal/land" "log" "runtime" @@ -37,10 +37,11 @@ func main() { } defer renderer.Destroy() - player := &entities.Player{ - Position: common.Vector2D{X: windowWidth / 2, Y: windowHeight / 2}, - Size: common.Vector2D{X: playerSize, Y: playerSize}, - } + root := &land.RootObject{} + root.AddChild(&entities.Player{ + Position: land.Vector2D{X: windowWidth / 2, Y: windowHeight / 2}, + Size: land.Vector2D{X: playerSize, Y: playerSize}, + }) running := true for running { @@ -51,26 +52,12 @@ func main() { } } - keys := sdl.GetKeyboardState() - if keys[sdl.SCANCODE_UP] == 1 || keys[sdl.SCANCODE_W] == 1 { - player.Move(0, -1) - } - if keys[sdl.SCANCODE_DOWN] == 1 || keys[sdl.SCANCODE_S] == 1 { - player.Move(0, 1) - } - if keys[sdl.SCANCODE_LEFT] == 1 || keys[sdl.SCANCODE_A] == 1 { - player.Move(-1, 0) - } - if keys[sdl.SCANCODE_RIGHT] == 1 || keys[sdl.SCANCODE_D] == 1 { - player.Move(1, 0) - } - - player.Move(0, 0) + root.Update() renderer.SetDrawColor(0, 0, 0, 255) renderer.Clear() - player.Draw(renderer) + root.Draw(renderer) renderer.Present() }