enemymanager.py 4.67 KB
Newer Older
1
"""
AbrarSyed's avatar
AbrarSyed committed
2
This module handles the spawning of enemies_list.
3 4 5
"""

import pygame
AbrarSyed's avatar
AbrarSyed committed
6
import enemies.enemybase
AbrarSyed's avatar
AbrarSyed committed
7
import enemies.enemytypes
8
import enemies.enemyentity
Elethier's avatar
Elethier committed
9
import Queue
10 11 12 13 14 15 16 17 18 19 20 21 22 23



class EnemyManager:

    """
    The event ID for a event to spawn a basic enemy. When a
    basic enemy is scheduled to spawn, an event with this ID
    is posted to the event queue.
    """
    SPAWN_EVENT_BASIC = pygame.USEREVENT

    """
    The time (in milliseconds) at which the last spawning of
AbrarSyed's avatar
AbrarSyed committed
24
    a wave of enemies_list occurred.
25 26
    """
    last_wave_time = 0
AbrarSyed's avatar
AbrarSyed committed
27 28 29 30 31
    
    """
    The number of the waves spawned
    """
    wave_number = 0
32 33 34 35

    """
    The amount of time between waves.
    """
AbrarSyed's avatar
AbrarSyed committed
36
    wave_interval = 10000
37 38

    """
AbrarSyed's avatar
AbrarSyed committed
39
    The number of basic enemies_list to spawn during a given wave.
40
    """
AbrarSyed's avatar
AbrarSyed committed
41
    basic_enemies = 3
42 43

    """
AbrarSyed's avatar
AbrarSyed committed
44 45
    The time between spawning two enemies_list during a wave. This
    is so that enemies_list don't overlap on the screen.
46
    """
AbrarSyed's avatar
AbrarSyed committed
47
    spawn_interval = 700
48 49

    """
AbrarSyed's avatar
AbrarSyed committed
50
    The list of enemies_list in the game.
51
    """
AbrarSyed's avatar
AbrarSyed committed
52
    enemies_list = []
53 54 55 56 57
    
    """
    enemy typs list
    """
    enemies_types = [
AbrarSyed's avatar
AbrarSyed committed
58 59 60
                     enemies.enemytypes.EnemyTank(),
                     enemies.enemytypes.EnemyTruck(),
                     enemies.enemytypes.EnemyUFO(),
61
                     ]
62 63

    """
Elethier's avatar
Elethier committed
64
    The time at which the last update occurred.
65
    """
Elethier's avatar
Elethier committed
66
    last_update_time = pygame.time.get_ticks()
67 68

    """
AbrarSyed's avatar
AbrarSyed committed
69
    The sprites of all enemies_list in the game.
70
    """
Elethier's avatar
Elethier committed
71
    spritegroup = pygame.sprite.Group()
72

Elethier's avatar
Elethier committed
73
    """
AbrarSyed's avatar
AbrarSyed committed
74
    The priority queue of enemies_list to be spawned. This queue is arranged
Elethier's avatar
Elethier committed
75 76 77 78 79 80 81 82 83
    on the scheduled time for an enemy to spawn. 
    """
    enemy_queue = Queue.PriorityQueue()

    """
    The last time something was spawned.
    """
    last_spawn_time = pygame.time.get_ticks()

Elethier's avatar
Elethier committed
84
    def __init__(self, size):
85
        self.last_wave_time = pygame.time.get_ticks()
Elethier's avatar
Elethier committed
86 87 88
        self.last_update_time = pygame.time.get_ticks()
        self.size = size
        
89

Elethier's avatar
Elethier committed
90
    """
AbrarSyed's avatar
AbrarSyed committed
91 92
    Updates the enemies_list and schedules waves, if necessary. This returns the number
    of enemies_list that have hit the destination.
Elethier's avatar
Elethier committed
93
    """
94
    def update(self, game):
Elethier's avatar
Elethier committed
95
        retval = 0
AbrarSyed's avatar
AbrarSyed committed
96 97
        # Update the enemies_list
        for curr in EnemyManager.enemies_list:
98
            curr.update(pygame.time.get_ticks()-self.last_update_time)
99
            if(curr.dead() or curr.offscreen(game.map)):
100
                EnemyManager.enemies_list.remove(curr)
Elethier's avatar
Elethier committed
101
                curr.sprite.kill() # Remove the sprite from the sprite group
102
            if(curr.atDestination(game.map)):
AbrarSyed's avatar
AbrarSyed committed
103
                EnemyManager.enemies_list.remove(curr)
Elethier's avatar
Elethier committed
104
                curr.sprite.kill()
Elethier's avatar
Elethier committed
105
                retval += 1
Elethier's avatar
Elethier committed
106 107
        self.last_update_time = pygame.time.get_ticks()
        current_time = pygame.time.get_ticks()
AbrarSyed's avatar
AbrarSyed committed
108
        # Spawn enemies_list that need to be spawned
109
        start = game.map.getStartingTile()
Elethier's avatar
Elethier committed
110 111 112 113 114 115 116
        spawning = True
        while(spawning and not EnemyManager.enemy_queue.empty()):
            current_enemy = EnemyManager.enemy_queue.get()
            if(current_enemy[0] > current_time): # Then we don't need to spawn yet
                EnemyManager.enemy_queue.put(current_enemy)
                spawning = False
            else:
117
                current_enemy[1].spawn(game, EnemyManager.spritegroup)
AbrarSyed's avatar
AbrarSyed committed
118
                EnemyManager.enemies_list.append(current_enemy[1])
Elethier's avatar
Elethier committed
119 120 121

        
        # If enough time has passed, and we're not spawning a wave, spawn a wave
Elethier's avatar
Elethier committed
122
        if(current_time - EnemyManager.last_wave_time >= EnemyManager.wave_interval):
123
            # Spawn a wave!
Elethier's avatar
Elethier committed
124
            EnemyManager.last_wave_time = current_time
125
            for index in range(0, EnemyManager.basic_enemies):
AbrarSyed's avatar
AbrarSyed committed
126
                new_enemy = enemies.enemyentity.EnemyEntity(EnemyManager.enemies_types[EnemyManager.wave_number % 3])
Elethier's avatar
Elethier committed
127 128
                scheduled_time = index*EnemyManager.spawn_interval+current_time
                EnemyManager.enemy_queue.put((scheduled_time, new_enemy))
AbrarSyed's avatar
AbrarSyed committed
129 130 131
            
            EnemyManager.wave_number += 1
            
Elethier's avatar
Elethier committed
132
        return retval
133 134

    """
AbrarSyed's avatar
AbrarSyed committed
135
    Draw all enemies_list in the game to the screen.
136 137 138 139 140
    """
    def draw(self, surface):
        EnemyManager.spritegroup.draw(surface)

    """
Elethier's avatar
Elethier committed
141 142
    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).
143
    """
144
    def spawnEnemy(self, event, game):
145
        if(event.type == EnemyManager.SPAWN_EVENT_BASIC):
146
            entity = enemies.enemyentity.EnemyEntity(EnemyManager.enemies_types[0])
147
            entity.spawn(game.map, EnemyManager.spritegroup)
148
            EnemyManager.enemies_list.append(entity)
149 150 151 152

# A little trick so we can run the game from here in IDLE
if __name__ == '__main__':
    execfile("main.py")
153 154