-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_ship_editor.rb
More file actions
210 lines (175 loc) · 7.6 KB
/
scene_ship_editor.rb
File metadata and controls
210 lines (175 loc) · 7.6 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module Scenes
class ShipEditorScene < BasicScene
def initialize(window, previous_scene)
super(window)
@previous_scene = previous_scene
@filter = Gosu::Color.new(0xaa000000)
@mount_point_image = Gosu::Image.new(window, "media/small_mount_point.png", false)
@font = Gosu::Font.new(window, Gosu::default_font_name, 11)
@font_color = Gosu::Color.new(0xff00ff00)
@modules = previous_scene.modules.dup
@modules.delete_if do |mod|
!mod.data[:compatible]
end
@ships = previous_scene.ships.dup
@ships.delete_if do |ship|
#!ship.data[:compatible]
end
@selected = nil
@selected_offset_x = 0
@selected_offset_y = 0
@selected_start_x = 0
@selected_end_x = 0
@selected_rotation = 0
@mount_points = []
@modules.each do |mod|
#mod.mount_points.each do |mount_point|
# @mount_points << mount_point
#end
@mount_points += mod.mount_points
end
@ships.each do |ship|
ship.children.each do |mod|
@mount_points += mod.mount_points
end
end
@camera = @previous_scene.camera
end
def update
if @selected != nil
if button_down?(Gosu::MsLeft)
#@selected.detach if @selected.attached? # Improve on this! #FIXME
@selected.shape.body.p.x = @camera.mouse_x + @selected_offset_x
@selected.shape.body.p.y = @camera.mouse_y + @selected_offset_y
@previous_scene.space.rehash_shape(@selected.shape)
#@selected.mount_points.each do |mp1|
# @mount_points.each do |mp2|
# if Gosu.distance(mp1.space_pos.x, mp1.space_pos.y, mp2.space_pos.x, mp2.space_pos.y) < 5
# mp2.s = 3
# end
# end
#end
elsif button_down?(Gosu::MsRight)
@selected.shape.body.a = Gosu.angle(@selected.shape.body.p.x, @selected.shape.body.p.y, @camera.mouse_x + @selected_offset_x, @camera.mouse_y + @selected_offset_y).gosu_to_radians
@previous_scene.space.rehash_shape(@selected.shape)
else # whoops, must have missed the button_up
end_drag
end
end
end
def draw
@previous_scene.draw
@camera.draw do
@window.draw_quad(0, 0, @filter, SCREEN_WIDTH, 0, @filter, SCREEN_WIDTH, SCREEN_HEIGHT, @filter, 0, SCREEN_HEIGHT, @filter, render_depth+ZOrder::Background)
@modules.each do |mod|
mod.draw(render_depth)
@window.rotate(mod.shape.body.a.radians_to_degrees, mod.shape.body.p.x, mod.shape.body.p.y) do
num_verts = mod.shape.num_verts
(0...num_verts).each do |vert_i|
x = mod.shape.body.p.x
y = mod.shape.body.p.y
cur = mod.shape.vert(vert_i)
nxt = mod.shape.vert((vert_i+1)%num_verts)
@window.draw_line(x+cur.x, y+cur.y, 0xaaff0000, x+nxt.x, y+nxt.y, 0xaaff0000, render_depth+ZOrder::UI)
end
end
@mount_points.each do |mp|
@mount_point_image.draw_rot(mp.space_pos.x, mp.space_pos.y, render_depth+ZOrder::UI, 0, 0.5, 0.5, mp.s, mp.s)
mp.s = 1
end
x = mod.shape.body.p.x
y = mod.shape.body.p.y
width = @font.text_width(mod.data[:angle])
height = 11
@window.draw_quad(x, y, @filter, x+width, y, @filter, x+width, y+height, @filter, x, y+height, @filter, render_depth+ZOrder::UI)
@font.draw(mod.data[:angle], x, y, render_depth+ZOrder::UI, 1, 1, @font_color)
end
@ships.each do |ship|
ship.draw(render_depth)
end
if @selected
@selected.mount_points.each do |mount_point|
@mount_point_image.draw_rot(mount_point.space_pos.x, mount_point.space_pos.y, render_depth+ZOrder::UI, 0)
#@font.draw(index.to_s, vec.x, vec.y, render_depth+ZOrder::UI, 0.7, 0.7, 0xff0000ff)
end
end
end
end
def button_up(id)
case id
when Gosu::KbLeft
end_drag
end
end
def button_down(id)
case id
when Gosu::KbE
@window.end_scene
when Gosu::KbEscape
@window.close
when Gosu::MsLeft
start_drag @previous_scene.space.point_query_first(CP::Vec2.new(@camera.mouse_x, @camera.mouse_y))
when Gosu::MsRight
start_drag @previous_scene.space.point_query_first(CP::Vec2.new(@camera.mouse_x, @camera.mouse_y))
end
end
def start_drag shape
p shape
if shape && shape.object.data[:compatible] #body.
@selected = shape
if @selected.object.attached?
#ship = @selected.object.ship
#ship.remove_module(@selected.object) #@selected.object.detach
#ship.update_ship
#@selected.object.parent.unmount(@selected.object) if @selected.object.mounted?
@selected.object.disband
end
@selected_offset_x = @selected.object.get_p.x - @camera.mouse_x#@selected.body.p.x - @camera.mouse_x
@selected_offset_y = @selected.object.get_p.y - @camera.mouse_y#@selected.body.p.y - @camera.mouse_y
@selected_start_x = @selected.object.get_p.x#@selected.body.p.x
@selected_start_y = @selected.object.get_p.y#@selected.body.p.y
@selected_rotation = @selected.object.get_a#@selected.body.a
@selected = @selected.object
@mount_points.delete_if do |mp|
mp.object == @selected
end
end
end
def end_drag
if @selected
# TODO: only mount once and when movement is > .. or whatever
# FIXME: what is this for?
if (@selected.shape.body.p.x - @selected_start_x).abs > 10 || (@selected.shape.body.p.y - @selected_start_y).abs > 10 || (@selected.shape.body.a - @selected_rotation).abs > 1
#@previous_scene.connection_manager.remove_for_object(@selected)
end
@selected.mount_points.each do |mp1|
@mount_points.each do |mp2|
if Gosu.distance(mp1.space_pos.x, mp1.space_pos.y, mp2.space_pos.x, mp2.space_pos.y) < 5
#@previous_scene.connection_manager.remove_for_object(@selected)#disconnect(mp2.object, @selected)
other_angle = (mp2.object.get_a + mp2.angle.degrees_to_radians) #(mp2.object.shape.body.a + mp2.angle.degrees_to_radians)
@selected.shape.body.a = (other_angle - 180.degrees_to_radians) - mp1.angle.degrees_to_radians
@selected.shape.body.p = mp2.space_pos - mp1.p.rotate(@selected.shape.body.rot)
#@selected.add_data({:parent_mount_point => mp2.index, :mount_on => mp1.index})
#Actually mount!
mp2.object.mount_on(mp2.index, mp1.index, @selected)
if mp2.object.attached?
p "ship is attached"
#@selected.attach(mp2.object.ship)
mp2.object.ship.add_module(@selected)
mp2.object.ship.update_ship
else
mp2.object.create_new_ship
@selected.attach_to(mp2.object.ship) #huh?
end
@previous_scene.space.rehash_shape(@selected.shape)
#@previous_scene.connection_manager.connect(mp2.object, @selected)
p "Connected #{@selected} with #{mp2.object}"
end
end
end
@mount_points += @selected.mount_points
end
@selected = nil
end
end
end