-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.py
43 lines (34 loc) · 1.33 KB
/
camera.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
import pygame
from stgs import *
class Cam:
def __init__(self, game, width, height):
self.camera = pygame.Rect(0, 0, width, height)
self.width = width
self.height = height
self.limit = CAMLIMIT
self.game = game
self.target = game.player
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def applyRect(self, rect):
return rect.move(self.camera.topleft)
def toggleTarget(self):
if self.target == self.game.player:
self.target = self.game.level.door
else:
self.target = self.game.player
def update(self):
self.width, self.height = self.game.map.floor.room.width, self.game.map.floor.room.height
x = -self.target.rect.centerx + int(winWidth / 2)
y = -self.target.rect.centery + int(winHeight / 2)
# limit scrolling to map size
if self.limit:
x = min(0, x) # left
y = min(0, y) # top
x = max(-(self.width - winWidth), x) # right
y = max(-(self.height - winHeight), y) # bottom
if self.width < winWidth:
x = (winWidth/2 - self.width/2)
if self.height < winHeight:
y = (winHeight/2 - self.height/2)
self.camera = pygame.Rect(x, y, self.width, self.height)