-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgun respwan advanced.cs
165 lines (140 loc) · 3.09 KB
/
gun respwan advanced.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
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class PlayerController : MonoBehaviour
{
public List<Gun> guns;
public int currentGunIndex = 0;
public Text inventoryText;
void Start()
{
SetActiveGun(currentGunIndex);
UpdateInventoryText();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
guns[currentGunIndex].FireBullet();
}
else if (Input.GetKeyDown(KeyCode.Q))
{
CycleGuns();
}
}
void CycleGuns()
{
currentGunIndex++;
if (currentGunIndex >= guns.Count)
{
currentGunIndex = 0;
}
SetActiveGun(currentGunIndex);
}
void SetActiveGun(int index)
{
for (int i = 0; i < guns.Count; i++)
{
if (i == index)
{
guns[i].gameObject.SetActive(true);
}
else
{
guns[i].gameObject.SetActive(false);
}
}
}
public List<Item> inventory = new List<Item>();
void UpdateInventoryText()
{
inventoryText.text = "Inventory:\n";
foreach (Item item in inventory)
{
inventoryText.text += "- " + item.name + "\n";
}
}
public void AddToInventory(Item item)
{
inventory.Add(item);
UpdateInventoryText();
}
public void RemoveFromInventory(Item item)
{
inventory.Remove(item);
UpdateInventoryText();
}
}
public class Gun : MonoBehaviour
{
public GameObject bulletPrefab;
public Text oopsText;
private float fireRate = 0.1f;
private float nextFire = 0.0f;
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
FireBullet();
}
}
public void FireBullet()
{
GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
Destroy(bullet, 3.0f);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit,
100.0f))
{
if (hit.collider.CompareTag("Enemy"))
{
Enemy enemy = hit.collider.GetComponent<Enemy>();
enemy.TakeDamage(10);
}
else if (hit.collider.CompareTag("Item"))
{
Item item = hit.collider.GetComponent<Item>();
item.Pickup();
}
}
}
}
public class Item : MonoBehaviour
{
public string name;
public void Pickup()
{
PlayerController playerController = FindObjectOfType<PlayerController>();
if (playerController != null)
{
playerController.AddToInventory(this);
Destroy(gameObject);
}
}
}
public class Enemy : MonoBehaviour
{
public int health = 100;
public Text oopsText;
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
oopsText.text = "Oops, you got caught!";
oopsText.gameObject.SetActive(true);
}
}
}