summaryrefslogtreecommitdiff
path: root/GameCode/PlayerAPI.cs
blob: 3edf9c52f3bddde43255dd1eb8e6443c09234263 (plain)
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
using System.Collections.Generic;
using UnityEngine;

public class PlayerAPI : MonoBehaviour
{
	public Player player;

	public CharacterData data;

	private GeneralInput input;

	private bool movedThisFrame;

	private bool attackedThisFrame;

	private bool blockedThisFrame;

	private void Awake()
	{
		player = GetComponent<Player>();
		data = GetComponent<CharacterData>();
		input = GetComponent<GeneralInput>();
	}

	public void Move(Vector2 direction)
	{
		direction = Vector2.ClampMagnitude(direction, 1f);
		movedThisFrame = true;
		data.input.direction = direction;
	}

	public void Jump()
	{
		data.jump.Jump();
	}

	public void Attack()
	{
		attackedThisFrame = true;
		data.input.shootWasPressed = true;
		data.input.shootIsPressed = true;
	}

	public void Block()
	{
		data.input.shieldWasPressed = true;
	}

	public void SetAimDirection(Vector2 direction)
	{
		data.input.aimDirection = direction;
	}

	public void AimForOtherPlayer()
	{
		Player otherPlayer = PlayerManager.instance.GetOtherPlayer(player);
		if ((bool)otherPlayer)
		{
			data.input.aimDirection = otherPlayer.transform.position - base.transform.position;
		}
	}

	public Vector2 TowardsOtherPlayer()
	{
		if (PlayerManager.instance.players.Count < 2)
		{
			return Vector2.zero;
		}
		return PlayerManager.instance.GetOtherPlayer(player).transform.position - base.transform.position;
	}

	public RaycastHit2D RayCastDirection(Vector2 direction, float distance)
	{
		return Physics2D.Raycast(base.transform.position, direction, distance);
	}

	public bool CheckGroundBelow(Vector2 pos, float range)
	{
		return data.ThereIsGroundBelow(pos, range);
	}

	public bool CanBlock()
	{
		return !data.block.IsOnCD();
	}

	public Player GetOtherPlayer()
	{
		return PlayerManager.instance.GetOtherPlayer(player);
	}

	public Vector3 OtherPlayerPosition()
	{
		Player otherPlayer = PlayerManager.instance.GetOtherPlayer(player);
		if ((bool)otherPlayer)
		{
			return otherPlayer.transform.position;
		}
		return Vector3.zero;
	}

	public Vector3 PlayerPosition()
	{
		return base.transform.position;
	}

	public List<BulletWrapper> GetAllBullets()
	{
		List<BulletWrapper> list = new List<BulletWrapper>();
		ProjectileHit[] array = Object.FindObjectsOfType<ProjectileHit>();
		for (int i = 0; i < array.Length; i++)
		{
			BulletWrapper bulletWrapper = new BulletWrapper();
			bulletWrapper.projectileHit = array[i].GetComponent<ProjectileHit>();
			bulletWrapper.projectileMovement = array[i].GetComponent<MoveTransform>();
			bulletWrapper.damage = bulletWrapper.projectileHit.damage;
			bulletWrapper.velocity = bulletWrapper.projectileMovement.velocity;
			list.Add(bulletWrapper);
		}
		return list;
	}

	public SpawnedAttack[] GetAllSpawnedAttacks()
	{
		return Object.FindObjectsOfType<SpawnedAttack>();
	}

	public bool CanShoot()
	{
		return player.data.weaponHandler.gun.IsReady();
	}

	public BulletWrapper GetMyBullet()
	{
		BulletWrapper bulletWrapper = new BulletWrapper();
		GameObject objectToSpawn = player.data.weaponHandler.gun.projectiles[0].objectToSpawn;
		MoveTransform component = objectToSpawn.GetComponent<MoveTransform>();
		ProjectileHit component2 = objectToSpawn.GetComponent<ProjectileHit>();
		bulletWrapper.projectileMovement = component;
		bulletWrapper.projectileHit = component2;
		bulletWrapper.damage = component2.damage;
		bulletWrapper.velocity = player.data.aimDirection.normalized * component.localForce.magnitude + component.worldForce;
		return bulletWrapper;
	}

	private void Update()
	{
		if (blockedThisFrame)
		{
			blockedThisFrame = false;
		}
		else
		{
			data.input.shieldWasPressed = false;
		}
		if (movedThisFrame)
		{
			movedThisFrame = false;
		}
		else
		{
			data.input.direction = Vector3.zero;
		}
		if (attackedThisFrame)
		{
			attackedThisFrame = false;
			return;
		}
		data.input.shootWasPressed = false;
		data.input.shootIsPressed = false;
	}
}