Class: CameraBase

Inherits:
Moon::DataModel::Metal
  • Object
show all
Includes:
Moon::Transitionable
Defined in:
lib/moon/packages/twod/camera_base.rb

Overview

Generic Point Camera

Direct Known Subclasses

Camera2, Camera3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Moon::Transitionable

#key_transition, #remove_transitions, #transition

Methods included from Moon::TransitionHost

#add_transition, #finish_transitions, #remove_transition, #remove_transitions, #update_transitions

Instance Attribute Details

#objObject

Returns:


14
# File 'lib/moon/packages/twod/camera_base.rb', line 14

field :obj,      type: Object,        allow_nil: true, default: nil

#speedInteger

Returns:


8
# File 'lib/moon/packages/twod/camera_base.rb', line 8

field :speed,    type: Integer,       default: 4

#ticksInteger

Returns:


11
# File 'lib/moon/packages/twod/camera_base.rb', line 11

field :ticks,    type: Integer,       default: 0

#viewMoon::Rect

Returns:


17
# File 'lib/moon/packages/twod/camera_base.rb', line 17

field :view,     type: Moon::Rect

Instance Method Details

#follow(obj) ⇒ Object

Camera should follow this object every update

Parameters:

  • obj (#position)

30
31
32
33
# File 'lib/moon/packages/twod/camera_base.rb', line 30

def follow(obj)
  self.obj = obj
  puts "[Camera:follow] #{obj}"
end

#screen_to_world(screen_pos) ⇒ Moon::Vector2, Moon::Vector3

Parameters:

Returns:


56
57
58
# File 'lib/moon/packages/twod/camera_base.rb', line 56

def screen_to_world(screen_pos)
  (view_offset.floor + screen_pos) / tilesize
end

#screen_to_world_reduce(screen_pos) ⇒ Moon::Vector2, Moon::Vector3

Parameters:

Returns:


63
64
65
# File 'lib/moon/packages/twod/camera_base.rb', line 63

def screen_to_world_reduce(screen_pos)
  screen_to_world(screen_pos).floor * tilesize - view_offset.floor
end

#update(delta) ⇒ Object

Frame update

Parameters:


47
48
49
50
51
# File 'lib/moon/packages/twod/camera_base.rb', line 47

def update(delta)
  update_transitions delta
  self.position += velocity delta
  self.ticks += 1
end

#velocity(delta) ⇒ Vector

Returns:

  • (Vector)

36
37
38
39
40
41
42
# File 'lib/moon/packages/twod/camera_base.rb', line 36

def velocity(delta)
  if obj
    (obj.position * tilesize - position) * speed * delta
  else
    0
  end
end

#world_to_screen(world_pos) ⇒ Moon::Vector2, Moon::Vector3

Parameters:

Returns:


70
71
72
# File 'lib/moon/packages/twod/camera_base.rb', line 70

def world_to_screen(world_pos)
  tilesize * world_pos - view_offset.floor
end