SIP/core/agent.py
2021-06-10 21:23:58 +02:00

104 lines
3.5 KiB
Python

import os
import pygame
from core.piece import Piece
class Agent(Piece):
def __init__(self,x_y, square_size):
self.name = "Agent"
self.col = x_y[0]
self.row = x_y[1]
self.angle = 0
self.current_angle = 0
self.moving = 0
self.rotating = 0
self.move_to = (self.row, self.col)
self.points = 0
self.img = pygame.image.load(r'core' + os.path.sep + 'resources' + os.path.sep +
'agent' + os.path.sep + 'new_detective.png')
self.square_size = square_size
self.img = pygame.transform.scale(self.img, (int(self.square_size[0]), int(self.square_size[1])))
def draw(self, win):
x = (self.col + 0.5 + (self.move_to[0] - self.col)/10*self.moving)*self.square_size[0]
y = (self.row + 0.5 + (self.move_to[1] - self.row)/10*self.moving)*self.square_size[1]
#win.blit(self.img, (x, y))
self.blitRotate(win, self.img, (x, y), (self.square_size[0]/2, self.square_size[1]/2), self.angle*90 + self.rotating*90/10)
def rotate(self, angle):
if self.moving == self.rotating == 0:
self.rotating += angle
#print(self.rotating)
def move(self):
if self.moving == self.rotating == 0:
move_to = (0,-1)
if self.angle == 1: move_to = (-1,0)
if self.angle == 2: move_to = (0,1)
if self.angle == 3: move_to = (1,0)
self.move_to = (self.col + move_to[0], self.row + move_to[1])
self.moving += 1
def update_animation(self):
if self.moving > 0:
self.moving += 1
if self.moving >= 10:
self.moving = 0
self.col, self.row = self.move_to
if self.rotating > 0:
self.rotating += 1
if self.rotating >= 10:
self.rotating = 0
self.angle = (self.angle + 1) % 4
if self.rotating < 0:
self.rotating -= 1
if self.rotating <= -10:
self.rotating = 0
self.angle = (self.angle - 1) % 4
def blitRotate(self, surf, image, pos, originPos, angle):
# calcaulate the axis aligned bounding box of the rotated image
w, h = image.get_size()
box = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
box_rotate = [p.rotate(angle) for p in box]
min_box = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
max_box = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
# calculate the translation of the pivot
pivot = pygame.math.Vector2(originPos[0], -originPos[1])
pivot_rotate = pivot.rotate(angle)
pivot_move = pivot_rotate - pivot
# calculate the upper left origin of the rotated image
origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])
# get a rotated image
rotated_image = pygame.transform.rotate(image, angle)
# rotate and blit the image
surf.blit(rotated_image, origin)
# draw rectangle around the image
#pygame.draw.rect(surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)