This repository has been archived on 2026-04-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ludumdare41/Assets/Scripts/Cannon.cs
T
2018-04-22 23:36:11 +10:00

48 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cannon : MonoBehaviour {
SpriteRenderer spriteRenderer;
public enum Direction {
NORTH, EAST, SOUTH, WEST
};
public Direction direction;
public GameObject pelletPrefab;
public float delay;
float speed = 3f;
public Sprite sprite; //TMP, hopefully
void Awake() {
spriteRenderer = GetComponent<SpriteRenderer> ();
spriteRenderer.sprite = sprite;
StartCoroutine (FireOnTimer (delay));
}
IEnumerator FireOnTimer(float seconds) {
while(true) {
yield return new WaitForSeconds (seconds);
GameObject go = Instantiate (pelletPrefab);
go.transform.position = transform.position;
switch (direction) {
case Direction.NORTH:
go.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, speed), ForceMode2D.Impulse);
break;
case Direction.EAST:
go.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (speed, 0), ForceMode2D.Impulse);
break;
case Direction.SOUTH:
go.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, -speed), ForceMode2D.Impulse);
break;
case Direction.WEST:
go.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (-speed, 0), ForceMode2D.Impulse);
break;
}
}
}
}