Class: Termworld::Terminal::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/termworld/terminal/controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Controller

Returns a new instance of Controller.



9
10
11
# File 'lib/termworld/terminal/controller.rb', line 9

def initialize(user)
  @user = user
end

Instance Method Details

#runObject



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
# File 'lib/termworld/terminal/controller.rb', line 13

def run
  field = TermCanvas::Canvas.new(x: 0, y: 0, w: TermCanvas.width, h: TermCanvas.height)

  map = Resources::Maps::Town.new
  loop do
    @user.bind_local_by_name
    users = Models::User.all_local.reject { |user| user.name == @user.name }

    break if handle_keys == :break
    field.clear

    height = field.height
    width = field.width / 2 - 1
    height.times do |y|
      width.times do |x|
        abs_position = {
          x: @user.positionx - (width / 2) + x,
          y: @user.positiony - (height / 2) + y,
        }
        user = users.find { |u|
          u.positionx == abs_position[:x] && u.positiony == abs_position[:y]
        }
        if user
          player_chip = Resources::Chip.new(x: x * 2 + 1, y: y, key: "pl")
          field.rect(player_chip.rect)
          next
        end
        next if abs_position.any? { |_, v| v < 0 }
        next if (chip = map.get_chip(abs_position)).nil?
        field.rect(chip.rect.position_override(x: x * 2 + 1, y: y))
      end
    end

    player_chip = Resources::Chip.new(x: field.centerx, y: field.centery, key: "pl")
    field.rect(player_chip.rect)

    field.update
    sleep 0.05
  end
  TermCanvas.close
end