Work towards deletion system

This commit is contained in:
Layla 2022-07-17 22:12:46 -04:00
parent 994f9a06c8
commit 987ef9502f
3 changed files with 47 additions and 4 deletions

View File

@ -21,19 +21,34 @@ func Run(root *node.Node, renderer rendering.Renderer2D, inputHandler input.Inpu
return nil
})
speed := 0.01
rotation_speed := 0.01
speed := 0.02
g.OnUpdate(func() error {
if inputHandler.IsKeyJustPressed(sdl.SCANCODE_SPACE) {
speed = -speed
if inputHandler.IsKeyJustReleased(sdl.SCANCODE_SPACE) {
rotation_speed = -rotation_speed
}
g.Rotation += speed
if inputHandler.IsKeyDown(sdl.SCANCODE_D) {
g.Position.X += speed
}
if inputHandler.IsKeyDown(sdl.SCANCODE_A) {
g.Position.X -= speed
}
if inputHandler.IsKeyDown(sdl.SCANCODE_S) {
g.Position.Y += speed
}
if inputHandler.IsKeyDown(sdl.SCANCODE_W) {
g.Position.Y -= speed
}
g.Rotation += rotation_speed
return nil
})
controller := node.NewNode()
controller.Name = "Controller"
controller.SetProcessMode(node.ActiveProcessMode)
defer root.AddChild(&controller)
controller.OnUpdate(func() error {
@ -45,6 +60,12 @@ func Run(root *node.Node, renderer rendering.Renderer2D, inputHandler input.Inpu
}
}
if inputHandler.IsKeyJustPressed(sdl.SCANCODE_P) {
if err := g.Free(); err != nil {
return err
}
}
return nil
})

View File

@ -15,4 +15,5 @@ type INode interface {
IsProcessing() bool
GetProcessMode() ProcessMode
SetProcessMode(ProcessMode)
Free() error
}

View File

@ -1,6 +1,7 @@
package node
import (
"fmt"
"log"
"github.com/manleydev/golang-game-framework/rendering"
@ -112,6 +113,26 @@ func (node *Node) GetName() string {
return node.Name
}
func (node *Node) Free() error {
var parent *Node = node.parent.(*Node)
if err := parent.removeChild(node); err != nil {
return err
}
node.parent = nil
return nil
}
func (node *Node) removeChild(target *Node) error {
for i, child := range node.children {
if child == target {
node.children = append(node.children[:i], node.children[i+1:]...)
return nil
}
}
return fmt.Errorf("Could not remove child '%s', child not found on parent!", target.Name)
}
func (node *Node) GetChild(index int) INode {
if index < len(node.children) {
return nil