-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerController.cs
More file actions
201 lines (164 loc) · 5.46 KB
/
Copy pathPlayerController.cs
File metadata and controls
201 lines (164 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.Analytics;
using UnityEditor;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public static PlayerController Instance;
private Vector3 playerSpos;
private int level;
public int lives = 3;
public float speed;
public float thrust;
public int jumps;
private int jumpsC;
public bool isLanded;
public bool isJumping;
public Rigidbody playerRB;
public Vector3 moveDir;
public Vector3 deathPos;
public TextAsset myDeathPoss;
public BoxCollider bottom;
Material m_Material;
GameController controller;
DataRecorder dataRecorder;
// Use this for initialization
void Start()
{
playerSpos = gameObject.transform.position;
level = SceneManager.GetActiveScene().buildIndex;
print("level = " + level);
m_Material = GetComponent<Renderer>().material;
m_Material.color = Color.blue;
playerRB = GetComponent<Rigidbody>();
controller = FindObjectOfType<GameController>();
}
private void Awake()
{
Instance = this;
}
// Update is called once per frame
void Update()
{
playerRB.AddForce(speed * Time.deltaTime, 0, 0, ForceMode.Acceleration);
}
private void OnTriggerEnter(Collider other)
{
//print("I collide");
if (other.gameObject.tag != gameObject.tag && other.gameObject.tag != "Finish")
{
if (lives > 0)
{
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
GetDeathPos();
gameObject.transform.position = playerSpos;
this.gameObject.tag = "blue";
m_Material.color = Color.blue;
lives--;
WriteDeathPos();
print("deded" + lives);
}
}
//else if (other.gameObject.tag == "Finish" && level == 3)
//{
// print("being triggered");
// //SceneManager.LoadScene(0, LoadSceneMode.Single);
//}
isLanded = true;
}
/*private void OnTriggerExit(Collider other)
{
}*/
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag != gameObject.tag && lives > 0)
{
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
gameObject.transform.position = playerSpos;
this.gameObject.tag = "blue";
m_Material.color = Color.blue;
lives--;
GetDeathPos();
WriteDeathPos();
print("you died at " + deathPos);
print("deded" + lives);
}
else if (other.gameObject.tag != gameObject.tag && lives <= 0)
{
print("deded");
lives = 3;
OnGameOver();
GetDeathPos();
WriteDeathPos();
SceneManager.LoadScene(0);
}
}
private void OnTriggerExit(Collider other)
{
if (isJumping == false)
{
//print("I no collide");
isLanded = false;
}
}
private void FixedUpdate()
{
if (Input.GetMouseButtonDown(0) && jumpsC > 0)
{
playerRB.AddForce(0, thrust, 0, ForceMode.Impulse);
isJumping = true;
jumpsC--;
}
if(Input.GetMouseButtonDown(1) && m_Material.color == Color.blue)
{
this.gameObject.tag = "green";
m_Material.color = Color.green;
}else if (Input.GetMouseButtonDown(1) && m_Material.color == Color.green)
{
this.gameObject.tag = "blue";
m_Material.color = Color.blue;
}
if(isLanded == true)
{
isJumping = false;
jumpsC = jumps;
}
}
public void GetDeathPos()
{
deathPos = this.gameObject.transform.position;
}
public void OnGameOver()
{
int deathLevel = SceneManager.GetActiveScene().buildIndex;
float deathTime = Mathf.Round(Time.timeSinceLevelLoad);
Analytics.CustomEvent("gameOver", new Dictionary<string, object>
{
{ "death level ", deathLevel },
{ "death distance ", controller.dist },
{ "death time (s)", deathTime }
});
print("gameOverCalled");
}
public void WriteDeathPos()
{
//dataRecorder.appendToEnd(deathPos);
////DataRecorder mydata = new DataRecorder;
////mydata.appendToEnd()
string path = "Assets/Resources/DeathPositions.txt";
//Write death position vector to text file
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine(deathPos.x + "," + deathPos.y + "," + deathPos.z);
//writer.WriteLine(deathPos + ";");your version
//writer.WriteLine(deathPos);
writer.Close();
////Re-import the file to update the reference in the editor
AssetDatabase.ImportAsset("Assets/Resources/DeathPositions");
TextAsset asset = Resources.Load<TextAsset>("Assets/Resources/DeathPositions.txt");
////Print the text from the file
print(myDeathPoss.text);
}
}