import pygame
from pygame.locals import *

import os
import random

from locals import *

import pathfind
from character import Character
from obj import Object

class Zombie (Character):
    def __init__(self, name, pos):
        Character.__init__(self, name, pos)
        self.objtype = TYPE_ENEMY
        self.idle = True
        self.hitting = False

        s_id = int(random.random() * 3)
        if s_id == 0:
            self.sound = pygame.mixer.Sound("sounds/zombie.wav")
        elif s_id == 1:
            self.sound = pygame.mixer.Sound("sounds/zombie2.wav")
        else:
            self.sound = pygame.mixer.Sound("sounds/zombie3.wav")

        self.sound.set_volume(0.3)

# 5-15 seconds until next sound
        self.next_sound = pygame.time.get_ticks() + random.random() * 10000

    def update(self, map_):
        Character.update(self, map_)
        if (not self.dead) and pygame.time.get_ticks() > self.next_sound:
            self.sound.play()
# 5-15 seconds until next sound
            self.next_sound = pygame.time.get_ticks() + random.random() * 10000 + 5000

    def stop(self):
        #Zombies seem to just keep on walking, so these should fix that...
        self.pos = [self.pos[0], self.pos[1]]
        self.pos[0] -= self.dir[0]
        self.pos[1] -= self.dir[1]
        Character.stop(self)

    def take_damage(self, damage):
        Character.take_damage(self, damage)

    def choose_action(self, map_):
        #Finds the nearest character to attack, if they're nearer than 5 units
        if pygame.time.get_ticks() - self.last_action < ACTION_CHECK_DELAY:
            return
        self.last_action  = pygame.time.get_ticks()
        atarget = None
        atargetdist = ZOMBIE_ATTACK_DISTANCE
        for o in map_.objects:
            if o.objtype == TYPE_CHARACTER and (not o.dead) and self.distance(o) < atargetdist:
               atarget = o
               atargetdist = self.distance(o)
        if atarget:
            self.attack(map_, atarget)
        if self.idle:
            #This might not work, if the zombie tries to get out of the map.
            targetx = self.pos[0] + (random.random() - 0.5)*5
            targety = self.pos[1] + (random.random() - 0.5)*5
            self.move_to_dumb((targetx, targety))



