Skip to content

Commit

Permalink
Reworked custom input events.
Browse files Browse the repository at this point in the history
  • Loading branch information
avahe-kellenberger committed Dec 24, 2023
1 parent 9d48059 commit b5ba0a8
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 233 deletions.
7 changes: 2 additions & 5 deletions examples/basic/animationplayer_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ 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()
)
Input.onKeyPressed(K_ESCAPE):
Game.stop()

Game.start()

5 changes: 2 additions & 3 deletions examples/particles/particles_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ emitter.setLocation(screenCenter)

layer.addChild(emitter)

Input.onKeyEvent:
if key == K_ESCAPE:
Game.stop()
Input.onKeyEvent(K_ESCAPE):
Game.stop()

Game.start()

36 changes: 17 additions & 19 deletions examples/physics/physics_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,29 @@ proc createRandomCollisionShape(mouseButton: int): CollisionShape =
result = newCollisionShape(aabb(-halfWidth, -halfHeight, halfHeight, halfWidth))

proc addRandomBodyToLayer(mouseButton: int, state: ButtonState, x, y, clicks: int) =
var shape = createRandomCollisionShape(mouseButton)
let body = newPhysicsShape(shape, getRandomColor())
if state.justPressed:
var shape = createRandomCollisionShape(mouseButton)
let body = newPhysicsShape(shape, getRandomColor())

body.setLocation(vector(x, y))
body.setLocation(vector(x, y))

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

body.buildCollisionListener:
if this.collisionShape.kind == CollisionShapeKind.CIRCLE and
other.collisionShape.kind == CollisionShapeKind.CIRCLE:
echo "Circle collisions!"
return true
body.buildCollisionListener:
if this.collisionShape.kind == CollisionShapeKind.CIRCLE and
other.collisionShape.kind == CollisionShapeKind.CIRCLE:
echo "Circle collisions!"
return true

layer.addChild(body)
layer.addChild(body)

# Add random shapes on click.
Input.addMousePressedListener(addRandomBodyToLayer)
Input.addKeyPressedListener(
K_ESCAPE,
proc(key: Keycode, state: KeyState) =
Game.stop()
)
Input.addMouseButtonListener(addRandomBodyToLayer)
Input.onKeyPressed(K_ESCAPE):
Game.stop()

Game.start()

25 changes: 9 additions & 16 deletions examples/platformer/platformer_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ const
jumpForce = -350.0

proc physicsProcess(this: Node, deltaTime: float) =
if Input.wasKeyJustPressed(K_ESCAPE):
Game.stop()
return

let
leftStickX = Input.leftStick.x
leftPressed = Input.isKeyPressed(K_LEFT) or leftStickX < 0
Expand Down Expand Up @@ -149,17 +145,13 @@ player.onUpdate = physicsProcess

# Toggle transparency upon pressing "t"
var isTransparent = false
Input.addKeyPressedListener(
K_t,
proc(key: Keycode, state: KeyState) =
if state.justPressed:
if isTransparent:
player.sprite.alpha = 1.0
else:
player.sprite.alpha = 0.5
Input.onKeyPressed(K_t):
if isTransparent:
player.sprite.alpha = 1.0
else:
player.sprite.alpha = 0.5

isTransparent = not isTransparent
)
isTransparent = not isTransparent

when not defined(debug):
# Play some music
Expand All @@ -174,8 +166,9 @@ Input.registerCustomAction("jump")
Input.addCustomActionTrigger("jump", MouseButton.LEFT)
Input.addCustomActionTrigger("jump", ControllerButton.A)
Input.addCustomActionTrigger("jump", K_SPACE)
Input.addCustomActionTrigger("jump", ControllerStick.RIGHT, Direction.UP)
Input.addCustomActionTrigger("jump", ControllerTrigger.RIGHT)

Input.onKeyPressed(K_ESCAPE):
Game.stop()

Game.start()

7 changes: 2 additions & 5 deletions examples/shaders/postprocessing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ let background = newSpriteEntity(backgroundImageSprite)
background.setLocation(width * 0.5, height * 0.5)
layer.addChild(background)

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

Game.postProcessingShader = shaderProgram

Expand Down
7 changes: 2 additions & 5 deletions examples/shaders/water_shader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ let bg = Background(shader: shaderProgram)
initEntity(Entity bg, RENDER)
layer.addChild(bg)

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

Game.start()

7 changes: 2 additions & 5 deletions examples/textbox/textbox_example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ 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()
)
Input.onKeyPressed(K_ESCAPE):
Game.stop()

Game.start()

6 changes: 6 additions & 0 deletions shade.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ task platformer, "Runs the platformer example":
task platformerd, "Runs the platformer example in debug mode":
exec "nim r -d:debug -d:collisionoutlines examples/platformer/platformer_example.nim"

task particles, "Runs the particles example":
exec "nim r -d:release examples/particles/particles_example.nim"

task particlesd, "Runs the particles example in debug mode":
exec "nim r -d:debug examples/particles/particles_example.nim"

task textbox, "Runs the textbox example":
exec "nim r -d:release examples/textbox/textbox_example.nim"

Expand Down
Loading

0 comments on commit b5ba0a8

Please sign in to comment.