-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.script
More file actions
52 lines (44 loc) · 1.93 KB
/
camera.script
File metadata and controls
52 lines (44 loc) · 1.93 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
_G.world_width = 4096 -- The total world size
_G.world_height = 3000
local camera_speed = 300 -- Speed of camera movement
_G.camera_pos = vmath.vector3()
function init(self)
self.camera = go.get_id()
self.camera_position = go.get_position(self.camera)
self.screen_width = _G.window_width
self.screen_height = _G.window_height
-- Movement direction (continuous movement when key is held)
self.move_x = 0
self.move_y = 0
msg.post(".", "acquire_input_focus")
end
function update(self, dt)
-- Smooth movement using velocity and delta time
local dx = self.move_x * camera_speed * dt
local dy = self.move_y * camera_speed * dt
-- Update position
self.camera_position.x = self.camera_position.x + dx
self.camera_position.y = self.camera_position.y + dy
-- Ensure camera stays inside world bounds
local half_screen_width = self.screen_width / 2
local half_screen_height = self.screen_height / 2
self.camera_position.x = math.max(half_screen_width, math.min(self.camera_position.x, _G.world_width - half_screen_width))
self.camera_position.y = math.max(half_screen_height, math.min(self.camera_position.y, _G.world_height - half_screen_height))
-- Apply position update
go.set_position(self.camera_position, self.camera)
-- Update global camera position
_G.camera_pos = go.get_position()
end
function on_input(self, action_id, action)
-- Adjust movement direction based on key press/release
if action_id == hash("left") then
self.move_x = action.pressed and -1 or (action.released and 0 or self.move_x)
elseif action_id == hash("right") then
self.move_x = action.pressed and 1 or (action.released and 0 or self.move_x)
elseif action_id == hash("up") then
self.move_y = action.pressed and 1 or (action.released and 0 or self.move_y)
elseif action_id == hash("down") then
self.move_y = action.pressed and -1 or (action.released and 0 or self.move_y)
end
msg.post("/initfactory#init_spawner", "offset", {pos = self.camera_position})
end