summaryrefslogtreecommitdiff
path: root/src/sliceitoff/field/field.py
blob: 0681c644197cfb250eca6e02ea431aae8a904d66 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
import pygame
from display import Scaling

class FieldSprite(pygame.sprite.Sprite):
    def __init__(self, rect: pygame.Rect):
        super().__init__()
        self.rect = rect
        self.image = pygame.Surface(self.rect.size)
        self.image.fill("green")


class Field():
    def __init__(self):
        self.sprites = pygame.sprite.Group()
        self.updated = True
        self.areas = [(0,0,40_000,28_000)]
        
    def coordinates_inside_area(self, area, x, y) -> bool:
        if x < area[0]:
            return False
        if y < area[1]:
            return False
        if x >= area[0]+area[2]:
            return False
        if y >= area[1]+area[3]:
            return False
        return True

    def slice(self, x: int, y: int, direction: bool, thickness: int) -> bool:
        """ Slice one area into two areas """

        # Find the overlapping area
        for area in self.areas:
            if self.coordinates_inside_area(area, x, y):
                break
        else:
            return False

        # remove current area
        self.areas.remove(area)
        ax, ay, aw, ah = area

        # create new areas if there is any space
        if direction:
            x1 = ax
            x2 = x - thickness
            x3 = x + thickness
            x4 = ax + aw
            
            if x2 > x1:
                self.areas.append( (x1, ay, x2-x1, ah) )
            if x4 > x3:
                self.areas.append( (x3, ay, x4-x3, ah) )
        else:
            y1 = ay
            y2 = y - thickness
            y3 = y + thickness
            y4 = ay + ah
            
            if y2 > y1:
                self.areas.append( (ax, y1, aw, y2-y1) )
            if y4 > y3:
                self.areas.append( (ax, y3, aw, y4-y3) )
        self.updated = True
        return True

    def __update_sprites(self):
        self.sprites.empty()
        for area in self.areas:
            self.sprites.add(FieldSprite(Scaling.area_to_rect(area)))
        self.updated = False
        
    def get_sprites(self) -> pygame.sprite.Group:
        if self.updated:
            self.__update_sprites()
        return self.sprites
        
    def __del__(self):
        pass