-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.lua
More file actions
152 lines (108 loc) · 3.71 KB
/
global.lua
File metadata and controls
152 lines (108 loc) · 3.71 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
local M = {}
function M.rectangles_collide(x1, y1, w1, h1, x2, y2, w2, h2)
return x1 < x2 + w2 and -- Left edge of rect1 is to the left of right edge of rect2
x1 + w1 > x2 and -- Right edge of rect1 is to the right of left edge of rect2
y1 < y2 + h2 and -- Top edge of rect1 is above bottom edge of rect2
y1 + h1 > y2 -- Bottom edge of rect1 is below top edge of rect2
end
function M.random_position()
local x = math.random(25, _G.world_width-25)
local y = math.random(25, _G.world_height-25)
return vmath.vector3(x, y, 0)
end
function M.random_position_around_base(base_pos, range)
local x = math.random(base_pos.x - range, base_pos.x + range)
local y = math.random(base_pos.y - range, base_pos.y + range)
return vmath.vector3(x, y, 0)
end
function M.contains(table, value)
for _, v in ipairs(table) do
if v == value then
return true
end
end
return false
end
function M.is_mouse_inside_collision(mouse_pos, go_position, collision_width, collision_height)
local half_width = collision_width / 2
local half_height = collision_height / 2
-- Check if the mouse is inside the bounding box of the collision object
if mouse_pos.x >= go_position.x - half_width and mouse_pos.x <= go_position.x + half_width and
mouse_pos.y >= go_position.y - half_height and mouse_pos.y <= go_position.y + half_height then
return true
end
return false
end
function M.is_point_inside_rect(point, rect_pos, rect_size)
local min_x = rect_pos.x
local max_x = rect_pos.x + rect_size.x
local min_y = rect_pos.y
local max_y = rect_pos.y + rect_size.y
return point.x >= min_x and point.x <= max_x and point.y >= min_y and point.y <= max_y
end
function M.get_rect_from_drag(pos1, pos2)
local min_x = math.min(pos1.x, pos2.x)
local max_x = math.max(pos1.x, pos2.x)
local min_y = math.min(pos1.y, pos2.y)
local max_y = math.max(pos1.y, pos2.y)
return vmath.vector3(min_x, min_y, 0), vmath.vector3(max_x - min_x, max_y - min_y, 0)
end
-- Converts screen position to world position based on the camera position.
function M.convert_screen_pos(screen_pos, camera_position)
-- Screen-to-world conversion: Adjust for camera position
local world_x = screen_pos.x + camera_position.x - (1500 / 2)
local world_y = screen_pos.y + camera_position.y - (800 / 2)
return vmath.vector3(world_x, world_y, 0)
end
-- Converts world position to screen position (for debugging)
function M.convert_click_pos(world_pos, camera_position)
-- World-to-screen conversion: Adjust for camera position
local screen_x = world_pos.x - camera_position.x + (1500 / 2)
local screen_y = world_pos.y - camera_position.y + (800 / 2)
return vmath.vector3(screen_x, screen_y, 0)
end
function M.random_float(min, max)
return min + (math.random() * (max - min))
end
function M.capitalize(str)
return str:sub(1, 1):upper() .. str:sub(2):lower()
end
function M.remove_by_value(table, value)
for i, v in ipairs(table) do
if v == value then
return i
end
end
end
function M.distance_to(start_pos, end_pos)
return vmath.length((start_pos - end_pos))
end
function M.is_point_in_circle(px, py, cx, cy, radius)
local dx = px - cx
local dy = py - cy
return (dx * dx + dy * dy) <= (radius * radius)
end
function M.random_position_for_enemy_base()
local x = math.random(0, 4000)
local y = math.random(0, 2500)
if M.distance_to(_G.base_pos, vmath.vector3(x,y,0)) > 1000 then
return vmath.vector3(x, y, 0)
else
return M.random_position_for_enemy_base()
end
end
function M.slice(tbl, first, last)
local sliced = {}
for i = first or 1, last or #tbl do
sliced[#sliced + 1] = tbl[i]
end
return sliced
end
function M.tables_equal(a, b)
if #a ~= #b then return false end
for i = 1, #a do
if a[i] ~= b[i] then return false end
end
return true
end
return M