-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
147 lines (126 loc) · 4.42 KB
/
player.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
from __future__ import division
import time
from math import atan2, sqrt, cos, sin, acos, asin, pi, degrees
from geom3 import Point3, Vector3, unit, dot
from robot import Robot
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
class Player(object):
def __init__(self, config, start_point):
self.position = start_point + Vector3(0, 0, 0)
self.lookat = start_point + Vector3(1, 0, 0)
self.viewup = Vector3(0, 1, 0)
self.sensitivity = config['sensitivity']
self.speed = config['speed']
self.moving = set()
self.in_motion = False
self.third_person = False
self.disconnected = False
self.robot = Robot(config['robot'], start_point)
def move(self, direction):
if direction in self.moving:
return
if direction in ['LEFT', 'RIGHT', 'FORWARD', 'BACK'] or self.disconnected and direction in ['UP', 'DOWN']:
self.moving.add(direction)
if not self.in_motion:
self.in_motion = True
self.update_time = time.time()
self.update()
elif self.disconnected:
self.robot.move(direction)
def stop(self, direction):
if direction in ['LEFT', 'RIGHT', 'FORWARD', 'BACK'] or self.disconnected and direction in ['UP', 'DOWN']:
if direction in self.moving:
self.moving.remove(direction)
elif self.disconnected:
self.robot.stop(direction)
def adjust(self, movement):
self.robot.adjust(movement)
def update(self, value=0):
if len(self.moving) > 0:
for direction in self.moving:
if direction == 'FORWARD':
self.forward(self.speed * (time.time() - self.update_time))
elif direction == 'LEFT':
self.right(-self.speed * (time.time() - self.update_time))
elif direction == 'RIGHT':
self.right(self.speed * (time.time() - self.update_time))
elif direction == 'BACK':
self.forward(-self.speed * (time.time() - self.update_time))
elif direction == 'UP':
self.up(self.speed * (time.time() - self.update_time))
elif direction == 'DOWN':
self.up(-self.speed * (time.time() - self.update_time))
self.update_time = time.time()
glutTimerFunc(10, self.update, 0)
else:
self.in_motion = False
def facing(self):
direction = self.lookat - self.position
if not self.disconnected:
direction.dy = 0
return unit(direction)
def forward(self, amount):
movement = amount * unit(self.facing())
self.position += movement
self.lookat += movement
if not self.disconnected:
self.robot.forward(amount)
glutPostRedisplay()
def right(self, amount):
movement = amount * unit(self.facing().cross(self.viewup))
self.position += movement
self.lookat += movement
if not self.disconnected:
self.robot.right(amount)
glutPostRedisplay()
def up (self, amount):
movement = amount * self.viewup
self.position += movement
self.lookat += movement
if not self.disconnected:
self.robot.up(amount)
glutPostRedisplay()
def offset(self):
if self.third_person and not self.disconnected:
return Vector3(-100 * (self.lookat - self.position))
else:
return Vector3(0,0,0)
def turn(self, rotation):
(x, y, z) = self.lookat - self.position
r = sqrt(x*x + y*y + z*z)
theta = max(0.01, min(pi - 0.01, acos(y / r) + self.sensitivity * rotation[1]))
phi = atan2(z, x) + self.sensitivity * rotation[0]
(x, y, z) = (r * sin(theta) * cos(phi), r * cos(theta), r * sin(theta) * sin(phi))
self.lookat = self.position + Vector3(x, y, z)
if not self.disconnected:
self.robot.turn(self.sensitivity * rotation[0])
glutPostRedisplay()
def action(self, value):
if value >= -15:
if self.disconnected:
self.robot.up(value/10)
else:
self.up(value/10)
glutPostRedisplay()
glutTimerFunc(10, self.action, value - 1)
def switch_third_person(self):
if self.third_person:
self.third_person = False
else:
self.third_person = True
glutPostRedisplay()
def switch_viewpoint(self):
if self.disconnected:
self.disconnected = False
self.position = self.robot.position
self.lookat = self.robot.lookat
else:
self.disconnected = True
glutPostRedisplay()
def gl_init(self):
self.robot.gl_init()
def display(self):
self.robot.display(self.disconnected or self.third_person)