-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
87 lines (76 loc) · 2.3 KB
/
app.rb
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
# frozen_string_literal: true
require 'rubygems'
require 'bundler/setup'
require 'ruby2d'
require './lib/morphogen'
require './lib/world'
Dir['./morphogens/*.rb'].each { |file| require file }
FONT = 'resources/Lato.ttf'
FONT_SIZE = 24
WIDTH = 120
HEIGHT = 90
CELL_SIZE = 8
GUTTER = 24
SIDEBAR = 240
FULL_HEIGHT = HEIGHT * CELL_SIZE
FULL_WIDTH = WIDTH * CELL_SIZE + GUTTER + SIDEBAR
STARTING = [3620, 3660, 3700, 7220, 7260, 7700].freeze
@world = World.new(width: WIDTH, height: HEIGHT)
@morphogens = Morphogen.classes.shuffle.each_with_index.each_with_object({}) do |(klass, index), hash|
char = (65 + index).chr
hash[char] = klass.new(char: char)
hash
end
@morphogens_enumerator = @morphogens.cycle
# starting positions
@morphogens.each_with_index do |(char, morphogen), index|
@world.seed_player(pos: STARTING[index], seed: morphogen.seed, char: char)
end
# text objects for player stats
@player_stats = {}
y = 0
default = { size: FONT_SIZE, font: FONT, x: FULL_WIDTH - SIDEBAR }
@morphogens.each do |char, morphogen|
@player_stats[morphogen.name] = {
name: Text.new(default.merge(y: y, text: morphogen.name, color: morphogen.color)),
score: Text.new(default.merge(y: y + FONT_SIZE, text: "score: #{@world.player_score(char)}", color: 'white'))
}
y += FONT_SIZE * 3
end
def make_moves
morphogen = @morphogens_enumerator.next[1]
moves = morphogen.moves(@world.state)
@world.set_player(player: morphogen.char, pos: moves[0])
@world.set_player(player: morphogen.char, pos: moves[1])
end
def update_scoreboard
@morphogens.each do |char, morphogen|
@player_stats[morphogen.name][:score].text = "score: #{@world.player_score(char)}"
@player_stats[morphogen.name][:score].add
@player_stats[morphogen.name][:name].add
end
end
set title: 'Biotic', width: FULL_WIDTH, height: FULL_HEIGHT
def render_world
state = @world.state
state.each_char.each_with_index do |char, pos|
next if state.dead?(pos)
x = pos % WIDTH
y = pos / WIDTH
Square.new(
x: x * CELL_SIZE, y: y * CELL_SIZE, size: CELL_SIZE, color: @morphogens[char].color
)
end
end
def redraw
clear
make_moves
render_world
update_scoreboard
@world.iterate
end
update do
# redraws every 2 seconds (ish)
redraw if (0.5 * (get :frames).to_i % (get :fps).to_i).zero?
end
show