-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayerController.cs
More file actions
executable file
·127 lines (104 loc) · 3.25 KB
/
Copy pathPlayerController.cs
File metadata and controls
executable file
·127 lines (104 loc) · 3.25 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
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Rigidbody2D egg;
public AudioClip shootAudio;
private Animator animator;
private float maxSpeed = 3.5f;
private float minFlySpeed = 2.2f;
private float minSpeed = 1.0f;
private float highestHeight = 4.3f;
private float lowestHeight = -2.65f;
private float incrementSpeed = 0.26f;// if click to give the power,increse the speed
private float decrementSpeed = 0.015f;//every frame decrese the speed
private float raiseUpSpeed = 0.25f;//once start fly, the player will higher and higher.
private float fallDownSpeed = 3.0f;//once no power to fly, the player will falldown.
private float fireRate = 0.5f;
private float nextFire = 0.0f;
public static float chickenSpeed;
public delegate void PlayerDelegate();
public event PlayerDelegate OnTouchEvent;
public event PlayerDelegate OnTapFoxEvent;
public event PlayerDelegate RunEvent;
public event PlayerDelegate FlyEvent;
private AudioSource source;
int i = 0;
void Awake(){
animator = GetComponent<Animator>();
source = GetComponent<AudioSource>();
}
// Use this for initialization
void Start () {
fireRate = 0.5f - (GameController.Level / 61.8f);
incrementSpeed = 0.26f + (GameController.Level / 61.8f);
raiseUpSpeed = 0.25f + (GameController.Level / 61.8f);
}
void OnPointedOutFox(Transform foxTransform){
if(Time.time > nextFire){
nextFire = Time.time + fireRate;
Rigidbody2D clone;
clone = Instantiate(egg, transform.position, transform.rotation) as Rigidbody2D;
clone.velocity = transform.TransformDirection(new Vector3(foxTransform.position.x - transform.position.x,foxTransform.position.y - transform.position.y,0.0f) * 3);
//source.clip = shootAudio;
//source.Play();
}
if(OnTapFoxEvent != null){
OnTapFoxEvent();
}
}
void OnDestroy(){
//Debug.Log ("Player die!");
}
void FixedUpdate(){
chickenSpeed = animator.GetFloat ("ChickenSpeed");
if(Input.GetKeyUp(KeyCode.F)){
//once click to give the player power, raise the speed up.
if(chickenSpeed <= maxSpeed){
chickenSpeed += incrementSpeed;
animator.SetFloat("ChickenSpeed",chickenSpeed);
}
if(OnTouchEvent != null){
OnTouchEvent();
Debug.Log("XXXX");
}
}
i = 0;
while (i < Input.touchCount) {
if (Input.GetTouch(i).phase == TouchPhase.Began){
//once click to give the player power, raise the speed up.
if(Input.GetTouch(i).position.x < (Screen.width / 2)){
if(chickenSpeed <= maxSpeed){
chickenSpeed += incrementSpeed;
animator.SetFloat("ChickenSpeed",chickenSpeed);
}
}
}
++i;
if(OnTouchEvent != null){
OnTouchEvent();
}
}
if(chickenSpeed >= minSpeed){
//always drag down the speed
chickenSpeed -= decrementSpeed;
animator.SetFloat("ChickenSpeed",chickenSpeed);
}
if (chickenSpeed > minFlySpeed) {
if(FlyEvent != null){
FlyEvent();
}
if (transform.position.y < highestHeight) {
//start raise up
transform.Translate (Vector2.up * raiseUpSpeed * Time.deltaTime);
}
} else {
if(RunEvent != null){
RunEvent();
}
if (transform.position.y > lowestHeight) {
//start fall down
transform.Translate (-Vector2.up * fallDownSpeed * Time.deltaTime);
}
}
}
}