✨ Object-based land engine
This commit is contained in:
parent
4b7358a1d0
commit
cd01069c89
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/dist
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -1,7 +1,7 @@
|
|||||||
package entities
|
package entities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.solsynth.dev/highland/codingland/pkg/internal/common"
|
"git.solsynth.dev/highland/codingland/pkg/internal/land"
|
||||||
"github.com/veandco/go-sdl2/sdl"
|
"github.com/veandco/go-sdl2/sdl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,9 +11,30 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
Position common.Vector2D
|
land.BaseObject
|
||||||
Size common.Vector2D
|
|
||||||
Velocity common.Vector2D
|
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) {
|
func (p *Player) Move(deltaX, deltaY float64) {
|
||||||
|
29
pkg/internal/land/object.go
Normal file
29
pkg/internal/land/object.go
Normal file
@ -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)
|
||||||
|
}
|
17
pkg/internal/land/root.go
Normal file
17
pkg/internal/land/root.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package common
|
package land
|
||||||
|
|
||||||
type Vector2D struct {
|
type Vector2D struct {
|
||||||
X, Y float64
|
X, Y float64
|
29
pkg/main.go
29
pkg/main.go
@ -1,8 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.solsynth.dev/highland/codingland/pkg/internal/common"
|
|
||||||
"git.solsynth.dev/highland/codingland/pkg/internal/entities"
|
"git.solsynth.dev/highland/codingland/pkg/internal/entities"
|
||||||
|
"git.solsynth.dev/highland/codingland/pkg/internal/land"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
@ -37,10 +37,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer renderer.Destroy()
|
defer renderer.Destroy()
|
||||||
|
|
||||||
player := &entities.Player{
|
root := &land.RootObject{}
|
||||||
Position: common.Vector2D{X: windowWidth / 2, Y: windowHeight / 2},
|
root.AddChild(&entities.Player{
|
||||||
Size: common.Vector2D{X: playerSize, Y: playerSize},
|
Position: land.Vector2D{X: windowWidth / 2, Y: windowHeight / 2},
|
||||||
}
|
Size: land.Vector2D{X: playerSize, Y: playerSize},
|
||||||
|
})
|
||||||
|
|
||||||
running := true
|
running := true
|
||||||
for running {
|
for running {
|
||||||
@ -51,26 +52,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := sdl.GetKeyboardState()
|
root.Update()
|
||||||
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)
|
|
||||||
|
|
||||||
renderer.SetDrawColor(0, 0, 0, 255)
|
renderer.SetDrawColor(0, 0, 0, 255)
|
||||||
renderer.Clear()
|
renderer.Clear()
|
||||||
|
|
||||||
player.Draw(renderer)
|
root.Draw(renderer)
|
||||||
|
|
||||||
renderer.Present()
|
renderer.Present()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user