Skip to content

Commit 1f7f30a

Browse files
authored
Got acceleration powerup working
1 parent e47fbbf commit 1f7f30a

8 files changed

+308
-17
lines changed

BatteryPlayerBehaviour.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class BatteryPlayerBehaviour : MonoBehaviour
6+
{
7+
// Start is called before the first frame update
8+
float maxCharge = 100.0f;
9+
float juce = 100.0f;
10+
float delta = 0.001f;
11+
float chargeAmount = 25.0f;
12+
void Start()
13+
{
14+
15+
}
16+
17+
public bool isEmpty() {
18+
return ( juce == 0);
19+
}
20+
21+
public void partCharge() {
22+
float newCharge = juce+chargeAmount;
23+
if (newCharge < maxCharge) {
24+
juce = newCharge;
25+
} else {
26+
juce = maxCharge;
27+
}
28+
}
29+
30+
// Update is called once per frame
31+
void Update()
32+
{
33+
float newCharge = juce - delta;
34+
if ( newCharge > 0) {
35+
juce = newCharge;
36+
} else {
37+
juce = 0;
38+
}
39+
}
40+
}

DronePlayer.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using UnityEngine;
2+
public class DronePlayer : MonoBehaviour
3+
{
4+
public float maxSpeed = 5.0f;
5+
public int score = 0;
6+
public float getMaxSpeed()
7+
{
8+
return maxSpeed;
9+
}
10+
public void setMaxSpeed(float speed)
11+
{
12+
maxSpeed = speed;
13+
}
14+
public /*Score*/ void getScore()
15+
{
16+
}
17+
18+
// Declare Flight Controller Variables
19+
public float forwardSpeed = 0.0f, strafeSpeed = 0.0f, hoverSpeed = 0.0f;
20+
private float activeForwardSpeed, activeStrafeSpeed, activeHoverSpeed;
21+
22+
// Start is called before the first frame update
23+
void Start()
24+
{
25+
print("Drone Simualtion ");
26+
}
27+
28+
public void Forward()
29+
{
30+
//Debug.Log("Forward Function Called");
31+
transform.position += transform.forward * Time.deltaTime * forwardSpeed;
32+
}
33+
public void Backward()
34+
{
35+
//Debug.Log("Backward Function Called");
36+
transform.position += transform.forward * Time.deltaTime * -forwardSpeed;
37+
}
38+
public void Right()
39+
{
40+
//Debug.Log("Right Function Called");
41+
transform.position += transform.right * Time.deltaTime * strafeSpeed;
42+
}
43+
public void Left()
44+
{
45+
//Debug.Log("Left Function Called");
46+
transform.position += transform.right * Time.deltaTime * -strafeSpeed;
47+
}
48+
public void Up()
49+
{
50+
//Debug.Log("Up Function Called");
51+
transform.position += transform.up * Time.deltaTime * hoverSpeed;
52+
}
53+
public void Down()
54+
{
55+
//Debug.Log("Down Function Called");
56+
transform.position += transform.up * Time.deltaTime * -hoverSpeed;
57+
}
58+
59+
60+
// Update is called once per frame
61+
void Update()
62+
{
63+
forwardSpeed = maxSpeed;
64+
strafeSpeed = maxSpeed;
65+
hoverSpeed = maxSpeed;
66+
//Debug.Log("forwardSpeed = "+forwardSpeed);
67+
//activeForwardSpeed = Input.GetAxisRaw("Vertical") * forwardSpeed;
68+
//activeStrafeSpeed = Input.GetAxisRaw("Horizontal") * strafeSpeed;
69+
//activeHoverSpeed = Input.GetAxisRaw("Hover") * hoverSpeed;
70+
//transform.position += transform.forward * activeForwardSpeed * Time.deltaTime;
71+
if (Input.GetKey(KeyCode.W))
72+
{
73+
//Debug.Log("Pressed w ");
74+
Forward();
75+
76+
}
77+
else if (Input.GetKey(KeyCode.S))
78+
{
79+
//Debug.Log("Pressed s ");
80+
Backward();
81+
82+
}
83+
else if (Input.GetKey(KeyCode.A))
84+
{
85+
//Debug.Log("Pressed a ");
86+
Left();
87+
88+
}
89+
else if (Input.GetKey(KeyCode.D))
90+
{
91+
//Debug.Log("Pressed d ");
92+
Right();
93+
94+
}
95+
else if (Input.GetKey(KeyCode.Space))
96+
{
97+
//Debug.Log("Pressed space ");
98+
Up();
99+
100+
}
101+
else if (Input.GetKey(KeyCode.LeftAlt))
102+
{
103+
//Debug.Log("Pressed left alt ");
104+
Down();
105+
106+
}
107+
}
108+
}

