Class: Teien::CameraMover

Inherits:
Object
  • Object
show all
Defined in:
lib/teien/ui/camera_mover.rb

Constant Summary collapse

CS_FREELOOK =
0
CS_ORBIT =
1
CS_MANUAL =
2
CS_TPS =
3
CAM_HEIGHT =
5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cam) ⇒ CameraMover

Returns a new instance of CameraMover.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/teien/ui/camera_mover.rb', line 15

def initialize(cam)
  @camera = cam
  @camera.set_position(0, 0, 0)
  @camera.set_near_clip_distance(0.1)

  # CS_FREELOOK, CS_ORBIT, CS_MANUAL
  @sdk_camera_man = Ogrebites::SdkCameraMan.new(@camera)
  @evt_frame = Ogre::FrameEvent.new

  # CS_TPS
  @height = CAM_HEIGHT
  @camera_pivot = cam.get_scene_manager().get_root_scene_node().create_child_scene_node()
  @camera_goal = @camera_pivot.create_child_scene_node(Ogre::Vector3.new(0, 0, 5))

  @camera_pivot.set_fixed_yaw_axis(true)
  @camera_goal.set_fixed_yaw_axis(true)

  @pivot_pitch = 0

  set_style(CS_FREELOOK)
end

Instance Attribute Details

#cameraObject

Returns the value of attribute camera.



13
14
15
# File 'lib/teien/ui/camera_mover.rb', line 13

def camera
  @camera
end

#camera_pivotObject

Returns the value of attribute camera_pivot.



12
13
14
# File 'lib/teien/ui/camera_mover.rb', line 12

def camera_pivot
  @camera_pivot
end

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/teien/ui/camera_mover.rb', line 11

def height
  @height
end

Instance Method Details

#look_at(pos) ⇒ Object



64
65
66
# File 'lib/teien/ui/camera_mover.rb', line 64

def look_at(pos)
  @camera.look_at(Vector3D.to_ogre(pos)) if @style == CS_FREELOOK
end

#mouse_moved(evt) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/teien/ui/camera_mover.rb', line 132

def mouse_moved(evt)
#    puts "#{evt.state.X.rel}, #{evt.state.X.abs}"
#    puts "#{evt.state.Y.rel}, #{evt.state.Y.abs}"
#    puts ""

  # deal with a warp.
  if evt.state.X.rel.abs > 300
    return true
  end

  if @style == CS_TPS
    update_camera_goal(-0.05 * evt.state.X.rel, 
                       -0.05 * evt.state.Y.rel, 
                       -0.0005 * evt.state.Z.rel)
  else
    @sdk_camera_man.inject_mouse_move(evt)      
  end    
  return true
end

#mouse_pressed(mouseEvent, mouseButtonID) ⇒ Object



182
183
184
185
# File 'lib/teien/ui/camera_mover.rb', line 182

def mouse_pressed(mouseEvent, mouseButtonID)
  @sdk_camera_man.inject_mouse_down(mouseEvent, mouseButtonID) if @style == CS_ORBIT
  return true
end

#mouse_released(mouseEvent, mouseButtonID) ⇒ Object



187
188
189
190
# File 'lib/teien/ui/camera_mover.rb', line 187

def mouse_released(mouseEvent, mouseButtonID)
  @sdk_camera_man.inject_mouse_up(mouseEvent, mouseButtonID) if @style == CS_ORBIT
  return true
end

#move_backward(bl) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/teien/ui/camera_mover.rb', line 81

def move_backward(bl)
  evt = Ois::KeyEvent.new(nil, Ois::KC_S, 0)
  if bl
    @sdk_camera_man.inject_key_down(evt)
  else
    @sdk_camera_man.inject_key_up(evt)
  end
end

#move_forward(bl) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/teien/ui/camera_mover.rb', line 72

def move_forward(bl)
  evt = Ois::KeyEvent.new(nil, Ois::KC_W, 0)
  if bl
    @sdk_camera_man.inject_key_down(evt)
  else
    @sdk_camera_man.inject_key_up(evt)
  end
end

#move_left(bl) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/teien/ui/camera_mover.rb', line 90

def move_left(bl)
  evt = Ois::KeyEvent.new(nil, Ois::KC_A, 0)
  if bl
    @sdk_camera_man.inject_key_down(evt)
  else
    @sdk_camera_man.inject_key_up(evt)
  end
end

#move_right(bl) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/teien/ui/camera_mover.rb', line 99

def move_right(bl)
  evt = Ois::KeyEvent.new(nil, Ois::KC_D, 0)
  if bl
    @sdk_camera_man.inject_key_down(evt)
  else
    @sdk_camera_man.inject_key_up(evt)
  end
end

#set_position(pos) ⇒ Object



60
61
62
# File 'lib/teien/ui/camera_mover.rb', line 60

def set_position(pos)
  @camera.set_position(Vector3D.to_ogre(pos)) if @style == CS_FREELOOK
end

#set_style(style) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/teien/ui/camera_mover.rb', line 37

def set_style(style)
  @style = style
  case @style
  when CS_FREELOOK
    @sdk_camera_man.set_style(Ogrebites::CS_FREELOOK)
  when CS_ORBIT
    @sdk_camera_man.set_style(Ogrebites::CS_ORBIT)
  else  # CS_MANUAL, CS_TPS
    @sdk_camera_man.set_style(Ogrebites::CS_MANUAL)
  end
end

#set_target(target) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/teien/ui/camera_mover.rb', line 49

def set_target(target)
  @target = target
  if @style == CS_TPS
    @camera.set_auto_tracking(false)
    @camera.move_relative(Ogre::Vector3.new(0, 0, 0))
    update_camera(1.0)
  else
    @sdk_camera_man.set_target(target.pivotSceneNode)
  end
end

#set_yaw_pitch_dist(yaw, pitch, dist) ⇒ Object



68
69
70
# File 'lib/teien/ui/camera_mover.rb', line 68

def set_yaw_pitch_dist(yaw, pitch, dist)
  @sdk_camera_man.setYawPitchDist(yaw, pitch, dist) if @style == CS_ORBIT
end

#update(delta) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/teien/ui/camera_mover.rb', line 109

def update(delta)
  if (@style == CS_TPS)
    update_camera(delta)
  else
    @evt_frame.timeSinceLastFrame = delta
    @sdk_camera_man.frame_rendering_queued(@evt_frame)
  end
end

#update_camera(deltaTime) ⇒ Object

This method moves this camera position to the goal position smoothly. In general, should be called in the frameRenderingQueued handler.



122
123
124
125
126
127
128
129
130
# File 'lib/teien/ui/camera_mover.rb', line 122

def update_camera(deltaTime)
  # place the camera pivot roughly at the character's shoulder
  @camera_pivot.set_position(Vector3D::to_ogre(@target.get_position()) + Ogre::Vector3.UNIT_Y * @height)
  # move the camera smoothly to the goal
  goalOffset = @camera_goal._get_derived_position() - @camera.get_position()
  @camera.move(goalOffset * deltaTime * 9.0)
  # always look at the pivot
  @camera.look_at(@camera_pivot._get_derived_position())
end

#update_camera_goal(deltaYaw, deltaPitch, deltaZoom) ⇒ Object

This method updates the goal position, which this camera should be placed finally. In general, should be called when the mouse is moved. deltaYaw::float, degree value. deltaPitch::float, degree value. deltaZoom::float, zoom



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/teien/ui/camera_mover.rb', line 159

def update_camera_goal(deltaYaw, deltaPitch, deltaZoom)

  @camera_pivot.yaw(Ogre::Radian.new(Ogre::Degree.new(deltaYaw)), Ogre::Node::TS_WORLD);

  # bound the pitch
  if (!(@pivot_pitch + deltaPitch > 25 && deltaPitch > 0) &&
      !(@pivot_pitch + deltaPitch < -60 && deltaPitch < 0))
    @camera_pivot.pitch(Ogre::Radian.new(Ogre::Degree.new(deltaPitch)), Ogre::Node::TS_LOCAL)
    @pivot_pitch += deltaPitch;
  end
  dist = @camera_goal._get_derived_position().distance(@camera_pivot._get_derived_position())
  distChange = deltaZoom * dist;

#    puts "dist: #{dist}:#{distChange}"

  # bound the zoom
  if (!(dist + distChange < 8 && distChange < 0) &&
      !(dist + distChange > 25 && distChange > 0))

    @camera_goal.translate(Ogre::Vector3.new(0, 0, distChange), Ogre::Node::TS_LOCAL)
  end
end