Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ sysinfo.txt
# Crashlytics generated file
crashlytics-build.properties

.DS_Store
19 changes: 16 additions & 3 deletions Assets/Alien.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ public class Alien : MonoBehaviour
public static Sprite alienSprite;

private const float speed = 0.02f;
/*
void Start()
{

}

}
*/
private void Update()
{
if (-PlayerController.VerticalCameraExtent > transform.position.y)
if (-PlayerController.VerticalCameraExtent > transform.position.y){
Destroy(gameObject);

ScoreManager.instance.RemoveLife();
}
}
void FixedUpdate()
{
Expand All @@ -28,6 +32,15 @@ void OnCollisionEnter2D(Collision2D col)
var pos = gameObject.transform.position;
Destroy(gameObject);
Destroy(Projectile.CreateExplosion(pos), 0.5f);


if( col.gameObject.name == "ship" ){
ScoreManager.instance.RemoveLife();
}
else
ScoreManager.instance.AddSinglePoint();

User.instance.AddAlienKilled(1);
}

public static GameObject Create(Vector3 position)
Expand Down
91 changes: 84 additions & 7 deletions Assets/AlienPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,116 @@
using System.Collections.Generic;
using UnityEngine;

/*
Alien Pool controls the level ( difficulty ) 1 to 5
> choose the background sprite
*/
public class AlienPool : MonoBehaviour
{
public static AlienPool instance;

static int level = 1;

public Sprite alienSprite;

public Sprite mothershipSprite;

private float time = 0.0f;
private static float time = 0.0f;

public float waveWaitTimeSecs = 3f;

// Start is called before the first frame update
public GameObject background;

public Sprite[] nebulaSprites;

private const int thresholdScoreSpawMothership = 50;

private void Awake()
{
instance = this;
}

/*
speed up spawns after 3' level
*/
void Start()
{
Alien.alienSprite = alienSprite;
Alien.alienSprite = alienSprite;
Alien_MotherShip.mothershipSprite = mothershipSprite;
refreshBackgroundLevel();

if(level > 3){
waveWaitTimeSecs -= (float) 0.2 * level;
Debug.Log($"Now aliens spawns quicker, every {waveWaitTimeSecs}s!");
}
}

// Update is called once per frame
/*
spawn aliens with random quantity range of ± 4, increasing on levelup
or spawn a single instance of mothership when reach xzy score, and after end the game
*/
void Update()
{
time += Time.deltaTime;

if (time >= waveWaitTimeSecs)
{
time = 0.0f;
SpawnWave(8);
int rInt = Random.Range(0 + level, 3 + level );

bool isMothershiopSpawned = GameObject.Find(Alien_MotherShip.mothership_GO_name) != null;
if( User.instance.getCurrentScore() > thresholdScoreSpawMothership && ! isMothershiopSpawned){
SpawnSingleMothership();
return;
}

SpawnWave(rInt);
}
}

private void SpawnWave(int numAliens)
{
var ypos = PlayerController.VerticalCameraExtent * (3 / 4.0f);
var horizontalExtent = PlayerController.HorizontalCameraExtent;
var space = horizontalExtent / numAliens;
for(int i =0; i < numAliens; i++)
{
Alien.Create(new Vector3(-horizontalExtent/2 + space * i, ypos, 0.5f));
var space = horizontalExtent / numAliens * i;
float randomPosX = new System.Random().Next(-1,1);
Alien.Create(new Vector3(-horizontalExtent/2 + space + randomPosX, ypos, 0.5f));
}
}

private void SpawnSingleMothership()
{
var ypos = PlayerController.VerticalCameraExtent * (3 / 4.0f);
var horizontalExtent = PlayerController.HorizontalCameraExtent;
var xpos = - horizontalExtent / 2 + new System.Random().Next(0,10);
Alien_MotherShip.Create(new Vector3( xpos, ypos, 0.5f));
}

public static int getLevel()
{
return level;
}

public static int setLevel(int lvl)
{
bool isValid = (lvl > 0) && (lvl < 6);
if( ! isValid )
Debug.Log($"setLevel() invalid value: {lvl}");
level = isValid ? lvl : 1;
return level;
}

public static int goNextLevel()
{
return setLevel(++level);
}

private void refreshBackgroundLevel()
{
int levelIndex = level -1;
if( levelIndex < nebulaSprites.Length )
background.GetComponent<SpriteRenderer>().sprite = nebulaSprites[levelIndex];
}
}
86 changes: 86 additions & 0 deletions Assets/Alien_MotherShip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Alien_MotherShip : MonoBehaviour
{
public static Sprite mothershipSprite;

private const float speed = 0.03f;

private int mothershipLife = 5;

private const int pointsMothership = 5;

public static string mothership_GO_name = "alienMothership";

void Start()
{
mothershipLife += (int)System.MathF.Floor(AlienPool.getLevel()/2);
Debug.Log($"Started a new mothership with life: {mothershipLife} \n position: {gameObject.transform.position.x}");
}

/*
if mothership passes the ship is gameover
*/
private void Update()
{
if (-PlayerController.VerticalCameraExtent > transform.position.y){
Destroy(gameObject);
ScoreManager.instance.GameOver();
}
}
void FixedUpdate()
{
gameObject.transform.Translate(0, -speed, 0.0f);
}

/*
touch mothership is gameover, defeat is gamewin
*/
void OnCollisionEnter2D(Collision2D col)
{
mothershipLife -= 1;

if( mothershipLife < 1 ){
Debug.Log("Mothership killed");
Destroy(gameObject);
Destroy(Projectile.CreateExplosion(gameObject.transform.position), 0.5f);
User.instance.AddAlienKilled(pointsMothership);
ScoreManager.instance.GameWin();
return;
}
Debug.Log($"OnCollisionEnter2D() mothership remaining life {mothershipLife}");

if( col.gameObject.name == "ship" ){
ScoreManager.instance.GameOver();
}
}

/*
increase horizontal box-collider for mothership to fit the sprite size (and game difficulty)
*/
public static GameObject Create(Vector3 position)
{
GameObject alien = new GameObject(mothership_GO_name);
SpriteRenderer renderer = alien.AddComponent<SpriteRenderer>();
renderer.sprite = mothershipSprite;
alien.transform.position = position;
var collider = alien.AddComponent<BoxCollider2D>();

collider.size = new Vector3(collider.size.x * 2, collider.size.y);



float scalingMotherShip = .3f;
if( User.instance.isLastLevel() ){
Debug.Log("User.instance.isLastLevel() bigger mothership");
scalingMotherShip = .6f;
}

alien.AddComponent<Alien_MotherShip>();
alien.transform.localScale = new Vector3(scalingMotherShip, scalingMotherShip, scalingMotherShip);

return alien;
}
}
11 changes: 11 additions & 0 deletions Assets/Alien_MotherShip.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading