Наследование: MonoBehaviour
 // Use this for initialization
 void Start()
 {
     gameEngine   = GameObject.Find("GameEngine");
     mainScript   = gameEngine.GetComponent("MainScript") as MainScript;
     loggerObject = GameObject.Find("Logger");
     logger       = loggerObject.GetComponent <LoggerScript>();
 }
    /// <summary>
    ///
    /// </summary>
    /// <param name="logger"></param>
    /// <returns></returns>
    public async Task <string> getFromAPIasync(LoggerScript logger)
    {
        logger.Log("Requesting the last anchor.");
        //track the elapsed time
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        //Send the request
        string              url          = $"http://{fullAdress}/getlastanchor";
        HttpClient          httpClient   = new HttpClient();
        HttpResponseMessage httpResponse = await httpClient.GetAsync(url);

        httpResponse.EnsureSuccessStatusCode();
        // work with the response
        string body = await httpResponse.Content.ReadAsStringAsync();

        IdentifierObject identifierObject = JsonConvert.DeserializeObject <IdentifierObject>(body);

        stopwatch.Stop();
        if (identifierObject.Equals(null) || String.IsNullOrEmpty(identifierObject.id))
        {
            logger.Log("Coould not receive any Identifiers..");
            return(String.Empty);
        }
        logger.Log($"Received the last Anchor in {stopwatch.ElapsedMilliseconds} ms.");
        return(identifierObject.id);
    }
    /// <summary>
    /// Verschickt den Anker mittels eines <see cref="HttpClient"/>.
    /// </summary>
    /// <param name="identifier">ID-String des Ankesr</param>
    /// <param name="logger">Wird genutzt um dem Nutzer </param>
    /// <returns></returns>
    public async Task <bool> postToAPIasync(string identifier, LoggerScript logger)
    {
        logger.Log("Sending anchor to the server...");
        //track the elapsed time
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        //make the post request
        HttpClient httpClient      = new HttpClient();
        string     url             = $"http://{fullAdress}/addanchor";
        string     jsonRequestBody = $"{{\"id\":\"{identifier}\"}}";

        using (var content = new StringContent(jsonRequestBody, Encoding.UTF8, "application/json"))
        {
            var result = await httpClient.PostAsync(url, content);

            stopwatch.Stop();
            if (result.StatusCode == HttpStatusCode.OK)
            {
                logger.Log($"Request succsessful finished in {stopwatch.ElapsedMilliseconds} ms.");
                return(true);
            }
            else
            {
                logger.Log($"Request failed in {stopwatch.ElapsedMilliseconds} ms.", TextState.ERROR);
                await Task.Delay(200);

                return(false);
            }
        }
    }
    public IEnumerator WaitForPlayerToStop(float poleDistance, float poleHeight, string currentPoleColl, string currentPoleMagnetism, string currentPoleFriction, string currentPoleMagnetismArea)
    {
        string currentPoleName  = currentPoleColl;
        string currentPoleMag   = currentPoleMagnetism;
        string currentPoleFric  = currentPoleFriction;
        string currentPoleMagAr = currentPoleMagnetismArea;

        // Setting a weird algorithm to check whether ray still can be spotted or not !
        // Here, we are setting a short amount of delay when the player has certain level of velocity
        float playersXvelocity = GameObject.FindGameObjectWithTag("Player").GetComponent <Rigidbody2D>().velocity.x;

        if (playersXvelocity > 3)
        {
            yield return(new WaitForSeconds(0.75F));
        }
        if (playersXvelocity <= 3 && playersXvelocity > 2)
        {
            yield return(new WaitForSeconds(0.50F));
        }
        //yield return new WaitUntil(playersXvelocity == 0);
        else if (playersXvelocity <= 2 && playersXvelocity > 1)
        {
            yield return(new WaitForSeconds(0.5F));
        }
        else if (playersXvelocity <= 1 && playersXvelocity > 0)
        {
            yield return(new WaitForSeconds(0.25F));
        }
        else
        {
            yield return(new WaitForSeconds(0.10F));
        }


        GameObject   loggerObject = GameObject.Find("Logger");
        LoggerScript logger       = loggerObject.GetComponent <LoggerScript>();
        var          ray          = GetComponent <rayCastLogic>();

        if (ray.isSpotted())
        {
            ScoreAdded(currentPoleName);


            // Write in to Files - although the file writing procedure is on database right now
            logger.loggerMethod(1, poleDistance, poleHeight, currentPoleName, currentPoleMag, currentPoleFric, score, currentPoleMagAr, nearestPoleName, distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel);

            //Debug.Log(currentPoleName);
            // Find the Successive Pole Name here
            GameObject whatPole = GameObject.Find(currentPoleName);
            successivePoleX    = whatPole.transform.position.x;
            successivePoleY    = whatPole.transform.position.y;
            successivePoleName = currentPoleName;
        }

        else
        {
            logger.loggerMethod(0, poleDistance, poleHeight, currentPoleName, currentPoleMag, currentPoleFric, score, currentPoleMagAr, nearestPoleName, distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel);
        }
    }
Пример #5
0
 /// <summary>
 /// Initializes the logger. The App wont start without it.
 /// !! call it as the first init, because other methods rely on it.
 /// </summary>
 private void initLogger()
 {
     logger = userFeedback.GetComponent <LoggerScript>();
     if (logger == null)
     {
         throw new NotImplementedException();
     }
 }
    void OnCollisionEnter2D(Collision2D collider)
    {
        GameObject   loggerObject             = GameObject.Find("Logger");
        LoggerScript logger                   = loggerObject.GetComponent <LoggerScript>();
        float        poleDistance_fl          = 0;
        float        poleHeight               = 0;
        string       currentPoleMagnetism     = null;
        string       currentPoleFriction      = null;
        string       currentPoleMagnetismArea = null;

        if (collider.gameObject.tag == "Pole" && collider.gameObject.name != "PoleEnd")
        {
            // Play the animation for the player
            animator.SetBool("doJump", false);
            animator.SetBool("doIdle", true);
            animator.SetBool("doRun", false);

            // Get the current Pole name with which the pole get collided
            currentPoleColl = collider.gameObject.name;     // Name of the pole the player just got hit


            // if the player is on the first pole
            if (currentPoleColl == "Pole00")
            {
                currentPoleCollX = collider.gameObject.transform.position.x;
                //Debug.Log("For Pole 01: " + currentPoleCollX);
                prevPoleCollX       = currentPoleCollX; // after player hits the first pole we set the previous pole to Pole00
                poleDistance_fl     = 0;
                GameOverCanvasReset = true;             // Resetting GameOverCanvas back to false
                //Debug.Log("Is Game Over Canvas Reset: " + GameOverCanvasReset);
                successivePoleX = currentPoleCollX;
                successivePoleY = collider.gameObject.transform.position.y;
            }

            // but if the collder is other than the first pole then
            else
            {
                currentPoleCollX = collider.gameObject.transform.position.x;    // current pole X is the current collider's X position
                poleDistance_fl  = currentPoleCollX - prevPoleCollX;            // pole distance calculation
                prevPoleCollX    = currentPoleCollX;                            // setting the current pole to prev pole again
            }

            poleHeight               = collider.gameObject.transform.position.y;
            currentPoleMagnetism     = collider.gameObject.GetComponentInChildren <PointEffector2D>().forceMagnitude.ToString();
            currentPoleFriction      = collider.gameObject.GetComponent <BoxCollider2D>().sharedMaterial.friction.ToString();
            currentPoleMagnetismArea = collider.gameObject.GetComponentInChildren <CircleCollider2D>().radius.ToString();

            if (currentPoleMagnetismArea == "6")
            {
                currentPoleMagnetismArea = "H";
            }
            else if (currentPoleMagnetismArea == "3")
            {
                currentPoleMagnetismArea = "L";
            }
            else
            {
                currentPoleMagnetismArea = "M";
            }

            // Call the raycasting here
            var ray = GetComponent <rayCastLogic>();
            if (ray.isSpotted())
            {
                // Start a coroutine to set up small wait until the player stops
                StartCoroutine(WaitForPlayerToStop(poleDistance_fl, poleHeight, currentPoleColl, currentPoleMagnetism, currentPoleFriction, currentPoleMagnetismArea));
            }
        }
        else if (collider.gameObject.tag == "Ground")
        {
            //print("Collsion occured with: " + collider.gameObject.tag + "Player has fall down");
            //animator.SetBool("doJump", false);
            //animator.SetBool("doIdle", false);
            animator.SetTrigger("doDeath");
            gameSounds.PlayOneShot(audioFall, 0.5F);

            MainScript.playerIsAlive = false;


            // Log in to files
            logger.loggerMethod(0, 0, 0, "null", "null", "null", score, "null", nearestPoleName, distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel);

            // Checking if the ninja hits the ground; in this case restart the game
            //if (!audioFall.isPlaying) audioFall.Play();
            if (GameOverCanvasReset == true)
            {
                GameOverCanvasReset = false;
                //Debug.Log("Is Game Over Canvas Reset: " + GameOverCanvasReset);
                StartCoroutine(GameEnd()); // shortly wait for the unity engine to reload the scene
            }
        }
        else if (collider.gameObject.name == "PoleEnd")
        {
            var ray = GetComponent <rayCastLogic>();
            if (ray.isSpotted())
            {
                // Update Score
                ScoreAdded(collider.gameObject.name);

                // Set player to !Alive to stop further mouse button click
                MainScript.playerIsAlive = false;

                // Start running
                animator.SetBool("doRun", true);

                // Log
                logger.loggerMethod(1, poleDistance_fl, poleHeight, "PoleEnd", "0", "0", score, "null", "poleEnd", distanceOfNearestPoleXPosition, assistanceLevel, hindranceLevel);
                StartCoroutine(GameComplete());
            }
        }

        if (collider.gameObject.name != "PoleEnd")
        {
            // Set the pole's magnetism false when player collides with it
            poleMagObjects = GameObject.FindGameObjectsWithTag("PoleMagnetism");
            foreach (GameObject pole in poleMagObjects)
            {
                if (pole.GetComponent <PointEffector2D>().enabled)
                {
                    pole.GetComponent <PointEffector2D>().enabled = false;
                }
            }
        }
    }
Пример #7
0
 public void Start()
 {
     _knight = GameObject.FindGameObjectWithTag("Player").GetComponent <KnightBehavior>();
     _logger = GameObject.Find("Logger").GetComponent <LoggerScript>();
 }