Added alot of things, waypoints, grid for editor, game camera system, enemy moving on pre defined waypoint (path)
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
from game.util import *
|
||||
import pygame as pg
|
||||
|
||||
class Enemy(pg.sprite.Group):
|
||||
def __init__(self, game, speed, health):
|
||||
super().__init__()
|
||||
self.game = game
|
||||
self.waypoints = self.game.tilemap.waypoints
|
||||
print(self.waypoints)
|
||||
|
||||
self.speed = speed
|
||||
self.health = health
|
||||
|
||||
self.image = self.game.assets['enemy']
|
||||
self.rect = self.image.get_rect()
|
||||
|
||||
self.pos = pg.Vector2(
|
||||
tile_to_pixel(self.waypoints[0], self.game.tilemap.tile_size, True)
|
||||
)
|
||||
|
||||
self.target_index = 1
|
||||
|
||||
self.rect.center = self.pos
|
||||
|
||||
def update(self):
|
||||
if self.target_index >= len(self.waypoints):
|
||||
return
|
||||
|
||||
tile_size = self.game.tilemap.tile_size
|
||||
|
||||
target = pg.Vector2(
|
||||
tile_to_pixel(
|
||||
self.waypoints[self.target_index],
|
||||
tile_size,
|
||||
True
|
||||
)
|
||||
)
|
||||
|
||||
direction = target - self.pos
|
||||
distance = direction.length()
|
||||
|
||||
if distance <= self.speed:
|
||||
self.pos = target
|
||||
self.target_index += 1
|
||||
else:
|
||||
direction = direction.normalize()
|
||||
self.pos += direction * self.speed
|
||||
|
||||
self.rect.center = self.pos
|
||||
|
||||
def render(self, surf, camera):
|
||||
zoom = camera.zoom
|
||||
scaled_rect = self.rect.scale_by(zoom)
|
||||
|
||||
scaled_image = pg.transform.scale(
|
||||
self.image,
|
||||
(scaled_rect.size[0], scaled_rect.size[1])
|
||||
)
|
||||
|
||||
scaled_pos = camera.world_to_screen(self.pos)
|
||||
scaled_rect.center = scaled_pos
|
||||
|
||||
surf.blit(scaled_image, scaled_rect)
|
||||
Reference in New Issue
Block a user