summaryrefslogtreecommitdiff
path: root/src/sliceitoff/field
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-23 21:02:56 +0200
committerViljami Ilola <+@hix.fi>2024-03-23 21:02:56 +0200
commit947df85053af2704ce1a17daf1fab13e273f0cc5 (patch)
treeece2e9e807e74988b36ec5fac0de7dcc9b4efca2 /src/sliceitoff/field
parent87cd7293498d6b30389e056a59b2e69eb7b3a418 (diff)
remove cross dependencies. moving logic towards game/...
Diffstat (limited to 'src/sliceitoff/field')
-rw-r--r--src/sliceitoff/field/field.py33
1 files changed, 14 insertions, 19 deletions
diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py
index 842b07a..9e04b4f 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -47,17 +47,15 @@ class Field(pygame.sprite.LayeredUpdates):
super().__init__()
self.add(FieldSprite( (0, 0, *__class__.initial_area) ))
self.area_full = __class__.initial_area[0] * __class__.initial_area[1]
- self.area_current = self.area_full
-
- def update_area(self):
+ def calculate_current_area(self):
+ return sum( s.area[2]*s.area[3] for s in self.active_sprites() )
+
+ def update_stats(self):
""" calculates remaining area and remaining percentage """
- self.area_current = sum( s.area[2]*s.area[3] for s in self.sprites() )
- Stats.percent = 100 * self.area_current / self.area_full
+ Stats.percent = 100 * self.calculate_current_area() / self.area_full
if DEBUG:
- print(
- f"FIELD: {self.area_full}/{self.area_current}, "
- f"{Stats.percent}")
+ print(f"FIELD: {Stats.percent}")
def slice(
self,
@@ -67,13 +65,16 @@ class Field(pygame.sprite.LayeredUpdates):
""" Slice one area into two areas """
# Slicing hits the area?
- overlap = self.get_sprites_at(Scaling.scale_to_display(pos))
- if not overlap:
+ for overlap in self.get_sprites_at(Scaling.scale_to_display(pos)):
+ if not overlap.dead:
+ break
+ else:
return None
+
# Save the area information and remove the sprite
- ax, ay, aw, ah = overlap[0].area
- overlap[0].remove(self)
+ ax, ay, aw, ah = overlap.area
+ overlap.remove(self)
# create new areas if there is any space
if direction:
@@ -86,7 +87,6 @@ class Field(pygame.sprite.LayeredUpdates):
self.add(FieldSprite( (x1, ay, x2-x1, ah) ))
if x4 > x3:
self.add(FieldSprite( (x3, ay, x4-x3, ah) ))
- self.update_area()
self.add(SliceSprite( (x2, ay, x3-x2, ah) ))
return Scaling.area_to_rect((x2, ay, x3-x2, ah))
@@ -102,12 +102,11 @@ class Field(pygame.sprite.LayeredUpdates):
self.add(SliceSprite( (ax, y2, aw, y3-y2) ))
- self.update_area()
return Scaling.area_to_rect((ax, y2, aw, y3-y2))
def active_sprites(self):
""" Returns all sprites that are not dead """
- return (s for s in self.sprites() if not s.dead)
+ return [s for s in self.sprites() if not s.dead]
def kill_if_not_colliding(self, sprites):
""" If there is empty fields that are not yet dead kill them """
@@ -117,7 +116,3 @@ class Field(pygame.sprite.LayeredUpdates):
break
else:
field.dead = True
-
-# def update(self, dt):
-# """---"""
- \ No newline at end of file