summaryrefslogtreecommitdiff
path: root/test/src/mode
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 /test/src/mode
parentf5534bad1f8430b769f21fa727ff29ebc539194c (diff)
test Collision and alignment detection
Diffstat (limited to 'test/src/mode')
-rw-r--r--test/src/mode/collision.rb87
1 files changed, 87 insertions, 0 deletions
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