-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
65 lines (48 loc) · 1.81 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
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
private GamePieceScript placePiece;
private GameObject[,] gameBoard;
private int[,] gameBoardState;
private UtilityScripts utility;
private GameController gameData;
private int player = 2;
// Use this for initialization
void Start ()
{
placePiece = gameObject.GetComponent<GamePieceScript>();
gameBoard = gameObject.GetComponent<DrawBoardScript>().drawnGameBoard;
utility = gameObject.GetComponent<UtilityScripts>();
gameData = gameObject.GetComponent<GameController>();
gameBoardState = gameObject.GetComponent<DrawBoardScript>().gameBoardState;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
//Get current player
if (gameData.againstAI)
{
player = 1;
}
else
{
player = (3 - player);
}
//Get position of cursor on screen in relation to camera
Vector2 cursorPos = Input.mousePosition;
Vector2 screenPos = Camera.main.ScreenToWorldPoint(cursorPos);
//Raycast downwards from cursor position to find the column to place the game piece in
RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.down, Mathf.Infinity);
Debug.DrawRay(screenPos, Vector2.down);
if (hit.collider != null)
{
//Get column to place piece in
int column = utility.GetIndex(gameBoard, hit.collider.gameObject)[0];
placePiece.PlaceGamePiece(gameBoardState, column, false, player);
}
}
}
}