summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2022-05-26 11:26:28 -0300
committerFrederico Linhares <fred@linhares.blue>2022-05-26 11:26:28 -0300
commit5baa7560704117c5528c9903c218df73793c8a33 (patch)
treec774968a0207dae342e13a4d8571bd99bb06f7b7
parentf5534bad1f8430b769f21fa727ff29ebc539194c (diff)
test Collision and alignment detection
-rw-r--r--test/Rakefile2
-rw-r--r--test/src/main.rb10
-rw-r--r--test/src/mode/collision.rb87
3 files changed, 92 insertions, 7 deletions
diff --git a/test/Rakefile b/test/Rakefile
index 586f646..c449fad 100644
--- a/test/Rakefile
+++ b/test/Rakefile
@@ -14,7 +14,7 @@
OBJ = 'test'
-RB_FILES = FileList['src/*.rb']
+RB_FILES = FileList['src/**/*.rb']
task :build do
rb_files = RB_FILES.inject('') {_1 + "#{_2} "}
diff --git a/test/src/main.rb b/test/src/main.rb
index d5254db..9e37faa 100644
--- a/test/src/main.rb
+++ b/test/src/main.rb
@@ -13,15 +13,13 @@
# limitations under the License.
def init()
+ $mode = Mode::Collision.new();
end
-def key_down(key)
-end
+def key_down(key) = $mode.controller.key_down(key);
-def key_up(key)
-end
+def key_up(key) = $mode.controller.key_up(key);
def quit() = CandyGear.quit();
-def tick()
-end
+def tick() = $mode.tick();
diff --git a/test/src/mode/collision.rb b/test/src/mode/collision.rb
new file mode 100644
index 0000000..dddfd5a
--- /dev/null
+++ b/test/src/mode/collision.rb
@@ -0,0 +1,87 @@
+# Copyright 2022 Frederico de Oliveira Linhares
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module Mode
+ class Collision
+ WINDOW_WIDTH = 640;
+ WINDOW_HEIGHT = 360;
+
+ class Square
+ attr_reader(:rect);
+
+ def initialize(x, y, vertical_direction, horizontal_direction)
+ @rect = CandyGear::Rect.new(x, y, 16, 16);
+ @vertical_direction = vertical_direction;
+ @horizontal_direction = horizontal_direction;
+ end
+
+ def x() = @rect.x;
+ def y() = @rect.y;
+
+ def draw() = @rect.draw_fill();
+
+ def tick()
+ case @vertical_direction
+ when :left
+ @rect.x -= 2;
+ @vertical_direction = :right if @rect.x < 0;
+ when :right
+ @rect.x += 2;
+ if @rect.x > WINDOW_WIDTH - @rect.width() then
+ @vertical_direction = :left;
+ end
+ end
+
+ case @horizontal_direction
+ when :up
+ @rect.y -= 2;
+ @horizontal_direction = :down if @rect.y < 0;
+ when :down
+ @rect.y += 2;
+ if @rect.y > WINDOW_HEIGHT - @rect.height() then
+ @horizontal_direction = :up;
+ end
+ end
+ end
+ end
+
+ def initialize()
+ @normal_color = CandyGear::Color.new(0x99, 0x99, 0x99);
+ @collision_color = CandyGear::Color.new(0x99, 0x33, 0x33);
+ @vertical_align_color = CandyGear::Color.new(0x33, 0x99, 0x33);
+ @horizontal_align_color = CandyGear::Color.new(0x33, 0x33, 0x99);
+
+ @square1 = Square.new(213, 120, :left, :down);
+ @square2 = Square.new(427, 240, :right, :down);
+ end
+
+ def tick()
+ @square1.tick();
+ @square2.tick();
+
+ if @square1.rect.collide?(@square2.rect) then
+ CandyGear::Graphic.set_color(@collision_color);
+ elsif @square1.rect.align_horizontally?(@square2.rect) then
+ CandyGear::Graphic.set_color(@horizontal_align_color);
+ elsif @square1.rect.align_vertically?(@square2.rect) then
+ CandyGear::Graphic.set_color(@vertical_align_color);
+ else
+ CandyGear::Graphic.set_color(@normal_color);
+ end
+
+ @square1.draw();
+ @square2.draw();
+ end
+ end
+end