Commit 0e92b759 authored by Andrew Okin's avatar Andrew Okin

Make enemies give resources.

parent 5fbbe301
......@@ -32,16 +32,19 @@ class EnemyBase(object):
# constant file path thing
IMAGE_PATH = "images"
def __init__(self, enemyIdName, health=100, speed=.1):
def __init__(self, enemyIdName, health=100, speed=.1, resources=10, load_images=True):
self.name = enemyIdName
self.default_health = health
self.default_speed = speed
self.images = (
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_1" + ".png")),
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_2" + ".png")),
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_3" + ".png")),
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_4" + ".png"))
)
self.default_resources = resources
if load_images:
# If this is false, the subclass probably wants to load its own images.
self.images = (
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_1" + ".png")),
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_2" + ".png")),
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_3" + ".png")),
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_4" + ".png"))
)
def getImage(self, direction):
if direction >= len(self.images) :
......@@ -58,4 +61,4 @@ class EnemyBase(object):
'''
entity.move(elapsed)
entity.applyDot(elapsed)
\ No newline at end of file
......@@ -20,6 +20,7 @@ class EnemyEntity(object):
loc_y
direction
alive
resources
dot_time_left
dot_damage
'''
......@@ -30,27 +31,28 @@ class EnemyEntity(object):
'''
self.speed = enemy_type.default_speed
self.health = enemy_type.default_health
self.resources = enemy_type.default_resources
self.enemy_type = enemy_type
def spawn(self, game_map, spritegroup):
def spawn(self, game, spritegroup):
'''
actually spawns the entity with the given type.
'''
start = game_map.getStartingTile()
self.game = game
start = game.map.getStartingTile()
self.loc_x = start[0]
self.loc_y = start[1]
self.path_index = 0
self.dot_time_left = 0
self.dot_damage = 0
self.alive = True
self.game_map = game_map
self.needs_dir_update = True
self.direction = 2
self.atEnd = False
# sprite stuff
self.sprite = pygame.sprite.Sprite()
self.size = game_map.getTileSize()
self.size = game.map.getTileSize()
self.sprite.rect = pygame.Rect(self.loc_x, self.loc_y, self.size[0], self.size[1])
self.sprite.image = self.enemy_type.getImage(2)
spritegroup.add(self.sprite)
......@@ -73,7 +75,7 @@ class EnemyEntity(object):
if self.needs_dir_update :
self.calcDirection()
self.sprite.image = self.enemy_type.getImage(self.direction-1)
self.sprite.image = pygame.transform.scale(self.sprite.image, self.game_map.getTileSize())
self.sprite.image = pygame.transform.scale(self.sprite.image, self.game.map.getTileSize())
deltaX = self.speed * enemybase.DIRECTION_MATRIX[self.direction][0];
deltaY = self.speed * enemybase.DIRECTION_MATRIX[self.direction][1];
self.loc_x += deltaX
......@@ -95,7 +97,7 @@ class EnemyEntity(object):
current = self.getPathLoc()
nextLoc = self.getNextLoc()
if self.game_map.getTileCoordinates((self.loc_x, self.loc_y)) == current :
if self.game.map.getTileCoordinates((self.loc_x, self.loc_y)) == current :
# its in the same place it was.. thats fine.
return
......@@ -105,26 +107,26 @@ class EnemyEntity(object):
adder = enemybase.DIRECTION_MATRIX[self.direction]
if self.direction == 4 or self.direction == 1:
adder = (adder[0] * .1, adder[1] * .1)
map_size = self.game_map.getTileSize()
map_size = self.game.map.getTileSize()
adder = (map_size[0] * adder[0], map_size[1] * adder[1])
adder = (self.loc_x + adder[0], self.loc_y + adder[1])
temp = self.game_map.getTileCoordinates(adder)
temp = self.game.map.getTileCoordinates(adder)
if temp != nextLoc:
self.path_index += 1
self.needs_dir_update = True
#print self.path_index
#print self.game_map.path[self.path_index]
#print self.game.map.path[self.path_index]
def getPathLoc(self):
return self.game_map.path[self.path_index]
return self.game.map.path[self.path_index]
def getNextLoc(self):
if len(self.game_map.path) <= self.path_index + 1 :
if len(self.game.map.path) <= self.path_index + 1 :
self.atEnd = True
return self.getPathLoc();
return self.game_map.path[self.path_index + 1]
return self.game.map.path[self.path_index + 1]
def getLoc(self):
return (self.loc_x, self.loc_y)
......@@ -141,6 +143,7 @@ class EnemyEntity(object):
if self.health <= 0 :
self.health = 0
self.alive = False
self.game.enemyKilled(self)
def dead(self):
......
......@@ -10,20 +10,19 @@ import pygame
class EnemyTank(enemybase.EnemyBase):
def __init__(self):
super(EnemyTank, self).__init__("tank", 100, 1)
super(EnemyTank, self).__init__("tank", health=100, speed=1)
class EnemyTruck(enemybase.EnemyBase):
def __init__(self):
super(EnemyTruck, self).__init__("truck", 50, 2)
super(EnemyTruck, self).__init__("truck", health=50, speed=2)
class EnemyUFO(enemybase.EnemyBase):
def __init__(self):
self.name = "alien"
self.default_health = 500
self.default_speed = 1
super(EnemyUFO, self).__init__("alien", health=500, speed=1, resources=50, load_images=False)
self.images = (
pygame.image.load(os.path.join(self.IMAGE_PATH, self.name + "_5" + ".png")),
)
def getImage(self, direction):
return self.images[0]
\ No newline at end of file
return self.images[0]
......@@ -91,22 +91,22 @@ class EnemyManager:
Updates the enemies_list and schedules waves, if necessary. This returns the number
of enemies_list that have hit the destination.
"""
def update(self, mapdata):
def update(self, game):
retval = 0
# Update the enemies_list
for curr in EnemyManager.enemies_list:
curr.update(pygame.time.get_ticks()-self.last_update_time)
if(curr.dead() or curr.offscreen(mapdata)):
if(curr.dead() or curr.offscreen(game.map)):
EnemyManager.enemies_list.remove(curr)
curr.sprite.kill() # Remove the sprite from the sprite group
if(curr.atDestination(mapdata)):
if(curr.atDestination(game.map)):
EnemyManager.enemies_list.remove(curr)
curr.sprite.kill()
retval += 1
self.last_update_time = pygame.time.get_ticks()
current_time = pygame.time.get_ticks()
# Spawn enemies_list that need to be spawned
start = mapdata.getStartingTile()
start = game.map.getStartingTile()
spawning = True
while(spawning and not EnemyManager.enemy_queue.empty()):
current_enemy = EnemyManager.enemy_queue.get()
......@@ -114,7 +114,7 @@ class EnemyManager:
EnemyManager.enemy_queue.put(current_enemy)
spawning = False
else:
current_enemy[1].spawn(mapdata, EnemyManager.spritegroup)
current_enemy[1].spawn(game, EnemyManager.spritegroup)
EnemyManager.enemies_list.append(current_enemy[1])
......@@ -141,10 +141,10 @@ class EnemyManager:
Given a pygame event from the event queue, spawn an enemy (if necessary).
The coordinates are the x and y coordinates of the starting tile (a tuple).
"""
def spawnEnemy(self, event, mapdata):
def spawnEnemy(self, event, game):
if(event.type == EnemyManager.SPAWN_EVENT_BASIC):
entity = enemies.enemyentity.EnemyEntity(EnemyManager.enemies_types[0])
entity.spawn(mapdata, EnemyManager.spritegroup)
entity.spawn(game.map, EnemyManager.spritegroup)
EnemyManager.enemies_list.append(entity)
# A little trick so we can run the game from here in IDLE
......
......@@ -228,7 +228,7 @@ class Game(object):
This updates enemies, towers, and other game logic stuff.
"""
# TODO: Make this not stupid.
livesLost = self.enemy_mgr.update(self.map)
livesLost = self.enemy_mgr.update(self)
self.data.lives -= livesLost
self.tower_mgr.update()
......@@ -263,6 +263,14 @@ class Game(object):
self.screen_surface.blit(self.ui_surface, (0, 0))
def enemyKilled(self, entity):
"""
Called when an enemy is killed.
"""
# TODO: Maybe use events for this?
self.data.resources += entity.resources
def handleEvent(self, event):
if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
self.handleKeyEvent(event)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment