You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.5 KiB

from .unit import Unit
from .points import Point
import random
import uuid
# Costanti
AGE_THRESHOLD = 200
SPEED_REDUCTION = 0.5
PREGNANCY_DURATION = 500
BABY_INTERVAL = 50
class Rat(Unit):
def __init__(self, game, position=(0,0), id=None):
super().__init__(position)
self.id = id if id else uuid.uuid4()
self.game = game
self.position = self.find_next_position()
self.bbox = (0, 0, 0, 0)
self.stop = 0
self.age = 0
self.speed = .10
self.partial_move = 0
self.position_before = position
self.fight = False
def calculate_rat_direction(self):
x, y = self.position
x_before, y_before = self.position_before
if x > x_before:
return "RIGHT"
elif x < x_before:
return "LEFT"
elif y > y_before:
return "DOWN"
elif y < y_before:
return "UP"
else:
return "DOWN"
def find_next_position(self):
neighbors = []
x, y = self.position
while not neighbors:
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx, ny = x + dx, y + dy
if not self.game.map.matrix[ny][nx] and not (nx, ny) == self.position_before:
neighbors.append((nx, ny))
if not neighbors:
self.position_before = self.position
self.position_before = self.position
return neighbors[random.randint(0, len(neighbors) - 1)]
def move(self):
self.age += 1
if self.age == AGE_THRESHOLD:
self.speed *= SPEED_REDUCTION
if getattr(self, "pregnant", False):
self.procreate()
if self.stop:
self.stop -= 1
return
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()
def collisions(self):
OVERLAP_TOLERANCE = self.game.cell_size // 4
if self.age < AGE_THRESHOLD:
return
units = []
units.extend(self.game.unit_positions.get(self.position_before, []))
for unit in units:
if unit.id == self.id or unit.age < AGE_THRESHOLD or self.position != unit.position_before:
continue
x1, y1, x2, y2 = self.bbox
ox1, oy1, ox2, oy2 = unit.bbox
# Verifica se c'è collisione con una tolleranza di sovrapposizione
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, score=10):
if not unit:
unit = self
self.game.units.pop(unit.id)
self.game.spawn_unit(Point, unit.position_before, value=score)
def draw(self):
start_perf = self.game.engine.get_perf_counter()
direction = self.calculate_rat_direction()
sex = self.sex if self.age > AGE_THRESHOLD else "BABY"
image = self.game.rat_assets[sex][direction]
image_size = self.game.engine.get_image_size(image)
self.rat_image = image
partial_x, partial_y = 0, 0
if direction in ["UP", "DOWN"]:
partial_y = self.partial_move * self.game.cell_size * (1 if direction == "DOWN" else -1)
else:
partial_x = self.partial_move * self.game.cell_size * (1 if direction == "RIGHT" else -1)
x_pos = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
self.bbox = (x_pos, y_pos, x_pos + image_size[0], y_pos + image_size[1])
#self.game.engine.draw_rectangle(self.bbox[0], self.bbox[1], self.bbox[2] - self.bbox[0], self.bbox[3] - self.bbox[1], "unit")
class Male(Rat):
def __init__(self, map, position=(0,0), id=None):
super().__init__(map, position, id)
self.sex = "MALE"
def fuck(self, unit):
if not unit.pregnant:
self.game.play_sound("SEX.WAV")
self.stop = 100
unit.stop = 200
unit.pregnant = PREGNANCY_DURATION
unit.babies = random.randint(1, 3)
class Female(Rat):
def __init__(self, map, position=(0,0), id=None):
self.sex = "FEMALE"
self.pregnant = False
self.babies = 0
super().__init__(map, position, id)
def procreate(self):
self.pregnant -= 1
if self.pregnant == self.babies * BABY_INTERVAL:
self.babies -= 1
self.stop = 20
if self.partial_move > 0.2:
self.game.new_rat(self.position)
else:
self.game.new_rat(self.position_before)
self.game.play_sound("BIRTH.WAV")