Class: Teien::SmoothMoving

Inherits:
Object
  • Object
show all
Defined in:
lib/teien/action_model/smooth_moving.rb

Constant Summary collapse

ACCELERATION =
5.0
TURN_SPEED =
500.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_object_name) ⇒ SmoothMoving



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/teien/action_model/smooth_moving.rb', line 12

def initialize(target_object_name)
  @target_object_name = target_object_name
  @target_object = Teien::get_component("base_object_manager").objects[target_object_name]
  @acceleration = ACCELERATION
  @turn_speed = TURN_SPEED
  @move_dir = Ogre::Vector3.new(0, 0, 0)
  @forward_dir = Ogre::Vector3.new(0, 0, 0)
  @zero_vector = Vector3D.new(0, 0, 0)
  clear_action()
  @movable = true

  @event_router = Teien::get_component("event_router")
end

Instance Attribute Details

#accelerationObject

Returns the value of attribute acceleration.



8
9
10
# File 'lib/teien/action_model/smooth_moving.rb', line 8

def acceleration
  @acceleration
end

#movableObject

Returns the value of attribute movable.



10
11
12
# File 'lib/teien/action_model/smooth_moving.rb', line 10

def movable
  @movable
end

#target_object_nameObject

Returns the value of attribute target_object_name.



7
8
9
# File 'lib/teien/action_model/smooth_moving.rb', line 7

def target_object_name
  @target_object_name
end

#turn_speedObject

Returns the value of attribute turn_speed.



9
10
11
# File 'lib/teien/action_model/smooth_moving.rb', line 9

def turn_speed
  @turn_speed
end

Instance Method Details

#clear_actionObject



26
27
28
29
30
31
# File 'lib/teien/action_model/smooth_moving.rb', line 26

def clear_action()
  @forward = false
  @backward = false
  @left = false
  @right = false
end

#move_backward(bool) ⇒ Object



56
57
58
# File 'lib/teien/action_model/smooth_moving.rb', line 56

def move_backward(bool)
  @backward = bool
end

#move_forward(bool) ⇒ Object



52
53
54
# File 'lib/teien/action_model/smooth_moving.rb', line 52

def move_forward(bool)
  @forward = bool
end

#move_left(bool) ⇒ Object



60
61
62
# File 'lib/teien/action_model/smooth_moving.rb', line 60

def move_left(bool)
  @left = bool
end

#move_right(bool) ⇒ Object



64
65
66
# File 'lib/teien/action_model/smooth_moving.rb', line 64

def move_right(bool)
  @right = bool
end

#moving?Boolean



33
34
35
# File 'lib/teien/action_model/smooth_moving.rb', line 33

def moving?()
  return (@forward || @backward || @left || @right)
end

#set_acceleration(accel) ⇒ Object



37
38
39
# File 'lib/teien/action_model/smooth_moving.rb', line 37

def set_acceleration(accel)
  @acceleration = accel
end

#set_forward_direction(forward_dir) ⇒ Object

This direction is the forward.



48
49
50
# File 'lib/teien/action_model/smooth_moving.rb', line 48

def set_forward_direction(forward_dir)
  @forward_dir = forward_dir
end

#set_turn_speed(turn_speed) ⇒ Object



41
42
43
# File 'lib/teien/action_model/smooth_moving.rb', line 41

def set_turn_speed(turn_speed)
  @turn_speed = turn_speed
end

#update_target(delta) ⇒ Object



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
# File 'lib/teien/action_model/smooth_moving.rb', line 69

def update_target(delta)
  @move_dir.x = 0
  @move_dir.y = 0
  @move_dir.z = 0

  # update target's acceleration
  if (@forward)
    @move_dir += Ogre::Vector3.new(@forward_dir.x, @forward_dir.y, @forward_dir.z)
  end
  if (@backward)
    @move_dir += Ogre::Vector3.new(-@forward_dir.x, -@forward_dir.y, -@forward_dir.z)
  end
  if (@left)
    @move_dir += Ogre::Vector3.new(@forward_dir.z, 0,  -@forward_dir.x)
  end
  if (@right)
    @move_dir += Ogre::Vector3.new(-@forward_dir.z, 0, @forward_dir.x)
  end

  @move_dir.y = 0
  @move_dir.normalise()

  if (@movable)
    newAcc = @move_dir * @acceleration
    @target_object.set_acceleration(Vector3D.to_self(newAcc))
  else
    @target_object.set_acceleration(@zero_vector)
  end

  # update target's direction
  toGoal = Vector3D.to_ogre(-@target_object.get_orientation().z_axis()).get_rotation_to(@move_dir)
  yawToGoal = toGoal.get_yaw().value_degrees()
  yawAtSpeed = yawToGoal / yawToGoal.abs * delta * @turn_speed

  if (yawToGoal < 0) 
    yawToGoal = [0, [yawToGoal, yawAtSpeed].max].min
  elsif (yawToGoal > 0) 
    yawToGoal = [0, [yawToGoal, yawAtSpeed].min].max
  end

  @target_object.yaw(Ogre::Degree.new(yawToGoal).value_radians())
end