Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        //player = GameObject.FindGameObjectWithTag("Player"); //at one point, the player game object was being set every update. I don't think this is necessary, but it may have had some effect.
        //if (clickManager.MouseClicked && !turnOffAb) //check if the laser has been activated
        if (clickManager.TouchRegistered && !turnOffAb)                  //check if the laser has been activated
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //get the current position of the mouse pointer
            //target = gridLayout.WorldToCell(ray.origin); //set the position of the target to the position of the mouse pointer in grid coordinates
            target = gridLayout.WorldToCell(clickManager.TouchPosition); //set the position of the target to the position of the mouse pointer in grid coordinates
            //target = gridLayout.WorldToCell(clickManager.ClickPosition); //set the position of the target to the position of the mouse pointer in grid coordinates
            //clickDistance = Vector3.Distance(gridLayout.CellToWorld(target), gridLayout.CellToWorld(gridLayout.WorldToCell(player.transform.position))); //this is find the distance between the player and the point where they click. It converts multiple times bewteen the grid and world coordinates because it wants the world coordinates of the exact center of the relevant hexs. The easiest way I have found to do this is to first take world coordinates, convert them to grid coordinates, then convert those back to world coordinates
            playerHex     = gridLayout.WorldToCell(player.transform.position);                                           //get the current position of the player in grid coordinates
            clickDistance = mapManager.HexCellDistance(mapManager.evenq2cube(target), mapManager.evenq2cube(playerHex)); //This calculation determines the distance to the clicked cell using the cube coordiante method

            if (laserState && clickDistance <= laserRange)                                                               //if the player clicks the mouse and it is within the set range for the laser then initiate the firing sequence
            {
                //The purpose of this loop is to identify what hexes within the range above are also identified by the highlighted hexes shown to the player. This is a hack and there must certainly be a better way to do this, but it ultimately comes down to figuring out a better method of measuring distance on the hex grid
                for (int x = -laserRange; x <= laserRange; x++)     //iterate x's
                {
                    for (int y = -laserRange; y <= laserRange; y++) //iterate y's
                    {
                        //Debug.Log(target);
                        //Debug.Log(playerHex + new Vector3Int(x, y, 0));
                        if (target == (playerHex + new Vector3Int(x, y, 0)) && weaponState) //check if the target hex matches one of the highlighted hexes
                        {
                            GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
                            foreach (GameObject enemy in enemies) //loop through the list of any enemies currently in the scene and destroy them
                            {
                                if (!enemy.GetComponent <EnemyShipControl>().CheckShotRunning)
                                {
                                    StartCoroutine(enemy.GetComponent <EnemyShipControl>().CheckShot(target));
                                }
                            }

                            instX = player.transform.position.x;                                                                                         //set the x position of the instatiation equal to the player's current x position
                            instY = player.transform.position.y;                                                                                         //set the y position of the instatiation equal to the player's current x position
                            //laserSpwanMod = Vector3.Normalize(player.transform.position - ray.origin); //calculate a vector that points between the target and the player, then normalize it
                            laserSpwanMod = Vector3.Normalize(player.transform.position - clickManager.TouchPosition);                                   //calculate a vector that points between the target and the player, then normalize it
                            //laserSpwanMod = Vector3.Normalize(player.transform.position - clickManager.ClickPosition); //calculate a vector that points between the target and the player, then normalize it
                            newInstance = Instantiate(laser, new Vector3(instX - (laserSpwanMod.x), instY - (laserSpwanMod.y), 0), Quaternion.identity); //Instantiate the laser object and apply some modifications that move the laser off of the ceneter of the player game object and to the edge of the ship sprite (this looks better in game then spawning at the center).
                            turnOffAb   = true;
                        }
                    }
                }
            }
            else if (jumpState)
            {
                bool makeJump = false;
                foreach (Vector3Int cell in jumpCells)
                {
                    if (target == cell)
                    {
                        makeJump = true;
                    }
                }
                if (makeJump)
                {
                    JumpActive();
                    //Debug.Log("set true at Ab con 112");
                    abilityUsed = true;
                    clickManager.WaitForQuarterSec();
                    movementController.MovePlayer(target, false);
                    //jumpRange = 0;
                    jumpRange -= (int)clickDistance;
                    uiController.SetJumpCharge(jumpRange, maxJumpRange);
                    if (turnManager.combatActive)
                    {
                        movementController.HasMoved = true;
                        uiController.SetEndTurnButtonState();
                    }
                    turnManager.StartCoroutine(turnManager.UpdateTurn());
                }
            }
            else if (rocketState)
            {
                bool inRange = false;
                foreach (Vector3Int flat in playerFlats)
                {
                    if (target == flat)
                    {
                        inRange = true;
                    }
                }
                if (inRange && !hasFired)
                {
                    Debug.Log("sending off a rocket");
                    hasFired = true;
                    currentRocketReloadAmount = 0;
                    Instantiate(rocket, player.transform.position, Quaternion.identity);
                    turnManager.StartCoroutine(turnManager.UpdateTurn());
                }
            }
        }
        if (turnOffAb)
        {
            timer += Time.deltaTime;
            if (timer > 0.5)
            {
                //Debug.Log("Timer");
                if (laserState)
                {
                    LaserActive();
                }
                if (turnManager.combatActive)
                {
                    //laserRange = 0;
                    laserRange -= (int)clickDistance;
                    uiController.SetLaserCharge(laserRange, maxLaserRange);
                }
                timer     = 0;
                turnOffAb = false;
                //Debug.Log("set true at Ab con 156");
                abilityUsed = true;
                turnManager.StartCoroutine(turnManager.UpdateTurn());
                if (turnManager.combatActive && movementController.HasMoved)
                {
                    uiController.SetEndTurnButtonState();
                }
            }
        }
    }