Skip to content

Commit

Permalink
Splitting Node into Entity/Node. (#73)
Browse files Browse the repository at this point in the history
* Splitting Node into Entity/Node.
  • Loading branch information
avahe-kellenberger authored Sep 27, 2023
1 parent aac2d48 commit bef84d0
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 146 deletions.
8 changes: 7 additions & 1 deletion examples/basic/animationplayer_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Game.scene.addLayer(layer)

let (_, image) = Images.loadImage("./examples/assets/images/king.png", FILTER_NEAREST)

let king = newSpriteNode(newSprite(image, 11, 8))
let king = newSpriteEntity(newSprite(image, 11, 8))

# Set up the run animation
const
Expand Down Expand Up @@ -54,5 +54,11 @@ king.onUpdate = proc(this: Node, deltaTime: float) =
king.setLocation(vector(width / 2, height / 2))
layer.addChild king

Input.addKeyPressedListener(
K_ESCAPE,
proc(key: Keycode, state: KeyState) =
Game.stop()
)

Game.start()

2 changes: 1 addition & 1 deletion examples/physics/physics_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ proc addRandomBodyToLayer(mouseButton: int, state: ButtonState, x, y, clicks: in

body.onUpdate = proc(this: Node, deltaTime: float) =
# Remove the body if off screen
if this.y > height + 100:
if Entity(this).y > height + 100:
layer.removeChild(this)

body.buildCollisionListener:
Expand Down
2 changes: 1 addition & 1 deletion examples/shaders/postprocessing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let
backgroundImageSprite = newSprite(image)

backgroundImageSprite.scale = vector(4, 4)
let background = newSpriteNode(backgroundImageSprite)
let background = newSpriteEntity(backgroundImageSprite)
background.setLocation(width * 0.5, height * 0.5)
layer.addChild(background)

Expand Down
4 changes: 2 additions & 2 deletions examples/shaders/rectangle_shader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let shaderProgram = newShader(vertShaderPath, fragShaderPath)

type Background = ref object of Node

Background.renderAsNodeChild:
Background.renderAsEntityChild:
ctx.rectangleFilled(
0,
0,
Expand All @@ -29,7 +29,7 @@ Background.renderAsNodeChild:
)

let bg = Background(shader: shaderProgram)
initNode(Node bg, {LayerObjectFlags.Render})
initNode(Node bg, RENDER)
layer.addChild(bg)

Input.addKeyPressedListener(
Expand Down
6 changes: 3 additions & 3 deletions examples/shaders/water_shader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const

let shaderProgram = newShader(vertShaderPath, fragShaderPath)

type Background = ref object of Node
type Background = ref object of Entity

Background.renderAsNodeChild:
Background.renderAsEntityChild:
ctx.rectangleFilled(
0,
0,
Expand All @@ -29,7 +29,7 @@ Background.renderAsNodeChild:
)

let bg = Background(shader: shaderProgram)
initNode(Node bg, RENDER)
initEntity(Entity bg, RENDER)
layer.addChild(bg)

Input.addKeyPressedListener(
Expand Down
6 changes: 6 additions & 0 deletions examples/textbox/textbox_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ let textBox = newTextBox(kennyPixel, "Hello, world!", RED)
textBox.setLocation(400, 300)
layer.addChild(textBox)

Input.addKeyPressedListener(
K_ESCAPE,
proc(key: Keycode, state: KeyState) =
Game.stop()
)

Game.start()

2 changes: 1 addition & 1 deletion shade.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namedBin["src/shadepkg/buildtool"] = "src/shade"

# Dependencies

requires "nim >= 1.6.12"
requires "nim >= 2.0.0"
requires "zippy == 0.9.7"
requires "https://github.com/avahe-kellenberger/sdl2_nim#head"
requires "safeseq >= 0.1.0"
Expand Down
4 changes: 2 additions & 2 deletions src/shade.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ else:
game/ui/textbox,
game/spritesheet,
game/sprite,
game/spritenode,
game/spriteentity,
game/scene,
game/layer,
game/physicslayer,
Expand Down Expand Up @@ -52,7 +52,7 @@ else:
gamestate,
spritesheet,
sprite,
spritenode,
spriteentity,
scene,
layer,
physicslayer,
Expand Down
38 changes: 19 additions & 19 deletions src/shadepkg/game/camera.nim
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import
node,
entity,
../math/[aabb, vector2, mathutils],
../game/gamestate

export aabb, vector2, mathutils
export entity, aabb, vector2, mathutils

type
Camera* = ref object of Node
Camera* = ref object of Entity
z*: float
bounds: AABB
viewport*: AABB

# For node tracking
# For entity tracking
offset*: Vector
trackedNode*: Node
trackedEntity*: Entity
completionRatioPerFrame*: CompletionRatio
easingFunction*: EasingFunction[Vector]

proc updateViewport*(this: Camera)

proc initCamera*(camera: Camera) =
initNode(Node(camera), UPDATE)
initEntity(Entity(camera), UPDATE)
camera.bounds = AABB_ZERO
camera.viewport = AABB_ZERO
camera.offset = VECTOR_ZERO
camera.trackedNode = nil
camera.trackedEntity = nil
camera.completionRatioPerFrame = 1.0
camera.easingFunction = lerp
camera.updateViewport()
Expand All @@ -34,19 +34,19 @@ proc newCamera*(): Camera =
initCamera(result)

proc newCamera*(
trackedNode: Node,
trackedEntity: Entity,
completionRatioPerFrame: CompletionRatio,
easingFunction: EasingFunction[Vector] = lerp
): Camera =
## Creates a camera which follows a node.
## Creates a camera which follows a entity.
## completionRatioPerFrame: The distance * CompletionRatio to travel each frame.
## If set to 1, it would track the entity perfectly.
## Set to 0, the camera will not move.
## Typically, lower values are desired (0.1 - 0.3).
## easingFunction: Determines how the camera follows the tracked node.
## easingFunction: Determines how the camera follows the tracked entity.
result = Camera()
initCamera(result)
result.trackedNode = trackedNode
result.trackedEntity = trackedEntity
result.completionRatioPerFrame = completionRatioPerFrame
result.easingFunction = easingFunction

Expand All @@ -72,8 +72,8 @@ proc updateViewport*(this: Camera) =
proc setTrackingEasingFunction*(this: Camera, easingFunction: EasingFunction[Vector]) =
this.easingFunction = easingFunction

proc setTrackedNode*(this: Camera, n: Node) =
this.trackedNode = n
proc setTrackedEntity*(this: Camera, n: Entity) =
this.trackedEntity = n

proc bounds*(this: Camera): AABB =
if this.bounds == AABB_ZERO:
Expand Down Expand Up @@ -112,23 +112,23 @@ template screenToWorldCoord*(this: Camera, x, y: float|int, relativeZ: float = 1
this.screenToWorldCoord(vector(x, y), relativeZ)

method setLocation*(this: Camera, x, y: float) =
procCall Node(this).setLocation(x, y)
procCall Entity(this).setLocation(x, y)
this.updateViewport()

method update*(this: Camera, deltaTime: float) =
procCall Node(this).update(deltaTime)
procCall Entity(this).update(deltaTime)

if this.trackedNode == nil:
# Don't need to track a node
if this.trackedEntity == nil:
# Don't need to track a entity
return

if this.easingFunction == nil:
this.setLocation(this.trackedNode.getLocation())
this.setLocation(this.trackedEntity.getLocation())
else:
this.setLocation(
this.easingFunction(
this.getLocation(),
this.trackedNode.getLocation() + this.offset,
this.trackedEntity.getLocation() + this.offset,
this.completionRatioPerFrame
)
)
Expand Down
69 changes: 69 additions & 0 deletions src/shadepkg/game/entity.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import
hashes,
sequtils

import
node,
gamestate,
../render/render,
../render/shader,
../math/vector2

export
node,
hashes,
sequtils,
render

type
Entity* = ref object of Node
shader*: Shader
location: Vector
# Rotation in degrees (clockwise).
rotation*: float

method setLocation*(this: Entity, x, y: float) {.base.}
method hash*(this: Entity): Hash {.base.}

proc initEntity*(entity: Entity, flags: NodeFlags = UPDATE_AND_RENDER) =
entity.flags = flags

proc newEntity*(flags: NodeFlags = UPDATE_AND_RENDER): Entity =
result = Entity()
initEntity(result, flags)

method getLocation*(this: Entity): Vector {.base.} =
this.location

method x*(this: Entity): float {.base.} =
this.location.x

method y*(this: Entity): float {.base.} =
this.location.y

method `x=`*(this: Entity, x: float) {.base.} =
this.setLocation(x, this.y)

method `y=`*(this: Entity, y: float) {.base.} =
this.setLocation(this.x, y)

method setLocation*(this: Entity, x, y: float) {.base.} =
this.location.x = x
this.location.y = y

method setLocation*(this: Entity, v: Vector) {.base.} =
this.setLocation(v.x, v.y)

method move*(this: Entity, dx, dy: float) {.base.} =
this.setLocation(this.x + dx, this.y + dy)

method move*(this: Entity, v: Vector) {.base.} =
this.setLocation(this.x + v.x, this.y + v.y)

method hash*(this: Entity): Hash {.base.} =
return hash(this[].unsafeAddr)

Entity.renderAsChildOf(Node):
if this.shader != nil:
this.shader.render(gamestate.runTime, gamestate.resolution)

5 changes: 3 additions & 2 deletions src/shadepkg/game/layer.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import node
import ../render/render
import std/algorithm

export node
Expand Down Expand Up @@ -40,7 +41,7 @@ proc sortChildren*(this: Layer, cmp: proc (x, y: Node): int) =
this.children.sort(cmp)

method addChild*(this: Layer, child: Node) {.base.} =
## Adds the child to this Node.
## Adds the child to this Layer.
this.children.add(child)

method removeChild*(this: Layer, child: Node) {.base.} =
Expand Down Expand Up @@ -78,7 +79,7 @@ method update*(this: Layer, deltaTime: float) {.base.} =

for child in this.children:
if child.shouldUpdate:
child.update(deltaTime)
update(child, deltaTime)

for i in countdown(this.children.len() - 1, 0):
if this.children[i].isDead:
Expand Down
Loading

0 comments on commit bef84d0

Please sign in to comment.