-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface.rb
195 lines (146 loc) · 4.5 KB
/
interface.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
193
194
require 'rubygems'
require 'stateology'
# basic interace for Actors
module InterfaceElementActor
@@last_clicked = nil
def left_mouse_click
message info, :log_level => 1
state :Inactive
if last_clicked && last_clicked != self then
last_clicked.unclicked
end
@@last_clicked = self
end
def left_mouse_held(mx, my)
warp(mx, my)
end
def left_mouse_released
state nil
end
def unclicked
end
def self.clear_last_clicked
@@last_clicked.unclicked if @@last_clicked
@@last_clicked = nil
end
def last_clicked=(lc)
@@last_clicked = lc
end
def last_clicked
@@last_clicked
end
def last_clicked?(lc)
@@last_clicked == lc
end
end
# interface for Actors that are Vehicles
module InterfaceElementVehicle
include InterfaceElementActor
def left_mouse_click
if last_clicked.kind_of?(Andy) && actor_collision_with?(last_clicked) then
add_driver(last_clicked)
end
if last_clicked == self then
@_saved_coords = [@x.to_i, @y.to_i]
end
super
end
def left_mouse_released
if @_saved_coords &&
# eject if location after left_mouse_release is within a radius of pixels of prior loc
Math::hypot(@_saved_coords[0] - @x.to_i, @_saved_coords[1] - @y.to_i) < 50 then
remove_driver
@_saved_coords = nil
end
state nil
end
end
# interface for Actors that can be controlled by keyboard
module InterfaceElementControllable
include InterfaceElementActor
def unclicked
unregister_animation(:arrow)
state nil
end
def left_mouse_released
state :Controllable
create_arrow
end
def do_controls(*args, &block)
#this should be left empty
#as we want no behaviour outside
#of the Controllable state
end
def create_arrow
hover = 2 * Math::PI * rand
dy = 0
y_float = lambda do
hover = hover + 0.1 % (2 * Math::PI)
dy = 10 * Math::sin(hover)
method(:y).call + dy
end
new_anim = register_animation(:arrow, :x => method(:x), :y => y_float, :x_offset => 0, :y_offset => -80,
:zorder => Common::ZOrder::Interface)
new_anim.make_animation(:standard, new_anim.load_image("assets/arrow.png"))
new_anim.load_animation(:standard)
end
alias_method :button_down, :do_controls
end
#################### End InterfaceElementControllable ##################
# interface for Andys
# interface for Actors that are Vehicles
module InterfaceElementAndy
include InterfaceElementControllable
def left_mouse_click
if last_clicked == self && !(vehicles = current_collisions.select { |v| v.is_a?(VehicleActor)}).empty? then
closest_vehicle = vehicles.inject { |m, c| dist(self, m) < dist(self, c) ? m : c }
closest_vehicle.add_driver(self)
end
if last_clicked == self then
@_saved_coords = [@x.to_i, @y.to_i]
end
# from InterfaceElementActor (NEED to refactor so uses composition instead)
message info, :log_level => 1
state :Inactive
if last_clicked && last_clicked != self then
last_clicked.unclicked
end
@@last_clicked = self
end
end
# interface for Actors that are both Vehicles and Controllable
module InterfaceElementControllableVehicle
include InterfaceElementVehicle
include InterfaceElementControllable
def left_mouse_released
if @_saved_coords &&
# eject if location after left_mouse_release is within a radius of pixels of prior loc
Math::hypot(@_saved_coords[0] - @x.to_i, @_saved_coords[1] - @y.to_i) < 50 then
remove_driver
@_saved_coords = nil
end
state nil
if has_driver? then
state :Controllable
create_arrow
else
state nil
end
end
end
# interface for Tiles
module InterfaceElementTile
def left_mouse_click
message info, :log_level => 1
end
def left_mouse_held(mx, my)
xp = mx - self.x
yp = my - self.y
@image.paint {
color :random
circle xp, yp, 10
}
end
def left_mouse_released
end
end