Repository navigation
Expand file tree
/
Copy pathDog.cs
More file actions
209 lines (176 loc) · 6.57 KB
/
Copy pathDog.cs
File metadata and controls
209 lines (176 loc) · 6.57 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
202
203
204
205
206
207
208
209
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
//using UnityEngine.Advertisements;
public class Dog : MonoBehaviour {
private const string line = "--------------------------------------------------------------------------";
[SerializeField] private Text levelIndication;
[SerializeField] private Text ScoreDisplay;
[SerializeField] private Text BestScoreDisplay;
public GameObject bone;
public GameObject AwardFace;
private BoneThrowing boneScript;
public GameObject background;
private BoxCollider2D boneCollider;
private BoxCollider2D dogCollider;
public GameObject MissThrowFace;
public float PlaceBoneOffsetY = 0.6f;
public float PlaceBoneOffsetX = 1.1f;
public float PlaceBoneTime = 0.27f;
public float MovementSpeed = 5.0f;
private Animator anim;
private bool GotBone;
private float rightBound;
private float leftBound;
private float Randomized_X;
private float DogBodyLength = 1.2f;
private float AcceptableRange = 0.1f;
private int level;
private int score;
private int bestScore;
private string dataPath ;
private const int MAXLEVEL = 10;
private const int SCORESINLEVEL = 3;
private const float LEVELHEIGHT = 0.8f;
private int adsCount = 5;
void Start(){
anim = GetComponent<Animator>();
boneCollider = bone.GetComponent<BoxCollider2D> ();
boneScript = bone.GetComponent<BoneThrowing> ();
dogCollider = GetComponent<BoxCollider2D> ();
dataPath = Application.persistentDataPath + "/playerInfo.dat";
LoadBestScore();
reStart ();
leftBound = background.GetComponent<SpriteRenderer>().bounds.center.x - background.GetComponent<SpriteRenderer>().bounds.size.x / 2.0f + DogBodyLength;
rightBound = background.GetComponent<SpriteRenderer>().bounds.center.x + background.GetComponent<SpriteRenderer>().bounds.size.x / 2.0f - DogBodyLength;
}
public void reStart(){
adsCount++;
// if (Advertisement.IsReady () && adsCount >= (10 + bestScore/2)) {
if (adsCount >= (10 + bestScore/2)) {
//Advertisement.Show ();
adsCount = 0;
}
bone.transform.position = new Vector3 (Randomized_X, -3.35f, bone.transform.position.z);
transform.position = new Vector3 (Randomized_X, -7.635269f, transform.position.z);
score = -1;
level = 1;
levelIndication.transform.position = new Vector3 (-11f,-8.7f,levelIndication.transform.position.z);
boneScript.GotCatch = true;
MissThrowFace.SetActive (false);
boneScript.restartSreen.enabled = false;
}
void PlaceBone(){
//Place bone in the new location
bone.transform.rotation = new Quaternion ();
if(transform.localScale.x < 0)
bone.transform.position = new Vector3
(transform.position.x-PlaceBoneOffsetX, transform.position.y-PlaceBoneOffsetY, transform.position.z);
else
bone.transform.position = new Vector3
(transform.position.x+PlaceBoneOffsetX, transform.position.y-PlaceBoneOffsetY, transform.position.z);
bone.SetActive (true);
// Get Ready to Run
// 1. get next location
// 2. update animation
// 3. ignore collision
Randomized_X = UnityEngine.Random.Range (leftBound, rightBound);
while(Mathf.Abs(Randomized_X - bone.transform.position.x) < DogBodyLength*3 || Mathf.Abs(Randomized_X - bone.transform.position.x) > 10f+level*2.0f)
Randomized_X = UnityEngine.Random.Range (leftBound, rightBound);
GotBone = true;
anim.SetTrigger ("Run");
Physics2D.IgnoreCollision (boneCollider, dogCollider, true);
}
void OnCollisionEnter2D(Collision2D hit)
{
//if the dog got the bone, and the bone did past the height level
if (hit.gameObject == bone && boneScript.pastLevel) {
//show award face
AwardFace.SetActive(true);
AwardFace.transform.position = new Vector2(transform.position.x,transform.position.y+1.3f);
Invoke ("disableAwardFace",0.7f);
anim.SetTrigger("GotBone");
bone.SetActive(false);
Invoke("PlaceBone",PlaceBoneTime);
boneScript.GotCatch = true;
//increment score, update levelIndication if score met
score++;
if(score % SCORESINLEVEL == 0 && level <= MAXLEVEL && score != 0){
level++;
levelIndication.transform.position = new Vector3(levelIndication.transform.position.x,levelIndication.transform.position.y + LEVELHEIGHT,levelIndication.transform.position.z);
}
//update score in the restart screen
ScoreDisplay.text = "Score: " + score;
if(score > bestScore)
bestScore = score;
BestScoreDisplay.text = "Best: " + bestScore;
SaveBestScore();
}
}
void disableAwardFace(){
AwardFace.SetActive (false);
}
void Run(){
//running
if (transform.position.x > Randomized_X) {
transform.Translate (new Vector3 (-Time.deltaTime * MovementSpeed, 0, 0));
transform.localScale = new Vector3 (-Mathf.Abs (transform.localScale.x), transform.localScale.y, transform.localScale.z);
} else {
transform.Translate (new Vector3 (Time.deltaTime * MovementSpeed, 0, 0));
transform.localScale = new Vector3 (Mathf.Abs (transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
//stop
if (Mathf.Abs (transform.position.x - Randomized_X) < AcceptableRange) {
GotBone = false;
anim.SetTrigger("GotBone");
Physics2D.IgnoreCollision (boneCollider, dogCollider, false);
boneScript.pastLevel = false;
boneScript.ReadyToThrow = true;
}
}
// Update is called once per frame
void Update () {
AwardFace.transform.position = new Vector2(AwardFace.transform.position.x,AwardFace.transform.position.y+0.01f);
if (GotBone) {
Run ();
return;
}
//Facing the bone
if (bone.transform.position.x > transform.position.x)
transform.localScale = new Vector3 (Mathf.Abs (transform.localScale.x), transform.localScale.y, transform.localScale.z);
else
transform.localScale = new Vector3 (-Mathf.Abs (transform.localScale.x), transform.localScale.y, transform.localScale.z);
//update levelIndication
if (level <= MAXLEVEL)
levelIndication.text = "Level " + level + " " + line;
else
levelIndication.text = score + " " + line;
}
private void SaveBestScore(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (dataPath, FileMode.OpenOrCreate);
PlayerData data = new PlayerData ();
data.adsCount = adsCount;
data.score = bestScore;
bf.Serialize(file,data);
file.Close();
}
private void LoadBestScore(){
if(File.Exists(dataPath)){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (dataPath,FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
adsCount = data.adsCount;
bestScore = data.score;
}
}
}
[Serializable]
class PlayerData{
public int score;
public int adsCount;
}