summaryrefslogtreecommitdiff
path: root/src/sliceitoff/field
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-16 12:35:07 +0200
committerViljami Ilola <+@hix.fi>2024-03-16 12:35:07 +0200
commita23b6647b0ca2cd9f2ccbbe12360c3fa0f96a554 (patch)
treebab154622f9767fd340278ab275babb76fc569d5 /src/sliceitoff/field
parent1ad8357f0039292b929e66cf703b4314660aded2 (diff)
scaling as class
Diffstat (limited to 'src/sliceitoff/field')
-rw-r--r--src/sliceitoff/field/field.py51
1 files changed, 35 insertions, 16 deletions
diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py
index c7ebf81..0681c64 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -10,21 +10,12 @@ class FieldSprite(pygame.sprite.Sprite):
class Field():
- def __init__(self, scaling: Scaling = None):
- self.scaling = scaling if scaling else Scaling(50,0,0)
+ def __init__(self):
self.sprites = pygame.sprite.Group()
self.updated = True
self.areas = [(0,0,40_000,28_000)]
- def area_to_rect(self, area):
- x, y, w, h = area
- return pygame.Rect(
- x * self.scaling.scale + self.scaling.left,
- y * self.scaling.scale + self.scaling.top,
- w * self.scaling.scale,
- h * self.scaling.scale)
-
- def coordinates_inside_area(self, area, x, y):
+ def coordinates_inside_area(self, area, x, y) -> bool:
if x < area[0]:
return False
if y < area[1]:
@@ -34,24 +25,52 @@ class Field():
if y >= area[1]+area[3]:
return False
return True
-
- def slice(self, x: int, y: int, direction: bool, thickness: int):
+
+ 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)
- return False
+ 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(self.area_to_rect(area)))
+ self.sprites.add(FieldSprite(Scaling.area_to_rect(area)))
+ self.updated = False
- def get_sprites(self):
+ def get_sprites(self) -> pygame.sprite.Group:
if self.updated:
self.__update_sprites()
return self.sprites