This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerController.cs
219 lines (187 loc) · 5.56 KB
/
PlayerController.cs
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
210
211
212
213
214
215
216
217
218
219
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.AI;
//Author: Raul Rojas
//CSE 1302
//Project One
//6-23-2017
public class PlayerController : MonoBehaviour
{
#region Fields
#region Public
public float speed = 1;
public RaycastHit aimpoint;
public KeyCode Fire;
public KeyCode changeWeapon;
public KeyCode reload;
public int maxHealth = 100;
public bool godMode;
#endregion
#region Private
private List<GunBase> inventory;
private int currentWeapon;
private int health;
private bool isAlive;
private bool pressedReload;
#endregion
#endregion
#region Properties
public List<GunBase> Inventory
{
get { return inventory; }
}
public int Health
{
get { return this.health; }
set { this.health = value; }
}
public int CurrentWeapon
{
get { return this.currentWeapon; }
}
public bool IsAlive
{
get { return this.isAlive; }
}
#endregion
#region MonoBehaviors
void Start()
{
inventory = new List<GunBase>();
currentWeapon = 0;
health = maxHealth;
isAlive = true;
}
void Update()
{
if (isAlive)
{
move();
look();
shoot();
weaponCycle();
}
}
#endregion
#region Public Methods
public void damage(int dmg)
{
if (!godMode && isAlive)
{
health -= dmg;
if (health <= 0)
{
health = 0;
isAlive = false;
AudioManager.instance.Stop(GameStructs.ClipName.GameMusic);
AudioManager.instance.Play(GameStructs.ClipName.PlayerDeathMusic);
AudioManager.instance.Play(GameStructs.ClipName.PlayerDeath);
}
}
}
public void heal(int healAmt)
{
health += healAmt;
if (health > maxHealth)
{
health = maxHealth;
}
}
public void addToInventory(GunBase gun)
{
AudioManager.instance.Play(GameStructs.ClipName.HolsterWeapon);
gun.transform.parent.GetChild(0).gameObject.SetActive(false);
if (inventory.Contains(gun))
{
//Debug.Log("inventory already contains this weapon.");
foreach (GunBase storedGun in inventory)
{
if (storedGun.Equals(gun))
{
storedGun.Ammo += storedGun.clipSize;
if (storedGun.Ammo > storedGun.maxAmmo)
{
storedGun.Ammo = storedGun.maxAmmo;
}
}
}
Destroy(gun.gameObject);
}
else
{
//Debug.Log("added weapon to inventory.");
GameObject gunSpawn = GameObject.Find("GunSpawn");
inventory.Add(gun);
gun.gameObject.transform.SetParent(gunSpawn.transform);
gun.gameObject.tag = "PlayerOwned";
gun.transform.rotation = gunSpawn.transform.rotation;
gun.transform.localScale = Vector3.one;
gun.transform.position = gunSpawn.transform.position;
if (inventory.Count > 1)
{
//Debug.Log("SetActive to FALSE. inventory count: " + inventory.Count);
gun.gameObject.SetActive(false);
}
else
gun.gameObject.SetActive(true);
}
}
#endregion
#region Private Methods
void move()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = Vector3.Normalize(new Vector3(moveHorizontal, 0.0f, moveVertical));
this.GetComponent<Rigidbody>().MovePosition(this.transform.position + speed * Time.deltaTime * movement);
//agent.Move(movement);
}
void weaponCycle()
{
if (Input.GetKeyDown(changeWeapon) && (inventory.Count>1))
{
AudioManager.instance.Play(GameStructs.ClipName.HolsterWeapon);
inventory[currentWeapon].gameObject.SetActive(false);
currentWeapon = (currentWeapon == (inventory.Count - 1)) ? 0 : currentWeapon + 1;
inventory[currentWeapon].gameObject.SetActive(true);
}
}
void look()
{
Ray aim = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));
Vector3 lookPoint = new Vector3();
if (Physics.Raycast(aim, out aimpoint, 100f))
{
lookPoint = aimpoint.point;
lookPoint.y = this.transform.position.y;
transform.LookAt(lookPoint);
}
}
void shoot()
{
if (inventory.Count>0)
{
if (inventory[currentWeapon].isAutomatic)
{
if (Input.GetKey(Fire))
{
inventory[currentWeapon].shoot();
}
}
else
{
if (Input.GetKeyDown(Fire))
{
inventory[currentWeapon].shoot();
}
}
}
if (Input.GetKeyDown(reload))
{
inventory[currentWeapon].PressedReload = true;
}
}
#endregion
}