File size: 2,329 Bytes
898c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
local camera = {}

camera.init = function(param)
  local self = {}
  self.position = {x = param.position[1], y = param.position[2]}
  self.zoom = param.zoom
  self.set_zoom = param.set_zoom
  self.players = {}
  return self
end

camera.hold = function(self, param)
  if param.position then
    self.position = {x = param.position[1], y = param.position[2]}
  end
  if param.zoom then
    self.zoom = param.zoom
  end
  self.hold_time = game.tick + (param.time*60)
end

camera.move = function (self, param)
  local d_t = param.time * 60
  local start_tick = game.tick + 1
  local tick_to_finish = start_tick + d_t
  local o_x = self.position.x
  local o_y = self.position.y
  local o_z = self.zoom
  local v_x = o_x
  local v_y = o_y
  if param.position then
    v_x = param.position[1]
    v_y = param.position[2]
  elseif param.entity then
    v_x = param.entity.position.x
    v_y = param.entity.position.y
  end
  local d_x = (v_x-o_x)/d_t
  local d_y = (v_y-o_y)/d_t
  local d_z = (param.zoom - self.zoom)/d_t or 0
  self.position_on_tick = {}
  local d = 1
  for k = start_tick, tick_to_finish do
    self.position_on_tick[k] = {position = {x = (o_x + (d_x*d)), y = (o_y + (d_y*d))}, zoom = (o_z + d_z*d)}
    d = d + 1
  end
  self.position_on_tick[tick_to_finish].last = true
end

camera.update = function(self)
  if not self then return end
  local tick = game.tick
  if self.position_on_tick then
    local new_position = self.position_on_tick[tick]
    if new_position then
      self.position = new_position.position
      self.zoom = new_position.zoom
      if new_position.last then
        self.position_on_tick = nil
      end
    end
  end
  if self.hold_time then
    if tick >= self.hold_time then
      self.hold_time = nil
    end
  end
  if self.following then
    if self.following.valid then
      self.position = self.following.position
    else
      self.following = nil
    end
  end
  for k, player in pairs (self.players) do
    player.teleport(self.position)
    if self.set_zoom then
      player.zoom = self.zoom
    end
  end
end

camera.follow = function(self, entity)
  self.following = entity
end

camera.idle = function(self)
  if self.position_on_tick then return false end
  if self.hold_time then return false end
  if self.following then return false end
  return true
end

return camera