-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
105 lines (80 loc) · 2.66 KB
/
camera.py
File metadata and controls
105 lines (80 loc) · 2.66 KB
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
import pyglet
from drawable import Drawable
from pyglet.window import key
from pyglet.gl import *
import sys
class Camera(Drawable):
zoom = 2
hud_zoom = 2
speed = 6
def __init__(self, game):
self.game = game
self.speed = 125
self.width = self.game.window.width
self.height = self.game.window.height
self.x = self.width // 4
self.y = self.height // 4
self.vx = 0
self.vy = 0
self.last_cull = None
self.target = None
def reset(self):
self.zoom = 2 if self.game.world.state == self.game.world.OVERWORLD else 3
glLoadIdentity()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
def apply(self):
self.reset()
cx, cy = self.x, self.y
if self.game.world.state == self.game.world.DUNGEON:
cx += self.game.dungeon_offset
cy += self.game.dungeon_offset
glTranslatef(-int(cx*self.zoom)+self.width//2, -int(cy*self.zoom)+self.height//2, 0)
glScaled(self.zoom, self.zoom, 1)
def apply_hud(self):
self.reset()
glScaled(self.hud_zoom, self.hud_zoom, 1)
def move_to_target(self):
self.x = self.target.x
self.y = self.target.y
def update(self, dt):
dx = 0
dy = 0
cx, cy = self.x, self.y
if self.game.world.state == self.game.world.DUNGEON:
cx += self.game.dungeon_offset
cy += self.game.dungeon_offset
if self.target:
dx = (self.target.x - cx) * dt
dy = (self.target.y - cy) * dt
self.x += dx
self.y += dy
cx, cy = self.x, self.y
min_x = self.width // 2 / self.zoom
min_y = self.height // 2 / self.zoom
max_x = self.game.world.WIDTH[self.game.world.state] * self.game.TILE_WIDTH - self.width // 2 / self.zoom
max_y = self.game.world.HEIGHT[self.game.world.state] * self.game.TILE_HEIGHT - self.height // 2 / self.zoom
if cx + dx < min_x:
self.x = cx = min_x
if cx + dx >= max_x:
self.x = cx = max_x
if cy + dy < min_y:
self.y = cy = min_y
if cy + dy >= max_y:
self.y = cy = max_y
self.vx *= 0.92
self.vy *= 0.92
#if self.game.keys[key.W]:
# self.vy = self.vy + (self.speed / self.zoom - self.vy) * 0.6
#elif self.game.keys[key.S]:
# self.vy = self.vy + (-self.speed / self.zoom - self.vy) * 0.6
#if self.game.keys[key.D]:
# self.vx = self.vx + (self.speed / self.zoom - self.vx) * 0.6
#elif self.game.keys[key.A]:
# self.vx = self.vx + (-self.speed / self.zoom - self.vx) * 0.6
if self.game.keys[key.UP]:
self.zoom *= 1.1
elif self.game.keys[key.DOWN]:
self.zoom *= 0.9
if self.game.world.state == self.game.world.DUNGEON:
cx += self.game.dungeon_offset
cy += self.game.dungeon_offset