# 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