-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWindow.rb
192 lines (182 loc) · 6.17 KB
/
GameWindow.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
gem 'gosu'
require "gosu"
require 'net/telnet'
require "./Rectangle.rb"
WINDOW_SIZE_X = 620
WINDOW_SIZE_Y = 593
OFFSET_TOP = 10
OFFSET_MIDDLE = WINDOW_SIZE_X / 2 + 25
OFFSET_BOTTOM = 25
OFFSET_LEFT = 35
OFFSET_RIGHT = 50
require "./Nard.rb"
class GameWindow < Gosu::Window
def initialize
super WINDOW_SIZE_X + 50, WINDOW_SIZE_Y + 100, false
self.caption = "Nards"
@roll = Gosu::Image.new(self, "images/roll_active.png", true, 0, 0, 200, 60)
@background_image = Gosu::Image.new(self, "images/bg.jpg", true, 0, 0, 620, 593)
@win_lose = Gosu::Image.new(self, "images/win.png", true, 0, 0, 0, 0)
@nard_side_1 = Nard.new(self, 1)
@nard_side_2 = Nard.new(self, 2)
@rect = Rectangle.new(self,Gosu::Color::RED);
@cursor = Gosu::Image.new(self, "images/cursor.png", true, 0, 0, 35, 35)
@mouse_down = false
@count_movement = []
@game_end = false
@client = Net::Telnet.new('Host'=>'localhost', 'Port'=>7000, "Prompt"=>/^\+OK/n)
@client.cmd("get_side"){ |str| @side = str.to_i}
@movement = @side == 1 ? true : false
end
def update
return if @game_end
if button_down?(Gosu::MsRight)
@nard_side_1.selected_index = -1
@nard_side_2.selected_index = -1
end
if @side == 1
if @nard_side_1.win?
@win_lose = Gosu::Image.new(self, "images/win.png", true, 0, 0, 200, 100)
@client.cmd("you_lose #{@side}")
@game_end = true
end
else
if @nard_side_2.win?
@win_lose = Gosu::Image.new(self, "images/win.png", true, 0, 0, 200, 100)
@client.cmd("you_lose #{@side}")
@game_end = true
end
end
if mouseup?
mouse_x = mouse_x()
mouse_y = mouse_y()
count_side_1 = @nard_side_1.get_count_nards_on_position(get_position_x(mouse_x), get_position_y(mouse_y))
count_side_2 = @nard_side_2.get_count_nards_on_position(get_position_x(mouse_x), get_position_y(mouse_y))
if mouse_x > OFFSET_MIDDLE - 70 && mouse_x <= OFFSET_MIDDLE || mouse_x > WINDOW_SIZE_X - OFFSET_RIGHT && mouse_x < WINDOW_SIZE_X
return
end
if @movement
if @side == 1
if !@nard_side_1.all_in_home?
if mouse_x > WINDOW_SIZE_X
return
end
end
if !@nard_side_1.selected_nard? && count_side_1 > 0
@nard_side_1.select_nard(get_position_x(mouse_x), get_position_y(mouse_y))
end
if @nard_side_1.selected_nard? && count_side_2 == 0 && @nard_side_1.can_move_to_position(get_position_x(mouse_x), get_position_y(mouse_y), @count_movement)
if @nard_side_1.move_selected_to_position(get_position_x(mouse_x), get_position_y(mouse_y))
@client.cmd("move_selected_to_position #{@side} #{get_position_x(mouse_x)} #{get_position_y(mouse_y)} #{@nard_side_1.selected_index}")
@nard_side_1.selected_index = -1
if @count_movement.empty?
sleep(0.1)
@client.cmd("your_movement #{@side}")
@movement = false
end
end
end
else
if !@nard_side_2.all_in_home?
if mouse_x > WINDOW_SIZE_X
return
end
end
if !@nard_side_2.selected_nard? && count_side_2 > 0
@nard_side_2.select_nard(get_position_x(mouse_x), get_position_y(mouse_y))
end
if @nard_side_2.selected_nard? && count_side_1 == 0 && @nard_side_2.can_move_to_position(get_position_x(mouse_x), get_position_y(mouse_y), @count_movement)
if @nard_side_2.move_selected_to_position(get_position_x(mouse_x), get_position_y(mouse_y))
@client.cmd("move_selected_to_position #{@side} #{get_position_x(mouse_x)} #{get_position_y(mouse_y)} #{@nard_side_2.selected_index}")
@nard_side_2.selected_index = -1
p @count_movement
if @count_movement.empty?
sleep(0.1)
@client.cmd("your_movement #{@side}")
@movement = false
end
end
end
end
end
else
if button_down?(Gosu::MsLeft)
@mouse_down = true
end
end
if button_down?(Gosu::MsLeft) && 230 < mouse_x() && mouse_x() < 460 && 620 < mouse_y() && mouse_y() < 680
roll
end
end
def mouseup?
if !button_down?(Gosu::MsLeft) && @mouse_down == true
@mouse_down = false
return true
end
return false
end
def draw
cmd, *arg = @client.cmd("get_message #{@side}").chomp.split
if cmd == "move_selected_to_position"
if @side == 1
@nard_side_2.move_selected_to_position(arg[0].to_i, arg[1].to_i, arg[2].to_i)
else
@nard_side_1.move_selected_to_position(arg[0].to_i, arg[1].to_i, arg[2].to_i)
end
end
if cmd == "your_movement"
@movement = true
end
if cmd == "you_lose"
@win_lose = Gosu::Image.new(self, "images/lose.png", true, 0, 0, 200, 100)
@game_end = true
end
@background_image.draw(0, 0, 0)
@nard_side_1.draw
@nard_side_2.draw
@cursor.draw(mouse_x(), mouse_y(), 9999)
@roll.draw(230, 620, 0) if @movement && !@game_end
if @bone_first && @bone_second && !@game_end
@bone_first.draw(50, 620, 0)
@bone_second.draw(120, 620, 0)
end
@win_lose.draw(400, 620, 0)
end
def get_position_x(mouse_x)
if mouse_x > OFFSET_MIDDLE - 70 && mouse_x <= OFFSET_MIDDLE || mouse_x > WINDOW_SIZE_X - OFFSET_RIGHT && mouse_x < WINDOW_SIZE_X
return 0
end
if mouse_x > WINDOW_SIZE_X
return 13
end
if mouse_x > OFFSET_MIDDLE
i = 6
x = OFFSET_MIDDLE
else
i = 0
x = OFFSET_LEFT
end
while mouse_x > x
mouse_x -= 41
i += 1
end
return i
end
def get_position_y(mouse_y)
if mouse_y >= WINDOW_SIZE_Y / 2 + OFFSET_BOTTOM
return 2
else
return 1
end
end
def roll
first = rand(6) + 1
second = rand(6) + 1
@bone_first = Gosu::Image.new(self, "images/" + first.to_s + ".png", true, 0, 0, 60, 60)
@bone_second = Gosu::Image.new(self, "images/" + second.to_s + ".png", true, 0, 0, 60, 60)
@count_movement.clear
@count_movement = [first, second]
end
end
window = GameWindow.new
window.show