2023-05-17 11:52:05 +00:00
|
|
|
import pygame
|
2023-05-17 12:42:21 +00:00
|
|
|
import uuid
|
2023-05-18 03:39:12 +00:00
|
|
|
import random
|
2023-05-17 11:52:05 +00:00
|
|
|
|
2023-05-17 13:05:04 +00:00
|
|
|
from globals import *
|
2023-05-18 08:03:23 +00:00
|
|
|
from utilities import *
|
2023-05-18 05:30:15 +00:00
|
|
|
from player import *
|
|
|
|
from blocks import *
|
2023-05-17 11:52:05 +00:00
|
|
|
|
2023-05-18 03:39:12 +00:00
|
|
|
def findClosestPlayer(x, y):
|
|
|
|
closestPlayerPosition = {"difference": (1,1), "distance": 9999}
|
|
|
|
for id, player in players.items():
|
|
|
|
difference = (player.x - x, player.y - y)
|
|
|
|
distance = max(abs(difference[0]), abs(difference[1]))
|
|
|
|
if distance > closestPlayerPosition["distance"]:
|
|
|
|
continue
|
2023-05-18 08:03:23 +00:00
|
|
|
closestPlayerPosition = {"difference": difference, "distance": distance, "player": player}
|
2023-05-18 03:39:12 +00:00
|
|
|
return closestPlayerPosition
|
|
|
|
|
2023-05-17 11:52:05 +00:00
|
|
|
class Enemy:
|
2023-05-17 13:05:04 +00:00
|
|
|
def __init__(self, id, x, y):
|
2023-05-17 12:42:21 +00:00
|
|
|
self.id = id
|
2023-05-18 07:35:26 +00:00
|
|
|
self.ENEMY = True
|
2023-05-17 11:52:05 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
2023-05-18 07:35:26 +00:00
|
|
|
self.renderPos = (x,y)
|
2023-05-18 08:57:15 +00:00
|
|
|
# self.AITime = random.randint(350, 600)
|
|
|
|
self.AITime = random.randint(10, 50)
|
|
|
|
self.deathColor = white
|
2023-05-17 13:05:04 +00:00
|
|
|
|
2023-05-17 12:42:21 +00:00
|
|
|
def die(self):
|
2023-05-17 13:05:04 +00:00
|
|
|
del enemies[self.id]
|
2023-05-17 12:42:21 +00:00
|
|
|
|
2023-05-17 11:52:05 +00:00
|
|
|
def pushed(self, x, y, caller, pusher):
|
2023-05-18 05:30:15 +00:00
|
|
|
if hasattr(pusher, "PLAYER") and hasattr(caller, "BLOCK"):
|
2023-05-18 08:03:23 +00:00
|
|
|
if checkOOBounds((self.x + x, self.y + y)) or hasattr(gameMap[self.x + x][self.y + y], "BLOCK"):
|
2023-05-18 05:30:15 +00:00
|
|
|
pusher.kills += 1
|
2023-05-18 08:57:15 +00:00
|
|
|
caller.color = self.deathColor
|
2023-05-17 12:42:21 +00:00
|
|
|
self.die()
|
|
|
|
return
|
2023-05-17 13:05:04 +00:00
|
|
|
raise Exception("Not crushing enemy!")
|
|
|
|
|
|
|
|
def move(self, x, y):
|
2023-05-18 03:39:12 +00:00
|
|
|
try:
|
2023-05-18 08:03:23 +00:00
|
|
|
if checkOOBounds((self.x + x, self.y + y)):
|
2023-05-18 03:39:12 +00:00
|
|
|
raise Exception("Cannot move off edge!")
|
|
|
|
if gameMap[self.x + x][self.y + y]:
|
2023-05-18 05:30:15 +00:00
|
|
|
if hasattr(gameMap[self.x + x][self.y + y], "PLAYER"):
|
2023-05-18 03:39:12 +00:00
|
|
|
gameMap[self.x + x][self.y + y].die()
|
|
|
|
else:
|
|
|
|
raise Exception("Path is blocked.")
|
2023-05-18 04:34:16 +00:00
|
|
|
gameMap[self.x][self.y] = False
|
2023-05-18 03:39:12 +00:00
|
|
|
self.x += x
|
|
|
|
self.y += y
|
|
|
|
gameMap[self.x][self.y] = self
|
|
|
|
except Exception as error:
|
|
|
|
raise error
|
|
|
|
|
|
|
|
def runAI(self):
|
|
|
|
self.AITime -= 1
|
|
|
|
if self.AITime < 1:
|
|
|
|
try:
|
2023-05-18 09:40:51 +00:00
|
|
|
diff = findClosestPlayer(self.x, self.y)["difference"]
|
|
|
|
weightsx = [-diff[0], abs(diff[1]), diff[0]]
|
2023-05-18 03:39:12 +00:00
|
|
|
if weightsx[0] < 0:
|
|
|
|
weightsx[0] = weightsx[2]/6
|
|
|
|
elif weightsx[2] < 0:
|
|
|
|
weightsx[2] = weightsx[0]/6
|
|
|
|
else:
|
|
|
|
weightsx[0] = weightsx[1]/8
|
|
|
|
weightsx[2] = weightsx[1]/8
|
2023-05-18 09:40:51 +00:00
|
|
|
weightsy = [-diff[1], abs(diff[0]), diff[1]]
|
2023-05-18 03:39:12 +00:00
|
|
|
if weightsy[0] < 0:
|
|
|
|
weightsy[0] = weightsy[2]/6
|
|
|
|
elif weightsy[2] < 0:
|
|
|
|
weightsy[2] = weightsy[0]/6
|
|
|
|
else:
|
|
|
|
weightsy[0] = weightsy[1]/8
|
|
|
|
weightsy[2] = weightsy[1]/8
|
|
|
|
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
|
|
|
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
|
|
|
self.move(movementx, movementy)
|
|
|
|
self.AITime = random.randint(150, 200)
|
|
|
|
except Exception as error:
|
2023-05-18 08:57:15 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
class Smart(Enemy):
|
|
|
|
def __init__(self, id, x, y):
|
|
|
|
Enemy.__init__(self, id, x, y)
|
|
|
|
self.deathColor = red
|
|
|
|
self.trackedPlayer = False
|
|
|
|
self.trackingCounter = 0
|
|
|
|
|
|
|
|
def runAI(self):
|
|
|
|
self.AITime -= 1
|
|
|
|
if self.AITime < 1:
|
|
|
|
try:
|
2023-05-18 09:40:51 +00:00
|
|
|
if self.trackingCounter < 1:
|
|
|
|
self.AITime = random.randint(150, 250)
|
|
|
|
self.trackingCounter = 9
|
|
|
|
self.trackedPlayer = False
|
|
|
|
elif self.trackedPlayer and self.trackedPlayer["player"].alive:
|
|
|
|
pathMap = self.trackedPlayer["player"].pathMap
|
|
|
|
weightsx = [1,1,1]
|
|
|
|
weightsy = [1,1,1]
|
|
|
|
if pathMap[self.x][self.y]:
|
|
|
|
fastDir = pathMap[self.x][self.y][2]
|
|
|
|
weightsx[1 + fastDir[0]] = 30
|
|
|
|
weightsy[1 + fastDir[1]] = 30
|
|
|
|
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
|
|
|
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
|
|
|
self.move(movementx, movementy)
|
2023-05-18 08:57:15 +00:00
|
|
|
self.trackingCounter -= 1
|
|
|
|
self.AITime = random.randint(10, 15)
|
|
|
|
else:
|
|
|
|
closestPlayer = findClosestPlayer(self.x, self.y)
|
2023-05-18 09:40:51 +00:00
|
|
|
self.trackedPlayer = closestPlayer
|
2023-05-18 08:57:15 +00:00
|
|
|
self.AITime = random.randint(10, 15)
|
|
|
|
except Exception as error:
|
2023-05-18 09:40:51 +00:00
|
|
|
pass
|