-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkarena.rb
177 lines (159 loc) · 6.26 KB
/
tkarena.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'tk'
require 'base64'
TkRobot = Struct.new(:body, :gun, :radar, :speech, :info, :status)
class TkArena
attr_reader :battlefield, :xres, :yres
attr_accessor :speed_multiplier, :on_game_over_handlers
attr_accessor :canvas, :boom, :robots, :bullets, :explosions, :colors
attr_accessor :default_skin_prefix
def initialize battlefield, xres, yres, speed_multiplier
@battlefield = battlefield
@xres, @yres = xres, yres
@speed_multiplier = speed_multiplier
@text_colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#00ffff', '#ff00ff', '#ffffff', '#777777']
@default_skin_prefix = "images/red_"
@on_game_over_handlers = []
init_canvas
init_simulation
end
def on_game_over(&block)
@on_game_over_handlers << block
end
def read_gif name, c1, c2, c3
data = nil
open(name, 'rb') do |f|
data = f.read()
ncolors = 2**(1 + data[10][0].to_i + data[10][1].to_i * 2 + data[10][2].to_i * 4)
ncolors.times do |j|
data[13 + j * 3 + 0], data[13 + j * 3 + 1], data[13 + j * 3 + 2] =
data[13 + j * 3 + c1], data[13 + j * 3 + c2], data[13 + j * 3 + c3]
end
end
TkPhotoImage.new(:data => Base64.encode64(data))
end
def usage
puts "usage: rrobots.rb <FirstRobotClassName[.rb]> <SecondRobotClassName[.rb]> <...>"
puts "\tthe names of the rb files have to match the class names of the robots"
puts "\t(up to 8 robots)"
puts "\te.g. 'ruby rrobots.rb SittingDuck NervousDuck'"
exit
end
def init_canvas
@canvas = TkCanvas.new(:height=>yres, :width=>xres, :scrollregion=>[0, 0, xres, yres], :background => '#000000').pack
@colors = []
[[0,1,1],[1,0,1],[1,1,0],[0,0,1],[1,0,0],[0,1,0],[0,0,0],[1,1,1]][[email protected]].zip(@battlefield.robots) do |color, robot|
bodies, guns, radars = [], [], []
image_path = robot.skin_prefix || @default_skin_prefix
reader = robot.skin_prefix ? lambda{|fn| TkPhotoImage.new(:file => fn) } : lambda{|fn| read_gif(fn, *color)}
36.times do |i|
bodies << reader["#{image_path}body#{(i*10).to_s.rjust(3, '0')}.gif"]
guns << reader["#{image_path}turret#{(i*10).to_s.rjust(3, '0')}.gif"]
radars << reader["#{image_path}radar#{(i*10).to_s.rjust(3, '0')}.gif"]
end
@colors << TkRobot.new(bodies << bodies[0], guns << guns[0], radars << radars[0])
end
@boom = (0..14).map do |i|
TkPhotoImage.new(:file => "images/explosion#{i.to_s.rjust(2, '0')}.gif")
end
end
def init_simulation
@robots, @bullets, @explosions = {}, {}, {}
TkTimer.new(20, -1, Proc.new{
begin
draw_frame
rescue => err
puts err.class, err, err.backtrace
raise
end
}).start
end
def draw_frame
simulate(@speed_multiplier)
draw_battlefield
end
def simulate(ticks=1)
@explosions.reject!{|e,tko| @canvas.delete(tko) if e.dead; e.dead }
@bullets.reject!{|b,tko| @canvas.delete(tko) if b.dead; b.dead }
@robots.reject! do |ai,tko|
if ai.dead
tko.status.configure(:text => "#{ai.name.ljust(20)} dead")
tko.each{|part| @canvas.delete(part) if part != tko.status}
true
end
end
ticks.times do
if @battlefield.game_over
@on_game_over_handlers.each{|h| h.call(@battlefield) }
unless @game_over
winner = @robots.keys.first
whohaswon = if winner.nil?
"Draw!"
elsif @battlefield.teams.all?{|k,t|t.size<2}
"#{winner.name} won!"
else
"Team #{winner.team} won!"
end
text_color = winner ? winner.team : 7
@game_over = TkcText.new(canvas,
:fill => @text_colors[text_color],
:anchor => 'c', :coords => [400,400], :font=>'courier 36', :justify => 'center',
:text => "GAME OVER\n#{whohaswon}")
end
end
@battlefield.tick
end
end
def draw_battlefield
draw_robots
draw_bullets
draw_explosions
end
def draw_robots
@battlefield.robots.each_with_index do |ai, i|
next if ai.dead
@robots[ai] ||= TkRobot.new(
TkcImage.new(@canvas, 0, 0),
TkcImage.new(@canvas, 0, 0),
TkcImage.new(@canvas, 0, 0),
TkcText.new(@canvas,
:fill => @text_colors[ai.team],
:anchor => 's', :justify => 'center', :coords => [ai.x / 2, ai.y / 2 - ai.size / 2]),
TkcText.new(@canvas,
:fill => @text_colors[ai.team],
:anchor => 'n', :justify => 'center', :coords => [ai.x / 2, ai.y / 2 + ai.size / 2]),
TkcText.new(@canvas,
:fill => @text_colors[ai.team],
:anchor => 'nw', :coords => [10, 15 * i + 10], :font => TkFont.new("courier 9")))
@robots[ai].body.configure( :image => @colors[ai.team].body[(ai.heading+5) / 10],
:coords => [ai.x / 2, ai.y / 2])
@robots[ai].gun.configure( :image => @colors[ai.team].gun[(ai.gun_heading+5) / 10],
:coords => [ai.x / 2, ai.y / 2])
@robots[ai].radar.configure(:image => @colors[ai.team].radar[(ai.radar_heading+5) / 10],
:coords => [ai.x / 2, ai.y / 2])
@robots[ai].speech.configure(:text => "#{ai.speech}",
:coords => [ai.x / 2, ai.y / 2 - ai.size / 2])
@robots[ai].info.configure(:text => "#{ai.name}\n#{'|' * (ai.energy / 5)}",
:coords => [ai.x / 2, ai.y / 2 + ai.size / 2])
@robots[ai].status.configure(:text => "#{ai.name.ljust(20)} #{'%.1f' % ai.energy}")
end
end
def draw_bullets
@battlefield.bullets.each do |bullet|
@bullets[bullet] ||= TkcOval.new(
@canvas, [-2, -2], [3, 3],
:fill=>'#'+("%02x" % (128+bullet.energy*14).to_i)*3)
@bullets[bullet].coords(
bullet.x / 2 - 2, bullet.y / 2 - 2,
bullet.x / 2 + 3, bullet.y / 2 + 3)
end
end
def draw_explosions
@battlefield.explosions.each do |explosion|
@explosions[explosion] ||= TkcImage.new(@canvas, explosion.x / 2, explosion.y / 2)
@explosions[explosion].image(boom[explosion.t])
end
end
def run
Tk.mainloop
end
end