-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotagonist.py
107 lines (93 loc) · 3.26 KB
/
protagonist.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
# This file is part of Nink.
#
# Nink is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Nink is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Nink. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
from contextlib import nested
from pyglet.gl import *
import math
from gletools import ShaderProgram, Sampler2D, Matrix, Texture, VBO
from ctypes import c_float
from vector import Vector
def xfrange(start, stop, step):
while start < stop:
yield start
start += step
class Protagonist(object):
def __init__(self, texture, program, x, y, position, collisionMap, mesh):
super(Protagonist, self).__init__()
self.tex = texture
self.prog = program
self.x = float(x)
self.y = float(y)
self.cooldown = 0.0
self.collisionMap = collisionMap
self.health = 100
self.velocity = Vector(0.0, 0.0, 0.0)
self.position = Vector(position[0], self.y/2, position[1])
self.maxSpeed = 4
self.modelview = Matrix().translate(position[0], self.y/2, position[1])
self.moved = False
self.angle = 0
self.mesh = mesh
with nested(self.tex):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
def update(self, delta, state):
self.cooldown-= delta
if self.moved:
self.moved = False
else:
self.velocity = self.velocity * 0.8
if self.velocity.len() < 0.1:
self.velocity = Vector()
facingVelocity = self.velocity.rotatey(self.angle)
movedBy = facingVelocity * delta
if not self.collisionMap.collision(self.position + movedBy, self.x/4):
self.position = self.position + movedBy
self.modelview = Matrix().translate(self.position.x, self.position.y, self.position.z) * Matrix().rotatey(-self.angle)
def move(self, dvel):
self.moved = True
if self.velocity.len() == self.maxSpeed:
return
else:
self.velocity = self.velocity + dvel
if self.velocity.len() > self.maxSpeed:
self.velocity.norm(self.maxSpeed)
def teleport(self, x, y):
self.position.x = x
self.position.z = y
def turn(self, angle):
self.angle = self.angle + angle
def point(self, angle):
self.angle = angle
def draw(self, projection, camera, player_position, r):
self.prog.vars.mvp = projection * camera * self.modelview
self.prog.vars.playerLight = r*0.1 + 0.9
self.prog.vars.playerPosition = player_position.tuple()
self.prog.vars.modelview = self.modelview
with nested(self.prog, self.tex):
self.mesh.draw(GL_QUADS)
def hit(self):
self.health -= 10
def within(self, position):
position = position - self.position
for i in xfrange(0.0, 1.0, 0.05):
point = self.position + (position * i)
if self.collisionMap.collision(point, self.x/4):
return False
return True
def depth(self, proj_cam):
mat = proj_cam * self.modelview
vector = mat.col(3)
return -vector.z