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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
GameName = 'Hextest'
Rl.init_window(800, 600, GameName)
include FECS
source_shine = Rl::Rectangle.new(7,
128,
111,
128)
source = Rl::Rectangle.new(7,
0,
111,
128)
offset_x = 100
offset_y = 100
Cmp.new('Position', x: 0, y: 0)
#Cmp.new('Shape', radius: 50, line_thickness: 5, sides: 3)
Cmp.new('Shape', :obj)
Cmp.new('ArraySpot', :x, :y)
Cmp.new('ShapeColor', :color)
Cmp.new('BorderColor', :color)
ShapeSize = 6.0 # needs to be float
Sys.new('InitGrid') do
arry = Array.new(5) do |x|
Array.new(5) do |y|
x_thingie = 90
Ent.new(
Cmp::Position.new(
x: (x * x_thingie) + (y * (x_thingie/2)) + offset_x,
y: (y * 50) + (y * 30) + offset_y,
),
Cmp::Shape.new(sides: 6),
Cmp::ArraySpot.new(x: x, y: y)
)
end
end
end
#Sys::InitGrid.call
Sys.new('InitShape') do
xoff = 350
yoff = 350
multi = 100
#shape = Cmp::Shape.new(sides:6)
ShapeSize.to_i.times do |point|
Ent.new(
Cmp::Position.new(
x: Math.sin((point/ShapeSize) * Math::PI * 2) * multi + xoff,
y: Math.cos((point/ShapeSize) * Math::PI * 2) * multi + yoff
),
Cmp::Shape.new(sides:ShapeSize)
)
end
end
#Sys::InitShape.call
class Shape
attr_reader :angle, :size, :x, :y, :sides
def initialize(angle: 0, size: 0, x: 0, y: 0, sides: 3)
@sides = sides
@angle = angle
@size = size
@x = x
@y = y
update
end
def points
@points ||= []
end
def sides=(sides)
@sides = sides
self.update
end
def angle=(angle)
@angle = angle
self.update
end
def size=(size)
@size = size
self.update
end
def x=(x)
@x = x
self.update
end
def y=(y)
@y = y
self.update
end
private
def update
sides.times do |point_num|
points[point_num] ||= Hash.new
points[point_num][:x] = Math.sin(((point_num/sides.to_f) * Math::PI * 2) - angle) * size + x
points[point_num][:y] = Math.cos(((point_num/sides.to_f) * Math::PI * 2) - angle) * size + y
end
[sides - points.length, 0].max.times do
points.pop # strip extra points
end
end
end
Target = Cmp::Shape.new(obj: Shape.new(sides: 6, size: 100, x: 300, y: 300))
MouseFollow = Cmp::Shape.new(obj: Shape.new(sides: 6, size: 100))
Ent.new(
Target,
Cmp::ShapeColor.new(color: Rl::Color.medium_orchid),
Cmp::BorderColor.new(color: Rl::Color.medium_orchid)
)
Ent.new(
MouseFollow,
Cmp::ShapeColor.new(color: Rl::Color.dodger_blue),
Cmp::BorderColor.new(color: Rl::Color.dodger_blue)
)
Sys.new('DrawShape') do
Ent.group(Cmp::Shape, Cmp::ShapeColor, Cmp::BorderColor) do |shape_cmp, color_cmp, border_color_cmp, entity|
shape = shape_cmp.obj
array_spot = false
#if !entity.components[Cmp::ArraySpot].nil?
# array_spot = entity.component[Cmp::ArraySpot]
#end
Rl.draw_poly(center: Rl::Vector2.new(shape.x, shape.y),
radius: shape.size,
sides: shape.sides,
rotation: shape.angle,
color: color_cmp.color)
Rl.draw_poly_lines(center: Rl::Vector2.new(shape.x, shape.y),
radius: shape.size,
sides: shape.sides,
rotation: shape.angle,
color: border_color_cmp.color,
line_thickness: shape.size/10)
if array_spot
"x: #{array_spot.x}".draw(x: position.x - 30, y: position.y - 20, color: Rl::Color.dark_violet)
"y: #{array_spot.y}".draw(x: position.x - 30, y: position.y, color: Rl::Color.dark_violet)
end
end
end
module SAT
class << self
# The hitbox logic
def hitbox_check(shape_a, shape_b)
# Get normals of both shapes
inverted = build_inverted_edges(shape_a)
inverted.concat(build_inverted_edges(shape_b))
#DEBUG
#debug_outer_loop(inverted)
inverted.each_with_index do |line, line_index|
# Determine max and min of a and b shapes
amax, amin = calculate_minmax(shape_a, line)
bmax, bmin = calculate_minmax(shape_b, line)
#DEBUG
#debug_inner_loop(shape_a, shape_b, line_index, amax, amin, bmax, bmin)
if ((amin <= bmax) && (amin >= bmin)) || ((bmin <= amax) && (bmin >= amin))
#next
else
# The logic should end the calculations early once it detects lack of collision
# however for debug purposes this is disabled
return false
end
end
true
end
# Creates edges out using coordinates and then gets the normal
def build_inverted_edges(shape)
edges = []
shape.each_with_index do |vertex_start, index|
vertex_end = if index == shape.length - 1
shape[0]
else
shape[index + 1]
end
edges.push [vertex_end[1] - vertex_start[1],
-(vertex_end[0] - vertex_start[0])]
end
edges
end
# Dot product
def vecDotProd(a, b)
(a[0] * b[0]) + (a[1] * b[1])
end
# Calculates the minimum point and maximum point projected onto the line
def calculate_minmax(shape, line)
min = vecDotProd(shape.first, line)
max = vecDotProd(shape.first, line)
shape.each_with_index do |vertex, _vertex_index|
dot = vecDotProd(vertex, line)
if dot > max
max = dot
elsif dot < min
min = dot
end
end
[max, min]
end
end
end
Rl.target_fps = 60
Rl.while_window_open do
if Rl.key_pressed? 61 # plus/equal
if (Rl.key_down? 340) || (Rl.key_down? 344)
Target.obj.sides += 1 unless Target.obj.sides == 9
else
MouseFollow.obj.sides += 1 unless MouseFollow.obj.sides == 9
end
end
if Rl.key_pressed? 45 # minus/underscore
if (Rl.key_down? 340) || (Rl.key_down? 344)
Target.obj.sides -= 1 unless Target.obj.sides == 3
else
MouseFollow.obj.sides -= 1 unless MouseFollow.obj.sides == 3
end
end
if Rl.key_down? 65 # a
if (Rl.key_down? 340) || (Rl.key_down? 344)
Target.obj.angle -= (Math::PI/180) * 2
else
MouseFollow.obj.angle -= (Math::PI/180) * 2
end
end
if Rl.key_down? 68 # d
if (Rl.key_down? 340) || (Rl.key_down? 344)
Target.obj.angle += (Math::PI/180) * 2
else
MouseFollow.obj.angle += (Math::PI/180) * 2
end
end
if Rl.key_down? 87 # w
if (Rl.key_down? 340) || (Rl.key_down? 344)
Target.obj.size += 1
else
MouseFollow.obj.size += 1
end
end
if Rl.key_down? 83 # s
if (Rl.key_down? 340) || (Rl.key_down? 344)
Target.obj.size -= 1
else
MouseFollow.obj.size -= 1
end
end
Rl.draw(clear_color: Rl::Color.black) do
MouseFollow.obj.x = Rl.mouse_x
MouseFollow.obj.y = Rl.mouse_y
if SAT.hitbox_check(
Array.new(MouseFollow.obj.sides) do |side|
[MouseFollow.obj.points[side][:x],
MouseFollow.obj.points[side][:y]]
end,
Array.new(Target.obj.sides) do |side|
[Target.obj.points[side][:x],
Target.obj.points[side][:y]]
end
)
MouseFollow.entity.component[Cmp::ShapeColor].color = Rl::Color.fire_brick
else
MouseFollow.entity.component[Cmp::ShapeColor].color = Rl::Color.lime_green
end
Sys::DrawShape.call
end
end
|