summaryrefslogtreecommitdiff
path: root/test/src/mode/collision.rb
blob: dddfd5a532910e39eb330a3359f03af75e8b32a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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