summaryrefslogtreecommitdiff
path: root/src/sliceitoff/player/player.py
blob: 6e2ab0bff3cf245deb7cd175991c88509fe34e37 (plain)
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
import os
import pygame

from display import Scaling
from images import Images

DEBUG = os.getenv("DEBUG")

class PlayerSprite(pygame.sprite.Sprite):
    def __init__(self, image, pos):
        super().__init__()
        self.image = image
        x, y = self.image.get_size()
        x = pos[0] - x // 2
        y = pos[1] - y // 2
        self.rect = self.image.get_rect().move(x,y)

class Player():
    def __init__(self, field = None, enemies = None):
        self.field = field
        self.enemies = enemies
        self.position = (200_000, 140_000)
        self.direction = False
        self.lazer = False
        self.sprites = pygame.sprite.Group()
        self.updated = True

    def __update_sprites(self):
        self.sprites.empty()
        image = Images.surfaces['player_00']
        if self.direction:
             image = pygame.transform.rotate(image, 90)
        self.sprites.add(PlayerSprite(
                image,
                Scaling.scale_to_display(self.position) ))
        if not self.lazer:
            self.updated = False
        
    def set_position(self, pos):
        if not self.lazer:
            self.position = Scaling.scale_to_internal(pos)
        self.updated = True
        
    def update(self, dt):
        """ Update sprites basis of dt. dt = milliseconds from last update """
        if self.updated:
            self.__update_sprites()
        
    def fire_lazer(self):
        if DEBUG:
            print("PLAYER: fire lazer")
        # self.lazer = True
        # self.updated = True
        hitbox = self.field.slice(self.position, self.direction, 4_500)
        hit = False
        if hitbox is not None:
            for enemy in self.enemies.sprites:
                if hitbox.colliderect(enemy.rect):
                    hit = True
                    break
        self.field.kill_if_not_colliding(self.enemies.sprites)
        return hit

    def set_direction(self):
        if not self.lazer:
            self.direction = not self.direction
            self.updated = True