little opt
This commit is contained in:
parent
ff1350e85c
commit
02f1d85e1f
|
|
@ -14,10 +14,16 @@ public class SnakeController : MonoBehaviour
|
|||
public Vector2 fieldSize= new Vector2(160,90);
|
||||
float moveTimer;
|
||||
|
||||
float topEdge => transform.position.y + fieldSize.y /2f;
|
||||
float botEdge => transform.position.y -fieldSize.y /2f;
|
||||
float leftEdge =>transform.position.x-fieldSize.x/2f;
|
||||
float rightEdge => transform.position.x + fieldSize.x/2f;
|
||||
|
||||
|
||||
void Start(){
|
||||
tail = new List<Vector2>();
|
||||
for(int i=0; i < startingLength; i++){
|
||||
Vector3 pos =-(Vector2.right * i);
|
||||
Vector3 pos = (Vector2)transform.position-(Vector2.right * i);
|
||||
tail.Add(pos);
|
||||
GameObject newPiece = Instantiate(snakePiecePrefab,pos, Quaternion.identity );
|
||||
snakePieces.Add(newPiece.transform);
|
||||
|
|
@ -27,6 +33,7 @@ public class SnakeController : MonoBehaviour
|
|||
void Update()
|
||||
{
|
||||
Move();
|
||||
|
||||
if(Input.GetKeyDown(KeyCode.RightArrow)){
|
||||
ChangeDir(Vector2.right);
|
||||
}else if(Input.GetKeyDown(KeyCode.LeftArrow)){
|
||||
|
|
@ -74,16 +81,14 @@ public class SnakeController : MonoBehaviour
|
|||
}
|
||||
|
||||
tail[0] = curPosition + curDirection;
|
||||
float leftEdge =transform.position.x-fieldSize.x/2f;
|
||||
float rightEdge = transform.position.x + fieldSize.x/2f;
|
||||
|
||||
if(tail[0].x > rightEdge){
|
||||
tail[0]= new Vector2(leftEdge,tail[0].y);
|
||||
}else if(tail[0].x <leftEdge){
|
||||
tail[0]= new Vector2(rightEdge,tail[0].y);
|
||||
}
|
||||
|
||||
float topEdge = transform.position.y + fieldSize.y /2f;
|
||||
float botEdge = transform.position.y -fieldSize.y /2f;
|
||||
|
||||
|
||||
if(tail[0].y > topEdge){
|
||||
tail[0] = new Vector2(tail[0].x, botEdge);
|
||||
|
|
@ -93,8 +98,7 @@ public class SnakeController : MonoBehaviour
|
|||
snakePieces[0].position = tail[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OnDrawGizmos() {
|
||||
Gizmos.DrawWireCube(transform.position, new Vector3(fieldSize.x,fieldSize.y));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user