-
Notifications
You must be signed in to change notification settings - Fork 4
/
Movable.rb
54 lines (46 loc) · 1.26 KB
/
Movable.rb
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
#
# Movable.rb
# CtF
#
# Created by Thomas R. Koll on 10.12.10.
# Copyright (c) 2010 ananasblau. All rights reserved.
#
module Movable
attr_accessor :speed, :turns, :last_turn, :orientation
MIN_SPEED = 2
MAX_SPEED = 10
MAX_TURN = 5
SPEED_FACTOR = 0.001
def turns_sum
self.turns = self.turns[0..(MAX_TURN*3)]
return (self.turns||[]).inject {|sum, i| sum + i}
end
def turn(val)
self.turns.unshift(val)
self.last_turn = self.turns_sum.abs > 1 ? val : 0
old_orientation = self.orientation
self.orientation = (self.orientation + self.turns_sum) % 360
return self.orientation - old_orientation
end
def speedUp(val)
self.speed = [[self.speed + val, MAX_SPEED].min, MIN_SPEED].max
end
def radians(angle)
(angle * Math::PI / 180) || 0
end
def camera_orientation
(orientation - 90) % 360
end
def move(turn = nil)
# First of all rotate
glRotatef(self.orientation, 0, 0, 1)
# Move x and y coords
if self.turns
self.turn(turn || self.last_turn || 0)
self.turns.shift if turn.nil?
self.turns.unshift(0)
end
self.x += Math.cos(self.radians(self.orientation)) * self.speed * SPEED_FACTOR
self.y += Math.sin(self.radians(self.orientation)) * self.speed * SPEED_FACTOR
end
end