From 5a7e710a1a1fd92ff8d8c5c8f310df090a9f9cd6 Mon Sep 17 00:00:00 2001 From: falsycat Date: Wed, 9 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Implemented clamping an actor's position. --- sjplayer/src/sjplayer/Actor.d | 11 +++++++++++ sjplayer/src/sjplayer/ActorController.d | 13 ++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sjplayer/src/sjplayer/Actor.d b/sjplayer/src/sjplayer/Actor.d index 299e023..74ab72f 100644 --- a/sjplayer/src/sjplayer/Actor.d +++ b/sjplayer/src/sjplayer/Actor.d @@ -12,6 +12,9 @@ class Actor { speed = vec2(0, 0); color = vec4(0, 0, 0, 0); + clip_lefttop = vec2(0, 0); + clip_rightbottom = vec2(0, 0); + program_ = program; } @@ -20,10 +23,18 @@ class Actor { program_.Draw(pos, speed, color); } + /// vec2 pos; + /// vec2 speed; + /// vec4 color; + /// + vec2 clip_lefttop; + /// + vec2 clip_rightbottom; + private: ActorProgram program_; } diff --git a/sjplayer/src/sjplayer/ActorController.d b/sjplayer/src/sjplayer/ActorController.d index 427076a..cab3afe 100644 --- a/sjplayer/src/sjplayer/ActorController.d +++ b/sjplayer/src/sjplayer/ActorController.d @@ -50,7 +50,13 @@ class ActorController : ActorScheduledController, ActorControllerInterface { } actor_.pos += actor_.speed; - // TODO: clamping the actor position + + const left_edge = -1 + actor_.clip_lefttop.x; + const right_edge = 1 - actor_.clip_rightbottom.x; + const top_edge = 1 - actor_.clip_lefttop.y; + const bottom_edge = -1 + actor_.clip_rightbottom.y; + actor_.pos.x = actor_.pos.x.clamp(left_edge, right_edge); + actor_.pos.y = actor_.pos.y.clamp(bottom_edge, top_edge); actor_.speed *= 1-SpeedAttenuation; } @@ -72,6 +78,11 @@ private alias ActorScheduledController = ScheduledController!( "color_g": "color.g", "color_b": "color.b", "color_a": "color.a", + + "clip_left": "clip_lefttop.x", + "clip_top": "clip_lefttop.y", + "clip_right": "clip_rightbottom.x", + "clip_bottom": "clip_rightbottom.y", ] );