Modifica il sistema di rendering SDL e migliora la gestione delle unità nel gioco

This commit is contained in:
2024-12-15 18:56:51 +01:00
parent 0ad9cd47c6
commit 3ccffdca7d
5 changed files with 95 additions and 28 deletions
Binary file not shown.
+21 -22
View File
@@ -1,7 +1,6 @@
from .unit import Unit
import random
import uuid
from engine.tkinter import GameWindow
# Costanti
AGE_THRESHOLD = 200
@@ -55,16 +54,14 @@ class Rat(Unit):
self.age += 1
if self.age == AGE_THRESHOLD:
self.speed *= SPEED_REDUCTION
if hasattr(self, "pregnant"):
if self.pregnant:
self.procreate()
if getattr(self, "pregnant", False):
self.procreate()
if self.stop:
self.stop -= 1
return
if self.partial_move < 1:
self.partial_move += self.speed
self.partial_move = round(self.partial_move, 2)
if self.partial_move == 1:
self.partial_move = round(self.partial_move + self.speed, 2)
if self.partial_move >= 1:
self.partial_move = 0
self.position = self.find_next_position()
self.direction = self.calculate_rat_direction()
@@ -72,27 +69,29 @@ class Rat(Unit):
def collisions(self):
if self.age < AGE_THRESHOLD:
return
units = self.game.units.copy().values()
units = []
units.extend(self.game.unit_positions.get(self.position, []))
units.extend(self.game.unit_positions.get(self.position_before, []))
units.extend(self.game.unit_positions_before.get(self.position, []))
units.extend(self.game.unit_positions_before.get(self.position_before, []))
for unit in units:
if unit.id == self.id:
continue
if unit.age < AGE_THRESHOLD:
if unit.id == self.id or unit.age < AGE_THRESHOLD:
continue
x1, y1, x2, y2 = self.bbox
ox1, oy1, ox2, oy2 = unit.bbox
# Verifica se c'è collisione con una tolleranza di sovrapposizione
if not (x1 < ox2 - OVERLAP_TOLERANCE and
x2 > ox1 + OVERLAP_TOLERANCE and
y1 < oy2 - OVERLAP_TOLERANCE and
y2 > oy1 + OVERLAP_TOLERANCE):
continue
if self.id in self.game.units and unit.id in self.game.units:
if self.sex == unit.sex and self.fight:
self.die(unit)
elif self.sex != unit.sex:
if "fuck" in dir(self):
self.fuck(unit)
if (x1 < ox2 - OVERLAP_TOLERANCE and
x2 > ox1 + OVERLAP_TOLERANCE and
y1 < oy2 - OVERLAP_TOLERANCE and
y2 > oy1 + OVERLAP_TOLERANCE):
if self.id in self.game.units and unit.id in self.game.units:
if self.sex == unit.sex and self.fight:
self.die(unit)
elif self.sex != unit.sex:
if "fuck" in dir(self):
self.fuck(unit)
def die(self, unit=None):
if not unit: