Skip to content

Commit

Permalink
Some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukant committed Jun 14, 2020
1 parent 273bc0d commit 9349aa4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Assets/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ee7c3be18d5ee4c1c8a618f7a81face1, type: 3}
m_Name:
m_EditorClassIdentifier:
isHurt: 0
health: 100
initialHealth: 100
explosion: {fileID: 6120152699592841991, guid: 939e38c880d4e40f8b9a5c33a4e1c9fe,
type: 3}
--- !u!1001 &6560008044271699336
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
30 changes: 16 additions & 14 deletions Assets/Scripts/CameraMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,33 @@ public class CameraMovement : MonoBehaviour {

// Use this for initialization
void Start () {
playerTransform = GameObject.Find("Player(Clone)").transform;
thisCamera = GetComponent<Camera>();
cameraWidth = thisCamera.aspect * thisCamera.orthographicSize * 2;
}

public void Run() {
playerTransform = GameObject.Find("Player(Clone)").transform;
goldSpeed = 0.1f;
}

// LateUpdate is called after Update each frame
void FixedUpdate () {
// right quarter
if (playerTransform.position.x > transform.position.x + cameraWidth/3
&& speed < 20f * goldSpeed) {
speed += 0.01f;
// left quarter
} else if (playerTransform.position.x < transform.position.x - cameraWidth/4 &&
speed > 0.5 * goldSpeed) {
speed -= 0.01f;
}
else if (Math.Abs(speed - goldSpeed) > 0.01f) {
speed += 0.005f * (speed < goldSpeed ? 1 : -1);
}
if (goldSpeed > 0 && playerTransform != null) {
// right quarter
if (playerTransform.position.x > transform.position.x + cameraWidth/3
&& speed < 5 * goldSpeed) {
speed += 0.01f;
// left quarter
} else if (playerTransform.position.x < transform.position.x - cameraWidth/4 &&
speed > 0.5 * goldSpeed) {
speed -= 0.01f;
}
else if (Math.Abs(speed - goldSpeed) > 0.01f) {
speed += 0.005f * (speed < goldSpeed ? 1 : -1);
}

transform.position = transform.position + new Vector3(speed, 0, 0);
transform.position = transform.position + new Vector3(speed, 0, 0);
}
}

public void Stop() {
Expand Down
10 changes: 10 additions & 0 deletions Assets/Scripts/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@ public class GameController : MonoBehaviour {
void Start() {
level1 = Instantiate(Level1Prefab, Vector3.zero, Quaternion.identity).GetComponent<LevelScript>();
level1.Load();
level1.enabled = false;
scoreText = Score.GetComponentInChildren<Text>();
MusicController.SoundController(MusicController.SOUNDS.MENU, true);
}

public void RunLevel1() {
level1.Run();
level1.enabled = true;
MainMenu.SetActive(false);
Score.SetActive(true);
MusicController.SoundController(MusicController.SOUNDS.MENU, false);
MusicController.SoundController(MusicController.SOUNDS.GAME_INTRO, true);
}

public void RestartLevel1() {
level1.mcMovement.Stop();
StartCoroutine(DelayRestart());

}

IEnumerator DelayRestart()
{
yield return new WaitForSeconds(4f);
resetScore();
level1.Restart();
}
Expand Down
6 changes: 4 additions & 2 deletions Assets/Scripts/LevelScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LevelScript : MonoBehaviour {


private GameObject mainCamera;
private CameraMovement mcMovement;
public CameraMovement mcMovement;
private GameObject movingBackground, player;
private Vector3 initialPos;

Expand All @@ -22,6 +22,7 @@ public class LevelScript : MonoBehaviour {
private float lastCameraPos;
private int generations = 0;
private float generationRangeY = 4;
private Vector3 cameraInitPos;

public void Start() {
initialPos = transform.position;
Expand All @@ -33,6 +34,7 @@ public void Load() {
generateEnemies(generationOffest, enemiesGenerationLength + generationOffest);

mainCamera = GameObject.Find("Main Camera");
cameraInitPos = mainCamera.transform.position;
mcMovement = mainCamera.GetComponent<CameraMovement>();
mcMovement.Stop();
lastCameraPos = 0f;
Expand All @@ -47,7 +49,7 @@ public void Restart() {
Destroy(player);

transform.position = initialPos;
mainCamera.transform.position = initialPos;
mainCamera.transform.position = cameraInitPos;

Load();
Run();
Expand Down
14 changes: 13 additions & 1 deletion Assets/Scripts/PlayerHealth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ public class PlayerHealth : MonoBehaviour {

public float health = 100;
public float initialHealth = 100;

public GameObject explosion;
private void OnCollisionEnter2D(Collision2D other) {
var damage = other.transform.gameObject.GetComponent<Damage>();
isHurt = damage != null;
if (damage != null) {
health -= damage.damage;

if (health < 0) {
Die();
}
}
}

void Die() {
var gc = GameObject.Find("GameController").GetComponent<GameController>();
gc.RestartLevel1();

Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}

0 comments on commit 9349aa4

Please sign in to comment.