Drone_Movement.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44

@@ -16,32 +16,32 @@ void Start()
1616

1717
public void Forward()
1818
{
19-
Debug.Log("Forward Function Called");
19+
//Debug.Log("Forward Function Called");
2020
transform.position += transform.forward * Time.deltaTime * forwardSpeed;
2121
}
2222
public void Backward()
2323
{
24-
Debug.Log("Backward Function Called");
24+
//Debug.Log("Backward Function Called");
2525
transform.position += transform.forward * Time.deltaTime * -forwardSpeed;
2626
}
2727
public void Right()
2828
{
29-
Debug.Log("Right Function Called");
29+
//Debug.Log("Right Function Called");
3030
transform.position += transform.right * Time.deltaTime * strafeSpeed;
3131
}
3232
public void Left()
3333
{
34-
Debug.Log("Left Function Called");
34+
//Debug.Log("Left Function Called");
3535
transform.position += transform.right * Time.deltaTime * -strafeSpeed;
3636
}
3737
public void Up()
3838
{
39-
Debug.Log("Up Function Called");
39+
//Debug.Log("Up Function Called");
4040
transform.position += transform.up * Time.deltaTime * hoverSpeed;
4141
}
4242
public void Down()
4343
{
44-
Debug.Log("Down Function Called");
44+
//Debug.Log("Down Function Called");
4545
transform.position += transform.up * Time.deltaTime * -hoverSpeed;
4646
}
4747

@@ -55,37 +55,37 @@ void Update()
5555
//transform.position += transform.forward * activeForwardSpeed * Time.deltaTime;
5656
if (Input.GetKey(KeyCode.W))
5757
{
58-
Debug.Log("Pressed w ");
58+
//Debug.Log("Pressed w ");
5959
Forward();
6060

6161
}
6262
else if (Input.GetKey(KeyCode.S))
6363
{
64-
Debug.Log("Pressed s ");
64+
//Debug.Log("Pressed s ");
6565
Backward();
6666

6767
}
6868
else if (Input.GetKey(KeyCode.A))
6969
{
70-
Debug.Log("Pressed a ");
70+
//Debug.Log("Pressed a ");
7171
Left();
7272

7373
}
7474
else if (Input.GetKey(KeyCode.D))
7575
{
76-
Debug.Log("Pressed d ");
76+
//Debug.Log("Pressed d ");
7777
Right();
7878

7979
}
8080
else if (Input.GetKey(KeyCode.Space))
8181
{
82-
Debug.Log("Pressed space ");
82+
//Debug.Log("Pressed space ");
8383
Up();
8484

8585
}
8686
else if (Input.GetKey(KeyCode.LeftAlt))
8787
{
88-
Debug.Log("Pressed left alt ");
88+
//Debug.Log("Pressed left alt ");
8989
Down();
9090

9191
}

GenItems.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,69 @@ public class GenItems : MonoBehaviour
77
public Rigidbody prefab; //TODO: change to powerup or item class when class exists
88
private Vector3 spawnPos;
99
private Vector3 velocity;
10+
private const float LIFETIME = 5.0f;
11+
private const float SIZE = 40.0f;
12+
private const float SIZE_RATE = 2.0f;
1013
// Start is called before the first frame update
1114
void Start()
1215
{
1316
spawnPos = this.transform.position;
14-
spawnPos.z += 50; //will be 50m away
17+
spawnPos += this.transform.forward * 50; //will be 50m away
1518
InvokeRepeating("createPowerUp", 0.5f, 2.0f);
1619
}
1720

1821
// Update is called once per frame
1922
void Update()
2023
{
2124
spawnPos = this.transform.position;
22-
spawnPos.z += 50; //will be 50m away
25+
spawnPos += this.transform.forward * 50; //will be 50m away
2326
}
2427

