Class: SlackGame::Game::Demo

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_game/game/demo.rb,
lib/slack_game/game/demo/canvas.rb,
lib/slack_game/game/demo/controller.rb

Defined Under Namespace

Classes: Canvas, Controller, Position

Instance Method Summary collapse

Constructor Details

#initialize(channel, x = 10, y = 10) ⇒ Demo

Returns a new instance of Demo.



7
8
9
10
11
12
13
# File 'lib/slack_game/game/demo.rb', line 7

def initialize(channel, x = 10, y = 10)
  @controller = Controller.new
  @canvas = Canvas.new(channel, x, y)
  @position = Position.new(0, 0, 0..9, 0..9)
  @canvas.matrix[@position.y][@position.x] = Canvas::CHARACTOR
  @canvas.draw
end

Instance Method Details

#main_loopObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/slack_game/game/demo.rb', line 15

def main_loop
  loop{
    begin
      update(@controller.take)
    rescue => e
      puts "GAME OVER #{e.message}"
      break
    end
  }
end

#set_position(x, y) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/slack_game/game/demo.rb', line 39

def set_position(x, y)
  @position.x = x
  @position.y = y
  matrix = @canvas.matrix
  matrix = matrix.map { |n| n.map { |d| d == Canvas::CHARACTOR ? Canvas::PAINTED : d } }
  matrix[@position.y][@position.x] = Canvas::CHARACTOR
  @canvas.matrix = matrix
  @canvas.draw
end

#update(cmd) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/slack_game/game/demo.rb', line 26

def update(cmd)
  case cmd
  when Controller::LEFT
    set_position(@position.x - 1, @position.y)
  when Controller::RIGHT
    set_position(@position.x + 1, @position.y)
  when Controller::DOWN
    set_position(@position.x, @position.y + 1)
  when Controller::UP
    set_position(@position.x, @position.y - 1)
  end
end