public void Move(Possition possition) { if (possition.X == 0 && possition.Y == 0) { return; } ConsoleHelper.Clear(SnakeBody.Tail.Value); SnakeBody.ReverseForEach(node => { if (node.Previous != null) { node.Value.X = node.Previous.Value.X; node.Value.Y = node.Previous.Value.Y; } }); SnakeBody.Head.Value.ChangePossition(possition); for (int i = 0; i < Foods.Count; i++) { if (Foods[i].Possition == SnakeBody.Head.Value) { Foods[i].EatFood(); Grow(possition); SpawnFood(); } } }
//吃东西特效 public void addEatingEffect(int num,float delay) { if (num == -1) { //蛇尾 render.enabled = false; } else { render.enabled = true; if (!eatingEffect) { eatingEffect = Instantiate (this.gameObject).GetComponent<SnakeBody> (); eatingEffect.trans.localPosition = new Vector3 (0, 0, -0.1f); eatingEffect.trans.SetParent (trans, false); } else { eatingEffect.gameObject.SetActive (true); } eatingEffect.updateTex (num); eatingEffect.trans.localScale = new Vector3 (1f,1f,1f); } if (coroEating != null) { StopCoroutine(coroEating); coroEating = null; } coroEating = StartCoroutine (eatingAnimate(delay)); }
void BodyCreate() { GameObject obj = Instantiate(SnakeBody, transform); SnakeBody body = new SnakeBody(); body.LastDistance = Mathf.Infinity; body.BodyTransform = obj.transform; Vector3 Rotation = body.BodyTransform.eulerAngles; if (BodyList.Count == 0) { Rotation.z = DefRotate; } else { Rotation.z = BodyList[BodyList.Count - 1].BodyTransform.eulerAngles.z; body.HeadAng.AddRange(BodyList[BodyList.Count - 1].HeadAng); body.HeadPos.AddRange(BodyList[BodyList.Count - 1].HeadPos); } body.BodyTransform.eulerAngles = Rotation; body.BodyTransform.position = BodyList.Count == 0 ? Head.position : BodyList[BodyList.Count - 1].BodyTransform.position; body.BodyTransform.Translate(Vector2.left * BodyDelta); BodyList.Add(body); }
public ClientGameControl() { // 使用这个构造函数则默认为离线模式 // 初始化食物和snake 设置游戏模式 this.PlayerGameMode = GameMode.OFFLINE; this.IsGameStart = false; m_snake = new SnakeBody(GeneratePlayerID()); // SnakeBody的构造函数只能生成id }
void SpawnAfterConsumption() { SnakeBody obj = Instantiate(snake, snakeBody[snakeBody.Count - 1].transform.position, snakeBody[snakeBody.Count - 1].transform.rotation); obj.transform.SetParent(this.transform); obj.Set(false); snakeBody.Add(obj); }
/// <summary> /// Generates new gem at ranom position /// </summary> protected virtual void NewGem() { Gem = new Point(rnd.Next(0, MAX_X), rnd.Next(0, MAX_Y)); while (SnakeBody.Contains(Gem)) { Gem = new Point(rnd.Next(0, MAX_X), rnd.Next(0, MAX_Y)); } }
private void Eat(SnakeBody body) { var left = 0.0; var top = 0.0; var direction = Direction.Right; if (_snakes.Count == 0) { _snakeHead.SpotChild = body; direction = _snakeHead.MoveDirection; left = Canvas.GetLeft(_snakeHead); top = Canvas.GetTop(_snakeHead); body.SpotParent = _snakeHead; } else { var last = _snakes[_snakes.Count - 1]; last.SpotChild = body; direction = last.MoveDirection; left = Canvas.GetLeft(last); top = Canvas.GetTop(last); body.SpotParent = last; } _snakes.Insert(_snakes.Count, body); _scatters.Remove(body); body.MoveDirection = direction; switch (direction) { case Direction.Top: body.SetValue(Canvas.LeftProperty, left); body.SetValue(Canvas.TopProperty, top + body.Height); break; case Direction.Bottom: body.SetValue(Canvas.LeftProperty, left); body.SetValue(Canvas.TopProperty, top - body.Height); break; case Direction.Left: body.SetValue(Canvas.LeftProperty, left + body.Width); body.SetValue(Canvas.TopProperty, top); break; case Direction.Right: body.SetValue(Canvas.LeftProperty, left - body.Width); body.SetValue(Canvas.TopProperty, top); break; } AddScatterSpot(); }
// Adds a new body part to the snake body public void AddNewBodyPart(Vector3 position) { GameObject body = Instantiate(bodyPrefab, position, Quaternion.identity); SnakeBody script = body.GetComponent <SnakeBody>(); script.xDir = this.xDir; script.yDir = this.yDir; bodies.Insert(0, body); }
private void MinusOneBody() { snakeLength--; snakeTail = snakeTail.GetLastSnakeBodyObj(); Destroy(snakeTail.GetNextSnakeBodyObj()); snakeTail.SetNextBody(null); snakeBodySpriteRenderer.sortingOrder++; }
public void SpawnNew(GameArea area, SnakeBody snake) { var availableFields = area.AllFields.Except(snake.GetBody().Select(n => (n.X, n.Y))).ToArray(); var idx = _rnd.Next(0, availableFields.Length); X = availableFields[idx].X; Y = availableFields[idx].Y; }
void RemoveSection() { SnakeBody body = m_bodies[m_bodies.Count - 1]; m_bodies.RemoveAt(m_bodies.Count - 1); body.Cell.Free(); body.Go.AddComponent <Rigidbody>(); }
/// <summary> /// Consumes gen - generating it at new position and incrementing <see cref="Score"/> by <see cref="ScoreIncrement"/> /// </summary> protected virtual void EatGem() { var last = SnakeBody.Last(); SnakeBody = ArrayAddItem(SnakeBody, new Point(last.X + 1, last.Y + 1)); NewGem(); Score += ScoreIncrement; }
public void Grow(Possition possition) { var oldPOssition = SnakeBody.Head.Value; var newHead = new Node(new Possition(oldPOssition.X, oldPOssition.Y)); //newHead.Value.ChangePossition(possition); BoundriesHelper.CheckBoundaries(newHead.Value, possition); SnakeBody.AddHead(newHead); }
public override void Eat(SnakeBody snake) { BodyPart lastPart = snake.BodyParts.Last(); snake.BodyParts.Remove(lastPart); if (snake.BodyParts.Count < 1) { snake.isDead = true; } }
private void InstantiateFirstBody() { nextSnakeBodyObj = Instantiate(snakeBodyObj, transform.position, transform.rotation) as GameObject; nextSnakeBody = nextSnakeBodyObj.GetComponent <SnakeBody>(); snakeLength = 1; nextSnakeBody.SetAllMemember(null, snakeLength, null); snakeTail = nextSnakeBody; snakeBodySpriteRenderer.sortingOrder--; }
private bool IsKnot() { for (int i = 1; i < SnakeBody.Count; i++) { if (SnakeBody.ElementAt(i) == SnakeBody.First.Value) { return(true); } } return(false); }
internal void Destroy() { for (int i = 0; i < m_bodies.Count; i++) { SnakeBody body = m_bodies[i]; if (body.Go != null) { GameObject.Destroy(body.Go); } } }
public override void _Ready() { body = GetNode("SnakeBody") as SnakeBody; body.Position = new Vector2(0, 0); timer = new System.Timers.Timer(10000); timer.Elapsed += NewApple; timer.AutoReset = true; timer.Start(); apple = GetNode("Apple") as Apple; apple.Position = new Vector2(rnd.Next(15) * 40, rnd.Next(8) * 40); }
void Start() { base.Start(); if (nextSegment) { nextSegmentBody = nextSegment.GetComponent <SnakeBody>(); } if (headObject) { head = headObject.GetComponent <SnakeHead>(); } }
// Loop through all segments and set their positions. public void PositionSegments(SnakeHead head) { SetHeadPosition(head); int positionIndex = 0; SnakeBody bodySegment = head.NextSegment; while (bodySegment != null) { positionIndex = SetSegmentPositionAndRotation(bodySegment, positionIndex); bodySegment = bodySegment.NextSegment; } }
private void CreateSnake() { Point location = new Point(random.Next(12, 790) / 10 * 10, random.Next(56, 590) / 10 * 10); Snake newsnake = new Snake(location, this, SnakeBody); SnakeBody.AddLast(location); for (int i = 4; i < 30; i += 10) { SnakeBody.AddLast(new Point(location.X - i, location.Y)); } newsnake.MessageSender += this.MessageSender; Snake = newsnake; }
public void MoveEgg(SnakeBody otherSegment) { // When this method is called, if this segment has an egg attached, // the egg should be transferred to the other segment if (this.Egg == null) { return; } otherSegment.BeginToCreateEgg(this.Egg); this.Egg.FullyGrown -= this.EggFullyGrown; this.Egg = null; }
public void NewGame() { if (Dead != null) { StopCoroutine(Dead); } grid = new CellGrid(Width, Height); int startX = Mathf.FloorToInt(Width / 2); int startY = Mathf.FloorToInt(Height / 2); body = new SnakeBody(grid.GetGridCell(startX, startY)); headPosition = new IntVector2(startX, startY); }
public void Draw() { SnakeBody.ForEach(node => { var text = "*"; Console.SetCursorPosition(node.Value.X, node.Value.Y); if (node == SnakeBody.Head) { text = "@"; } ConsoleHelper.Write(node.Value, text); }); }
//*************************************************************************** //对蛇的相关操作 private void AddOneBody() { snakeLength++; tSnakeBodyObj = Instantiate(snakeBodyObj, snakeTail.transform.position, snakeTail.transform.rotation) as GameObject; tSnakeBody = tSnakeBodyObj.GetComponent <SnakeBody>(); tSnakeBody.SetAllMemember(snakeTail, snakeLength, null); snakeTail.SetNextBody(tSnakeBodyObj); snakeTail = tSnakeBody; snakeBodySpriteRenderer.sortingOrder--; }
public ClientGameControl(IPAddress serverIPAddress, int serverPort) { // 初始化食物和snake 设置游戏模式 this.PlayerGameMode = GameMode.ONLINE; this.IsGameStart = false; this.ServerIPAddress = serverIPAddress; this.ServerPort = serverPort; m_snake = new SnakeBody(GeneratePlayerID()); // SnakeBody的构造函数只能生成id m_playerList = new List <SnakeBody>(); m_frameID = 0; }
// Moves the snake (head and body included) in the current direction protected void MoveSnake() { // Dont move until the game board is done setting up if (GameManager.instance.doingSetup) { return; } Vector2 pos = transform.position; // Update Direction UpdateDirection(); // Move Head MoveHead(); // If no body to instantiate if (!hasEaten) { if (!this.boxCollider.enabled) { this.boxCollider.enabled = true; } // Move Body if (bodies.Count > 0) { int x = xDir; int y = yDir; // Get the tail GameObject snakeBodyObj = bodies[bodies.Count - 1]; SnakeBody body = snakeBodyObj.GetComponent <SnakeBody>(); // Remove the tail from the list bodies.RemoveAt(bodies.Count - 1); // Move tail snakeBodyObj.transform.position = pos; // Insert the tail to the front of the bodies list bodies.Insert(0, snakeBodyObj); } } // else create new body, link to rest of body, and do not move body else { this.boxCollider.enabled = false; AddNewBodyPart(pos); hasEaten = false; } }
public void GrowBody(Vector3 pos) { if (bodyPrefab) { SnakeBody newBody = Instantiate(bodyPrefab, transform); newBody.Init(pos, bodySprites[snakeBodyQueue.Count % 2]); snakeBodyQueue.Enqueue(newBody); newBody.gameObject.name = snakeBodyQueue.Count.ToString(); if (snakeBodyQueue.Count <= ingoreBodyCollider) { newBody.GetComponent <CircleCollider2D>().enabled = false; } } }
void SpawnNewPart() { Debug.Log("called spawn"); SnakeBody obj = Instantiate(snake); obj.transform.SetParent(this.transform); obj.transform.position = snakeBody[snakeBody.Count - 1].transform.position; obj.transform.rotation = snakeBody[snakeBody.Count - 1].transform.rotation; Vector3 position = obj.transform.position - offset; obj.transform.position = position; obj.Set(false); snakeBody.Add(obj); }
public static void Register(SnakeBody sb) { GameMaster mygm = GetInstance(); do { Vector2 sbRealPos = (sb.transform.position); if (mygm.CheckBoundry(sbRealPos) != 1) { Vector2 sbPos = AdjustPosToBoard(sb.transform.position); mygm.board[(int)sbPos.x, (int)sbPos.y] = 1; } sb = sb.back; } while (sb != null); }
void Start() { if (nextSegment) { nextSegmentBody = nextSegment.GetComponent <SnakeBody>(); } graph = graphObject.GetComponent <Graph>(); nav = GetComponent <UnityEngine.AI.NavMeshAgent>(); currIndex = graph.GetIndexFromPosition(transform.position); lastIndex = currIndex; enemyName = gameObject.name; graph.vertices[currIndex].occupiedBy = enemyName; graph.vertices[currIndex].occupied = true; graph.vertices[currIndex].NotifyParentOrChild(); }
public void addTail() { if (bodyFollower == null) { Transform T = (Transform)Instantiate(levelManager.snakeTail_prefab, transform.position, transform.rotation); SnakeBody newSnakeTail = T.GetComponent<SnakeBody>(); newSnakeTail.bodyFollowed = this; newSnakeTail.bodyFollower = null; bodyFollower = newSnakeTail; Color col = getIsPlayer() ? levelManager.playerTailColor : levelManager.enemyTailColor; newSnakeTail.GetComponentInChildren<Gyroscope>().SetColor(col); } else { bodyFollower.addTail(); } }
void Awake() { ++instance_count; render = GetComponent<Renderer> (); eatingEffect = null; trans = gameObject.GetComponent<Transform> (); }
public void OnHeadTouchesTail(SnakeBody snakeTail) { SnakeBody otherSnakeHead = snakeTail; // is last tail? (bell) while (otherSnakeHead.bodyFollowed != null) otherSnakeHead = otherSnakeHead.bodyFollowed; if (this != otherSnakeHead) { if (!snakeTail.getIsInvulnerable()) { // if is bell, get control of all other body's snake if (snakeTail.isBell()) { this.bodyFollower.bodyFollowed = snakeTail; snakeTail.bodyFollower = this.bodyFollower; this.bodyFollower = otherSnakeHead.bodyFollower; otherSnakeHead.bodyFollower.bodyFollowed = this; this.transform.position = otherSnakeHead.transform.position; if (otherSnakeHead.getIsPlayer()) { // tell levelmanager we died... levelManager.OnPlayerDied(); // change color. this.setTailColor(levelManager.enemyTailColor); } if (this.getIsPlayer()) { this.setTailColor(levelManager.playerTailColor); levelManager.playClip(LevelManager.ClipType.EAT); } Destroy(otherSnakeHead.gameObject); } else // otherwise, only split. { snakeTail.splitTail(); this.addTail(); if (this.getIsPlayer()) { levelManager.playClip(LevelManager.ClipType.EAT); } if (otherSnakeHead.getIsPlayer()) { levelManager.playClip(LevelManager.ClipType.HURT); } isInvulnerable = true; invulnerableRemainingTime = Mathf.Min(invulnerableRemainingTime + 3.0f, 3.0f); } if (otherSnakeHead.getIsPlayer()) { levelManager.OnPlayerTailLenghtChanged(otherSnakeHead.countTailLength()); } else if (this.getIsPlayer()) { levelManager.OnPlayerTailLenghtChanged(this.countTailLength()); } } } else { /*if (snakeTail.isBell()) { this.makeRing(); }*/ } }
public void splitTail() { if (!isBell()) { this.bodyFollower.bodyFollowed = null; this.bodyFollower.makeHead(); } Destroy(this.gameObject); }
public void addToList(SnakeBody part) { part.setPrevious(getLastElem()); snake.Add(part); }
public void setPrevious(SnakeBody prev) { previous = prev; }