import Player.py
import Shot.py
from random import Random
import numpy as np
import curses



class World(object):
    """description of class"""

    

    def __init__(self, numPlayers, width, height, playerWidth, playerHeight, gravity):

        self.world = curses.initscr() #initialize a screen

        self.numPlayers = numPlayers
        self.width = width
        self.height = height
        self.playerWidth = playerWidth
        self.playerHeight = playerHeight
        self.gravity = gravity

        #randomly generated terrain
        self.terrain =  [random.randint(-1,1) for n in range(width)]
        self.terrain[0] = self.height/2
        for x in range(width):
            try:
                terrain[x+1] = terrain[x]-terrain[x+1]
            except:
                pass

        self.playerXpos = random.sample(range(3,width-3), numPlayers) # the x value is the middle bottom of the tank
        #make a list of players --- need to randomly generate player position
        #Player will be  bottom left of the tank for y value
        self.playerList = [Player.Player(n, playerHeight,playerWidth,self.getAltitude(playerXpos[n])-3,self.playerXpos[n],1) for n in range(numPlayers)]


    def initialize(self):
        """Randomly generate land """
        self.world.clear()
        #generate world with terrain
        for y in range(self.height):
            for x in range(self.width):
                if( y == terrain[x]):
                    world.addstr(y,x,"T")



    def reset(self):
        """  generate player position"""
        self.world.clear()
        for y in range(self.height):
            for x in range(self.width):
                if( y == terrain[x]):
                    world.addstr(y,x,"T")
        

    def getPlayerLocation(self, playerIndex):
        """do some stuff"""

    def getAltitude(self, x):
        """get the height at a given x position
            world[x] is subtracted b/c python matrix
            is top to bottom
        """
        return self.terrain[x]

    def getLastShotHeight(self, x):
        """ Height of last shot for a given x or -1 if last shot is not present at x"""

    def resetLastShot(self):
        """stop displaying the last shot"""

    def doShot(self, playerIndex, velocity, angle):
        """player fires a shot with a given velocity and angle"""
        x = playerList[playerIndex].getXPos()
        y = playerList[playerIndex].getYPos()
        power = 1
        shot = Shot.Shot(x, y, velocity, angle,power)
        maxDistance = shot.getXforV()

        #draw the shot
        shotImage = "*"
        for w  in range(maxDistance+1):
            self.world.addstr(shot.getYForX(w),w,shotImage)
                              


    def isDead(self, playerIndex):
        """check to see if a player isAlive"""
        for player in self.playerList:
            if( player.getIndex() == playerIndex):
                if( player.isAlive()):
                    return False
                else:
                    return True
            else:
                return True

        




       







