forked from JEG2/broadsides
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rb
97 lines (88 loc) · 2.36 KB
/
mod.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
# mod.rb
module Mod
POS = [40, 360]
RPOS = POS.reverse
def read_game_log
file = ask_open_file
file ||= '../log/last_game.log'
IO.readlines(file).each do |line|
key, *log = line.split
case key
when /SETUP/
@players = log.first.split(':').last.split(',')
when /SHIPS/
@ships ||= []; log.shift; @ships << log
when /SHOTS/
@shots ||= []; log.shift; @shots << log
when /WINNER/
@winner = log.first
else
error 'invarid game log...'
end
end
end
def start_battle
POS.each_with_index do |x, i|
caption @players[i], :left => x, :top => 10, :stroke => forestgreen, :weight => 'bold'
end
show_ships
make_bombs
show_battle
end
def show_ships
@ships.each_with_index do |ships, i|
ships.each do |ship|
len, pos, hv = ship.split ':'
hv == 'H' ? (x, y = len.to_i, 1) : (x, y = 1, len.to_i)
l, t = convert pos
image 'ship.gif', :width => 30*x, :height => 30*y, :left => POS[i]+30*l, :top => 50+30*t
end
end
@bgs[1].style :fill => rand_color
end
def make_bombs
@bombs = []
@shots.each_with_index do |shots, i|
shots.each do |shot|
pos, hm = shot.split ':'
l, t = convert pos
img = hm == 'hit' ? 'fire.gif' : 'bomb.gif'
@bombs << image(img, :width => 30, :height => 30, :left => RPOS[i%2]+30*l, :top => 50+30*t).hide
end
@bombs << 2
end
@times = @bombs.length - 1
end
def show_battle
e = every do |i|
@bombs[i] == 2 ? change_focus : @bombs[i].show
if i == @times
e.stop
show_winner
end
end
end
def show_winner
@players.each do |player|
stack :width => 500, :height => 340, :left => 100, :top => 30 do
background rgb(240, 230, 140, 0.7), :curve => 30
subtitle "\n WINNER:", :stroke => green
para
title @winner, :align => 'center', :stroke => deeppink, :weight => 'bold'
end
end
end
def change_focus
@bgs.each { |bg|
bg.style[:fill] == white ? (bg.style :fill => rand_color) : (bg.style :fill => white)
}
sleep 2
end
def convert pos
l, t = pos[0, 1], pos[1..-1]
[('A'..'J').to_a.index(l), t.to_i - 1]
end
def rand_color
rgb(rand(255), rand(255), rand(255), 0.5)..rgb(rand(255), rand(255), rand(255), 0.5)
end
end