Skip to content
Snippets Groups Projects
Commit e1b8c4dc authored by Jantz's avatar Jantz
Browse files

Enemy now faces the direction of the movement.

parent 24c39c42
No related branches found
No related tags found
No related merge requests found
"""This module contains the Enemy class."""
import pygame as pg
import math
class Enemy:
"""Class for the enemy object."""
def __init__(self, hp: float, speed: float, damage: float, texture_path: str):
def __init__(self, hp: float, speed: float, damage: float, full_path_time: float, texture_path: str):
"""Initializes the enemy object."""
self.hp = hp
self.speed = speed
......@@ -13,9 +14,10 @@ class Enemy:
self.texture = pg.image.load(self.texture_path).convert_alpha()
self.path_position = 0
self.screen_position = (0, 0)
self.full_path_time = 20
self.full_path_time = full_path_time
self.render_size = 64
def calculate_current_path_segment(self, path: tuple[tuple[int, int]]) -> int:
def calculate_current_path_segment(self, path: tuple[tuple[int, int]]) -> tuple[int, float]:
"""
Returns the index of current path segment and the distance along the path segment.
......@@ -39,6 +41,7 @@ class Enemy:
if current_path_length >= full_path_length:
# Return the end of the last path segment if the path_position more than 1
return len(path) - 2, 1
def move_along_path(self, dt: float, path: tuple[tuple[int, int]]):
"""
Moves the enemy along the path.
......@@ -56,22 +59,45 @@ class Enemy:
path[path_segment][1] + (path[path_segment + 1][1] - path[path_segment][1]) * segment_position
)
def draw(self, surface):
def face_movement_direction(self, path):
"""
Draws the enemy to the passed surface.
Rotates the enemy's texture to face the direction of movement.
Parameters:
path:
The waypoints of the path.
"""
# Get current path segment
path_segment, not_used = self.calculate_current_path_segment(path)
# Get the start and end positions of the current segment
current_segment_start = path[path_segment]
current_segment_end = path[path_segment + 1]
# Calculate the direction vector
dx = current_segment_end[0] - current_segment_start[0]
dy = current_segment_end[1] - current_segment_start[1]
# Calculate the angle in degrees
angle = -math.degrees(math.atan2(dy, dx))
# Rotate the texture
self.rotated_texture = pg.transform.rotate(self.texture, angle)
def draw(self, surface, path: tuple[tuple[int, int]]):
"""
Draws the enemy to the passed surface using the rotated texture.
Parameters:
surface:
The surface to draw the enemy to.
path:
The waypoints of the path.
"""
self.texture_rect = self.texture.get_rect()
self.face_movement_direction(path)
self.texture_rect = self.rotated_texture.get_rect()
self.texture_rect.center = self.screen_position
surface.blit(self.texture, self.texture_rect)
# TODO: Rotate the sprite to face the direction of movement.
surface.blit(self.rotated_texture, self.texture_rect)
class Zombie(Enemy):
"""Class for the zombie object."""
def __init__(self):
"""Initializes the zombie object."""
super().__init__(100, 1, 10,'assets/images/zombie/skeleton-idle_0.png')
super().__init__(100, 1, 10, 30, 'assets/images/zombie/zombie_128x128.png')
......@@ -82,7 +82,7 @@ class World:
"""Draws the world elements to the passed surface."""
self.window.blit(self.texture, (0, 0))
for enemy in self.enemies:
enemy.draw(self.window)
enemy.draw(self.window, self.waypoints)
self.turret_group.draw(self.texture)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment