private void ExtendSnakeRecursive(int count) { if (bodyParts.Count > 1) { SnakeMiddleBody bodyPart = Instantiate <SnakeMiddleBody> (middleBodyPrefab, transform); bodyPart.Init(this); bodyPart.transform.position = bodyParts[bodyParts.Count - 1].transform.position; bodyPart.positionX = bodyParts[bodyParts.Count - 1].positionX; bodyPart.positionY = bodyParts[bodyParts.Count - 1].positionY; bodyParts.Insert(bodyParts.Count - 1, bodyPart); TiledGameMap.Instance.AddOccupantClean(bodyPart.positionX, bodyPart.positionY, bodyPart); count--; if (count > 0) { ExtendSnakeRecursive(count); } } // add tail of not if (bodyParts.Count == 1) { SnakeTail tail = Instantiate <SnakeTail> (tailPrefab, transform); tail.Init(this); tail.transform.position = bodyParts[0].transform.position; tail.positionX = bodyParts[0].positionX; tail.positionY = bodyParts[0].positionY; bodyParts.Add(tail); TiledGameMap.Instance.AddOccupantClean(tail.positionX, tail.positionY, tail); count--; if (count > 0) { ExtendSnakeRecursive(count); } } }
/// <summary> /// Release this instance. /// </summary> public void Release() { //Release Component! for (int i = 0; i < m_listCompoent.Count; ++i) { m_listCompoent[i].Release(); } m_listCompoent.Clear(); //释放整条蛇,包括头尾 SnakeNode node = m_head; while (node != null) { SnakeNode next = node.Next; EntityFactory.ReleaseEntity(node); node = next; } m_head = null; m_tail = null; if (m_container != null) { GameObject.Destroy(m_container); m_container = null; } m_context = null; }
public SnakeTail BuildSnakeTail() { SnakeTail snakeTail = SnakeTailPool.Get() as SnakeTail; snakeTail.gameObject.SetActive(true); return(snakeTail); }
/// <summary> /// The snake this tail belongs to. /// </summary> //public Snake AssignedSnake { get; set; } /// <summary> /// Unity event. Called once the behavior is instantiated. /// Gets the snake tail prefab from the game resources. /// </summary> private void Awake() { if (!_snakeTailPrefab) { _snakeTailPrefab = Resources.Load <SnakeTail>("Snake Tail"); } }
public void SetTailToPosition(int posX, int posY, float angle, bool isCurved) { SnakeTail part = snakeTailPool.FirstOrDefault(x => x.rectTransform.anchoredPosition == defaultPoolLocation); part.image.sprite = isCurved ? curvedTail : straightTail; part.rectTransform.anchoredPosition = new Vector2(posX, posY); part.rectTransform.eulerAngles = new Vector3(0, 0, angle); }
void AddTail() { SnakeTail tail = Instantiate(snakeTailPrefab, Vector3.zero, Quaternion.identity) as SnakeTail; tail.SetColor(color); tail.SetWidth(width); snakeTails.Add(tail); snakeCounter++; }
/// <summary> /// Recursively adds a tail to this or one of the next tails, /// dependat on which tail needs a next tail. /// </summary> public SnakeTail AddTail() { if (NextTail) { return(NextTail.AddTail()); } _nextTail = (SnakeTail)Instantiate(_snakeTailPrefab, transform.position, Quaternion.identity); return(_nextTail); }
/// <summary> /// This method is repeated every few seconds and spawns a new snake tail from the object pool. /// </summary> private void SpawnCollider() { newSnakeTail = ObjectPool.sharedInstance.GetPooledObject(); newSnakeTail.transform.position = transform.position; newSnakeTail.transform.rotation = transform.rotation; newSnakeTail.gameObject.SetActive(true); newSnakeTail.Init(thinPowerupEnabled, invincible); snakeTailList.Add(newSnakeTail); }
private void FillSnakeTailPool() { for (int i = 0; i < snakeTailPoolSize; i++) { GameObject spawnedObj = Instantiate(snakeTailPrefab, snakeTailsParent); SnakeTail obj = spawnedObj.GetComponent <SnakeTail>(); snakeTailPool.Add(obj); } }
private void Start() { componentRigidbody = GetComponent <Rigidbody2D>(); componentSnakeTail = GetComponent <SnakeTail>(); componentSnakeTail.AddCircle(); componentSnakeTail.AddCircle(); componentSnakeTail.AddCircle(); componentSnakeTail.AddCircle(); }
void AddTail() { SnakeTail tail = Instantiate(tailPrefab, Vector3.zero, Quaternion.identity) as SnakeTail; tail.SetColor(color); tail.SetWidth(width); tail.SetOffset((int)(width * 7)); // was 8 tail.transform.parent = map.transform; // optional, for convenience snakeTails.Add(tail); }
private void Start() { pooledObjects = new List <SnakeTail>(); for (int i = 0; i < amountToPool; i++) { SnakeTail snakeTail = Instantiate(objectToPool); snakeTail.gameObject.SetActive(false); pooledObjects.Add(snakeTail); snakeTail.transform.parent = instantiatedObjects.transform; } }
//see SnakeElement public override void AddElement(List <SnakeElement> snakeElements) { //position and rotation from current last Snake Element Vector3 position = snakeElements[snakeElements.Count - 1].gameObject.transform.position; Quaternion rotation = snakeElements[snakeElements.Count - 1].gameObject.transform.rotation; SnakeTail newTail = Instantiate(gameObject, position, rotation).GetComponent <SnakeTail>(); newTail.gameObject.name = gameObject.name; snakeElements.Add(newTail); }
private void AddToSnake() { var previous_tail = this.Tail[this.Tail.Count - 1]; var tail = new SnakeTail() { Position = previous_tail.Position, Depth = previous_tail.Depth + 1 }; Engine.SpawnInstance(tail); this.Tail.Add(tail); }
public Vector3 GetNewEndTailLocation(SnakeTail tail) { if (nextTail != null) { return(nextTail.GetNewEndTailLocation(tail)); } else { nextTail = tail; return(new Vector3(prevLoc.x, transform.position.y, prevLoc.y)); } }
/*<summary> * OnTriggerEnter checks with wihich object head is colliding. * When collision is detected, AudioManager is calling to play sound. * </summary> */ private void OnTriggerEnter(Collider other) { Debug.Log(other.gameObject.name); //first tail element. Head can collide with it because of specificaton of tail movement. //It does not crush the game, because it is not posible to bump into first tail element, //because head can not be turned 180 degrees if (other.gameObject == GameController.snake[1]) { return; } if (other.gameObject.tag == "Wall" || other.gameObject.tag == "Tail") { //play sound AudioManager.instance.PlaySound("GameOver"); //gameover GameManager.instance.GameOver(); return; } if (other.gameObject.tag == "NormalFood") { //playSound AudioManager.instance.PlaySound("NormalFood"); //add points GameObject.FindObjectOfType <UIController>().AddPoints(1); //instantiate new food other.gameObject.GetComponent <Food>().AddNewOne(); } if (other.gameObject.tag == "SpecialFood") { //play sound AudioManager.instance.PlaySound("SpecialFood"); //add points GameObject.FindObjectOfType <UIController>().AddPoints(10); } //destroy existing Destroy(other.gameObject); //add snake tail SnakeTail tail = GameController.snake[GameController.snake.Count - 1] as SnakeTail; tail.AddElement(GameController.snake); }
private void Start() { mainCamera = Camera.main; componentRigidbody = GetComponent <Rigidbody2D>(); componentSnakeTail = GetComponent <SnakeTail>(); for (int i = 0; i < Length; i++) { componentSnakeTail.AddCircle(); } PointsText.SetText(Length.ToString()); }
/// <summary> /// Remove tail sections from the end equal to the amount given /// </summary> /// <param name="amount">Damage amount.</param> private void TakeDamage(int amount) { // repeat for damaged tail sections for (int i = health - 1; i >= health - amount; i--) { // remove the snaketail gameobject and any reference to it from the list SnakeTail tail = tailList [i]; tailList.RemoveAt(i); Destroy(tail.gameObject); } // reduce total health remaining health -= amount; }
// Start is called before the first frame update void Start() { photonView = GetComponent <PhotonView>(); componentRigidbody = GetComponent <Rigidbody2D>(); componentSnakeTail = GetComponent <SnakeTail>(); for (int i = 0; i < Length; i++) { componentSnakeTail.AddCircle(); } PointsText.SetText(Length.ToString()); }
public void ClientSetSpawnedTailToEnd(int _playerIndex) { if (nextTail != null) { nextTail.ClientSetSpawnedTailToEnd(_playerIndex); } else { nextTail = allTails.Find(x => (x.playerIndex == _playerIndex) && !x.isAttached); nextTail.isAttached = true; isEnd = false; nextTail.isEnd = true; } }
public SnakeTail GetPooledObject() { for (int i = 0; i < pooledObjects.Count; i++) { if (!pooledObjects[i].gameObject.activeInHierarchy) { return(pooledObjects[i]); } } SnakeTail snakeTail = Instantiate(objectToPool); snakeTail.gameObject.SetActive(false); pooledObjects.Add(snakeTail); snakeTail.transform.parent = instantiatedObjects.transform; return(snakeTail); }
//====================================================================== /// <summary> /// Create SnakePlayer with the specified data and pos. /// </summary> /// <returns>void</returns> /// <param name="data">Data.</param> /// <param name="pos">Position.</param> public void Create(PlayerData data, Vector3 pos) { LOG_TAG = LOG_TAG + "[" + data.id + "]"; m_data = data; m_context = GameManager.Instance.Context; //create gameobject for player m_container = new GameObject("SnakePlayer" + data.id); //create snake head m_head = EntityFactory.InstanceEntity <SnakeHead>(); m_head.Create(0, m_data, m_container.transform); //create snake tail m_tail = EntityFactory.InstanceEntity <SnakeTail>(); m_tail.Create(0, m_data, m_container.transform); //connect head and tail m_head.SetNext(m_tail); m_tail.SetPrev(m_head); //add default number of nodes to snake int initCount = m_data.snakeData.length; m_data.snakeData.length = 0; AddNodes(initCount); //create aiSnake if (m_data.ai > 0) { var ai = new AISnake(this); m_listCompoent.Add(ai); } //move to position where the snake is born MoveTo(pos); }
//====================================================================== /// <summary> /// Create SnakePlayer with the specified data and pos. /// </summary> /// <returns>void</returns> /// <param name="data">Data.</param> /// <param name="pos">Position.</param> public void Create(PlayerData data, Vector3 pos) { LOG_TAG = LOG_TAG + "[" + data.id + "]"; m_data = data; m_context = GameManager.Instance.Context; //创建用来显示视图的容器 m_container = new GameObject("SnakePlayer" + data.id); //创建Head m_head = EntityFactory.InstanceEntity <SnakeHead>(); m_head.Create(0, m_data, m_container.transform); //创建Tail m_tail = EntityFactory.InstanceEntity <SnakeTail>(); m_tail.Create(0, m_data, m_container.transform); //组合成一条蛇 m_head.SetNext(m_tail); m_tail.SetPrev(m_head); //增加默认数量的Node int initCount = m_data.snakeData.length; m_data.snakeData.length = 0; AddNodes(initCount); //创建AI if (m_data.ai > 0) { var ai = new PCSnakeAI(this); m_listCompoent.Add(ai); } //放置在出生坐标 MoveTo(pos); }
public Snake(int start_delay, Point position) { this.InitialWaitDelay = start_delay; this.CurrentLocation = position; this.DirectionChangeLocation = this.CurrentLocation; this.InternalLocation = position.ToVector2(); this.Position = this.InternalLocation; this.Direction = Directions.Right; this.State = (this.InitialWaitDelay == 0) ? States.Alive : States.Waiting; var texture = ContentHolder.Get(Settings.CurrentSnake); var region = new Region(texture, 0, 0, Snake.Size, Snake.Size, Snake.Size / 2, Snake.Size / 2); var sprite = new Sprite(region); this.AddSprite("main", sprite); this.AddColliderRectangle(Directions.Up.ToString(), -Snake.Size / 2, -Snake.Size / 2, Snake.Size, 1, false); this.AddColliderRectangle(Directions.Down.ToString(), -Snake.Size / 2, Snake.Size / 2 - 1, Snake.Size, 1, false); this.AddColliderRectangle(Directions.Left.ToString(), -Snake.Size / 2, -Snake.Size / 2, 1, Snake.Size, false); this.AddColliderRectangle(Directions.Right.ToString(), Snake.Size / 2 - 1, -Snake.Size / 2, 1, Snake.Size, true); this.AddColliderRectangle(this.MouthColliderName, -Snake.Size / 2, -Snake.Size / 2, Snake.Size, Snake.Size, true); for (int i = 0; i < 2; i++) { var tail = new SnakeTail { Position = new Vector2(this.Position.X - (i + 1) * Snake.Size, this.Position.Y), Depth = this.Depth + 1 + i }; Engine.SpawnInstance(tail); this.Tail.Add(tail); } for (int i = 0; i < this.Tail.Count * Snake.Size; i++) { this.SnakeLocations["x"].Add(this.CurrentLocation.X - i); this.SnakeLocations["y"].Add(this.CurrentLocation.Y); } }
public void SpawnTail(int count = 1, bool ext = true) { if (ext) { pendingTailCount += count; return; } GameObject tailObj = Instantiate(tailPrefab); tailObj.transform.position = head.GetNewEndTailLocation(tailObj.GetComponent <SnakeTail>()); tailObj.GetComponent <SnakeTail>().speed = speed; SnakeTail tail = tailObj.GetComponent <SnakeTail>(); tail.SetMatchId(networkMatchChecker.matchId); tail.playerIndex = playerIndex; NetworkServer.Spawn(tailObj); ClientSetSpawnedTailToEnd(playerIndex); pendingTailCount -= 1; }
/// <summary> /// Adds a tail piece at the given location, following the given object, and appends the tail list /// Also returns the newly created tail piece in case the next tail piece after that needs it /// </summary> /// <returns>Newly created tail piece</returns> /// <param name="following">The GameObject this piece must follow</param> /// <param name="position">Transform position of the new piece</param> /// <param name="rotation">Quaternion rotation of the new piece</param> /// <param name="tailNumber">Sequential tail number</param> private GameObject AddTailPiece(GameObject following, Vector3 position, Quaternion rotation, int tailNumber) { // create the new tail piece's actual gameobject GameObject newTail = Instantiate(snakeTailPrefab, position, rotation); // name the tail piece for later when doing searches by name newTail.name = "SnakeTail" + tailNumber; // get the snaketail script of the new tail SnakeTail newScript = newTail.GetComponent <SnakeTail> (); // set the script's variables as necessary (which object it follows, the snake head's script, // the distance between tail sections, which tail piece it is) newScript.FollowedObject = following; newScript.SnakeHeadScript = this; newScript.FollowDistance = tailDistance; newScript.TailNumber = tailNumber; // add the new tail script to the list of scripts tailList.Add(newScript); // exit the method returning the newly created gameobject return(newTail); }
void Start() { snakeTail = transform.parent.GetComponent <SnakeTail>(); }
/// <summary> /// Adds a new tail to this snake. /// </summary> public void AddTail() { SnakeTail tail = _firstTail.AddTail(); tail.transform.SetParent(transform); }
public void AddTail(SnakeTail tail) { tails.Add(tail); pieces.Add(tail); }