public void setSnakeMapPosition(SnakeTransformer snakePosition) { this.snakePosition = snakePosition; transform.position = new Vector3(snakePosition.getMapPosition().x, snakePosition.getMapPosition().y); //Use Direction to set ANGLE for transform. //Alligns body segments based on direction of travel and cornering the segments. float angle; switch (snakePosition.getDirection()) { default: case Direction.Right: switch (snakePosition.getPriorDirection()) { default: angle = 90; break; case Direction.Up: //Last was up angle = 90 + 45; break; case Direction.Down: //Last was down angle = 90 - 45; break; } break; case Direction.Up: switch (snakePosition.getPriorDirection()) { default: angle = 0; break; case Direction.Right: //Last was Right angle = -45; break; case Direction.Left: //Last was Left angle = 45; break; } break; case Direction.Left: switch (snakePosition.getPriorDirection()) { default: angle = -90; break; case Direction.Up: //Last was up angle = -90 - 45; break; case Direction.Down: //Last was down angle = -90 + 45; break; } break; case Direction.Down: switch (snakePosition.getPriorDirection()) { default: angle = 180; break; case Direction.Right: //Last was Right angle = 180 + 45; break; case Direction.Left: //Last was Left angle = 180 - 45; break; } break; } transform.eulerAngles = new Vector3(0, 0, angle); }
private Direction direction; //Up, Down, Left, Right public SnakeTransformer(SnakeTransformer lastSnakeTransformer, Vector2Int mapPos, Direction direction) { this.lastSnakeTransform = lastSnakeTransformer; this.mapPos = mapPos; this.direction = direction; }
private void SnekMovement() { crawlTimer += Time.deltaTime; //Used for snake movement interval if (crawlTimer >= timeTillMove) //If snake needs to move, then move { SnakeTransformer last = null; //Failsafe for first body segment if (snekLocationHistory.Count > 0) { last = snekLocationHistory[0]; //Prior Segment if avaialable, else null } SnakeTransformer snekLocation = new SnakeTransformer(last, gridPosition, snekDirection); //Class (and "last" parameter) used for properly rotaing body segments. Vector2Int director; //Used in determining next location of snake. switch (snekDirection) { default: case Direction.Right: director = new Vector2Int(+1, 0); break; case Direction.Left: director = new Vector2Int(-1, 0); break; case Direction.Up: director = new Vector2Int(0, +1); break; case Direction.Down: director = new Vector2Int(0, -1); break; } //updateSnekParts(); snekLocationHistory.Insert(0, snekLocation); //Add current location to front of list. gridPosition += director; //Needed location added before this, else included head tile as first body. gridPosition = mapGrid.MapWrapper(gridPosition); //If the snake went through a border, return at opposite border. (same velocity) crawlTimer -= timeTillMove; //Reset Tick of timer. Game ticks. //Body Tracker//// bool snakeAte = mapGrid.SnekSneakSnekEat(gridPosition);//Let Map know where Snekky is for food collection check if (snakeAte) { //Grow Snake snekSize++; generateBodySegment(); //Adds the body segment. SoundManager.playSound(SoundManager.SFX.pop); //Plays a sound effect. } //ReUse snakeAte for secret test if ((snekSize > 5) && (spawnedD == false)) { spawnedD = mapGrid.spawnD(); } if ((snekSize > 4) && (spawnedG == false)) { spawnedG = mapGrid.spawnG(); } if ((snekSize > 3) && (spawnedS == false)) { spawnedS = mapGrid.spawnS(); } //char snakeLetterTemp = mapGrid.SnekLetter(gridPosition);//Let Map know if Snake collected G D or S if (lettersCollectedList.Count < 3) { //If not all letters were collected yet. //Check if Snake is eating any Letters. //if so return which char snakeLetterTemp = mapGrid.SnekLetter(gridPosition);//Let Map know if Snake collected G D or S //Returns 'N' if no. if (snakeLetterTemp != 'N') { lettersCollectedList.Add(snakeLetterTemp); } //Else ya didn't get a letter, lol. } else if (lettersCollectedList.Count == 3) { //Once all letters have been collected //Check if letters are in order; enable button. if (lettersCollectedList[0] == 'G') { if (lettersCollectedList[1] == 'D') { //Therefore [2] is 'S' //Enable Level Warp Button.. or instant... SnakeManager.gds(); } else { //Hint that first was right. } } //<Else>/<Always> add an arbitrary char to the lettersCollectedList //Always lettersCollectedList.Add('N'); } else { //Hacks //or add something to list if the letters weren't in the right order, thus preventing game from loading code within count == 3 logic repeatedly each update after... } if (snekLocationHistory.Count >= (snekSize + 1)) //<if> We are storing more locations than the total body size of the snake { snekLocationHistory.RemoveAt((snekLocationHistory.Count - 1)); //<then> Trim fat, ya didn't need to store so much snek. } ////////////////// transform.position = new Vector3(gridPosition.x, gridPosition.y); //Move Head transform.eulerAngles = new Vector3(0, 0, GetAngleFromVector(director) - 90); //Minus 90 fixes original rotation updateSnekParts(); //Do not check for Death before this Update ////Check for Death foreach (SnekParts bodypart in snakeBodyList) //Have we killed the snek??? { Vector2Int bodyMapPosition = bodypart.getMapPos(); if (gridPosition == bodyMapPosition) //If the head overlaps any bodypart { //GameOver //Debug.Log("Snake has Died!! :c"); //Commented out after popup retry working state = Status.Dead; SnakeManager.ripSnek(); } } ////////////////// } }