2528
void createPowerUp()
2629
{
2730
Quaternion rotate = Quaternion.Euler(Random.Range(-15.0f, 15.0f), Random.Range(-15.0f, 15.0f),0);
28-
Rigidbody powerUp = Instantiate(prefab, new Vector3(spawnPos.x, spawnPos.y, spawnPos.z), Quaternion.identity);
31+
Rigidbody powerUp = Instantiate(prefab, new Vector3(spawnPos.x, spawnPos.y, spawnPos.z), Quaternion.Euler(0.0f, 90.0f, 0.0f));
32+
powerUp.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
2933
powerUp.velocity = rotate * (new Vector3(0f,0f,Random.Range(-20.0f, -10.0f)));
30-
Destroy(powerUp.gameObject, 5.0f);
34+
StartCoroutine(grow(powerUp.gameObject));
35+
StartCoroutine(shrink(powerUp.gameObject));
36+
StartCoroutine(destroyItem(powerUp.gameObject));
37+
}
38+
39+
IEnumerator grow(GameObject obj)
40+
{
41+
for (float s = 0; s <= SIZE; s += SIZE_RATE)
42+
{
43+
if (obj != null)
44+
{
45+
obj.transform.localScale = new Vector3(s, s, s);
46+
}
47+
yield return null;
48+
}
49+
}
50+
51+
IEnumerator shrink(GameObject obj)
52+
{
53+
if (obj != null)
54+
{
55+
yield return new WaitForSeconds(LIFETIME - 0.5f);
56+
for (float s = SIZE; s >= 0.0f; s -= SIZE_RATE)
57+
{
58+
if (obj != null)
59+
{
60+
obj.transform.localScale = new Vector3(s, s, s);
61+
}
62+
yield return null;
63+
}
64+
}
65+
}
66+
67+
IEnumerator destroyItem(GameObject obj)
68+
{
69+
yield return new WaitForSeconds(LIFETIME);
70+
if(obj != null)
71+
{
72+
Destroy(obj);
73+
}
3174
}
3275
}

IncreaseMaxSpeed.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class IncreaseMaxSpeed : PowerUpObject {
6+
// Start is called before the first frame update
7+
public float strength = 5.0f;
8+
void Start()
9+
{
10+
11+
}
12+
13+
public override void performPowerupOnPlayer(DronePlayer player){
14+
player.setMaxSpeed(player.getMaxSpeed() + strength);
15+
}
16+
17+
// Update is called once per frame
18+
void Update()
19+
{
20+
21+
}
22+
}

PlayerCollision.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class PlayerCollision : MonoBehaviour
6+
{
7+
// Start is called before the first frame update
8+
void Start()
9+
{
10+
11+
}
12+
void OnCollisionEnter(Collision collision){
13+
if(collision.gameObject.tag == "player") {
14+
// collision is with the player.
15+
// get the player interface
16+
DronePlayer player = collision.gameObject.GetComponent(typeof(DronePlayer)) as DronePlayer;
17+
if( player != null) {
18+
PowerUpObject obj = (this.gameObject.GetComponent(typeof(PowerUpObject)) as PowerUpObject);
19+
if( obj != null ){
20+
obj.performPowerupOnPlayer(player);
21+
}
22+
}
23+
Destroy(gameObject);
24+
}
25+
}
26+
27+
private void OnTriggerEnter(Collider other)
28+
{
29+
if(other.gameObject.tag == "MainCamera")
30+
{
31+
// collision is with the player.
32+
// get the player interface
33+
DronePlayer player = other.GetComponent(typeof(DronePlayer)) as DronePlayer;
34+
if (player != null)
35+
{
36+
PowerUpObject obj = (this.gameObject.GetComponent(typeof(PowerUpObject)) as PowerUpObject);
37+
if (obj != null)
38+
{
39+
obj.performPowerupOnPlayer(player);
40+
}
41+
}
42+
Destroy(gameObject);
43+
}
44+
}
45+
46+
// Update is called once per frame
47+
void Update()
48+
{
49+
50+
}
51+
}

PowerUpObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using UnityEngine;
2+
public abstract class PowerUpObject : MonoBehaviour
3+
{
4+
public abstract void performPowerupOnPlayer(DronePlayer player);
5+
}

0 commit comments

Comments
 (0)