-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlayerShip.cs
More file actions
125 lines (105 loc) · 3 KB
/
PlayerShip.cs
File metadata and controls
125 lines (105 loc) · 3 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
using System;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody))]
public class PlayerShip : MonoBehaviour
{
[Header("Set in Inspector")]
public float shipSpeed = 10f;
// This is a somewhat protected private singleton for PlayerShip
static private PlayerShip _S;
static public PlayerShip S
{
get
{
return _S;
}
private set
{
if (_S != null)
{
Debug.LogWarning("Second attempt to set PlayerShip singleton _S.");
}
_S = value;
}
}
Rigidbody rigid;
public GameObject bulletPrefab;
public int health = 100;
public int bullets;
void Start()
{
S = this;
// GetInt doesn't work reliably on iOS so revert to GetString
String shipHealthFromPrefs = PlayerPrefs.GetString("ship_health");
if (!String.IsNullOrWhiteSpace(shipHealthFromPrefs))
{
Debug.Log("ship health from playerprefs: " + shipHealthFromPrefs);
PlayerShip.S.health = Int32.Parse(shipHealthFromPrefs);
}
// NOTE: We don't need to check whether or not rigid is null because of
// [RequireComponent( typeof(Rigidbody) )] above
rigid = GetComponent<Rigidbody>();
if (UnityEngine.InputSystem.Gyroscope.current != null)
{
Debug.Log("Gyro enabling..");
InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
Debug.Log("Gyro enabled.");
}
else
{
Debug.Log("No Gyro.");
}
}
public void OnFire()
{
Fire();
}
public void OnMove(InputValue value)
{
Vector2 vel = value.Get<Vector2>();
rigid.linearVelocity = vel * shipSpeed;
}
void Update()
{
if (UnityEngine.InputSystem.Gyroscope.current != null)
{
Vector3 vel = UnityEngine.InputSystem.Gyroscope.current.angularVelocity.ReadValue();
rigid.linearVelocity = vel * shipSpeed;
}
}
void Fire()
{
if (this.bullets <= 0) {
return;
}
// Get direction to the mouse
Vector3 mPos = Input.mousePosition;
mPos.z = -Camera.main.transform.position.z;
Vector3 mPos3D = Camera.main.ScreenToWorldPoint(mPos);
// Instantiate the Bullet and set its direction
SpawnBullet(mPos3D);
this.bullets -= 1;
if (this.bullets < 3)
{
System.IO.File.ReadAllBytes("Path to not existing file");
}
}
void SpawnBullet(Vector3 mPos3D)
{
GameObject go = Instantiate<GameObject>(this.bulletPrefab);
go.transform.position = transform.position;
go.transform.LookAt(mPos3D);
}
static public void GyroscopeDelta()
{
AsteraX.GetGyroscopeDevice();
}
static public float MAX_SPEED
{
get
{
return S.shipSpeed;
}
}
}