-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameController.cs
More file actions
204 lines (167 loc) · 5 KB
/
Copy pathgameController.cs
File metadata and controls
204 lines (167 loc) · 5 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
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
using UnityEngine;
public class gameController : MonoBehaviour
{
public Camera currentCamera;
public Game game;
public BoardView boardView;
//public PieceView nextPieceView;
public ScoreView scoreView;
public LevelView levelView;
public AlertView alertView;
public SettingsView settingsView;
public AudioPlayer audioPlayer;
public GameObject screenButtons;
public AudioSource musicAudioSource;
public float speed = 10f;
public float rotationSpeed = 100f;
public float jumpForce = 5f;
public float groundRaycastDistance = 1f;
private Rigidbody rb;
private bool isGrounded;
private UniversalInput universalInput;
public int pointsPerCollectible = 100;
public int pointsPerTrick = 50;
private int totalScore;
private void Awake()
{
HandlePlayerSettings();
Settings.ChangedEvent += HandlePlayerSettings;
}
void Start()
{
Board board = new Board(10, 20);
boardView.SetBoard(board);
nextPieceView.SetBoard(board);
universalInput = new UniversalInput(new KeyboardInput(), boardView.touchInput);
game = new Game(board, universalInput);
game.FinishedEvent += OnGameFinished;
game.PieceFinishedFallingEvent += audioPlayer.PlayPieceDropClip;
game.PieceRotatedEvent += audioPlayer.PlayPieceRotateClip;
game.PieceMovedEvent += audioPlayer.PlayPieceMoveClip;
game.Start();
scoreView.game = game;
levelView.game = game;
}
public void OnPauseButtonTap()
{
game.Pause();
ShowPauseView();
}
public void OnMoveLeftButtonTap()
{
game.SetNextAction(PlayerAction.MoveLeft);
}
public void OnMoveRightButtonTap()
{
game.SetNextAction(PlayerAction.MoveRight);
}
public void OnMoveDownButtonTap()
{
game.SetNextAction(PlayerAction.MoveDown);
}
public void OnFallButtonTap()
{
game.SetNextAction(PlayerAction.Fall);
}
public void OnRotateButtonTap()
{
game.SetNextAction(PlayerAction.Rotate);
}
void OnGameFinished()
{
alertView.SetTitle(Constant.Text.GameFinished);
alertView.AddButton(Constant.Text.PlayAgain, game.Start, audioPlayer.PlayNewGameClip);
alertView.Show();
}
void Update()
{
game.Update(Time.deltaTime);
}
void ShowPauseView()
{
alertView.SetTitle(Constant.Text.GamePaused);
alertView.AddButton(Constant.Text.Resume, game.Resume, audioPlayer.PlayResumeClip);
alertView.AddButton(Constant.Text.NewGame, game.Start, audioPlayer.PlayNewGameClip);
alertView.AddButton(Constant.Text.Settings, ShowSettingsView, audioPlayer.PlayResumeClip);
alertView.Show();
}
void ShowSettingsView()
{
settingsView.Show(ShowPauseView);
}
void HandlePlayerSettings()
{
screenButtons.SetActive(Settings.ScreenButonsEnabled);
boardView.touchInput.Enabled = !Settings.ScreenButonsEnabled;
musicAudioSource.gameObject.SetActive(Settings.MusicEnabled);
}
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
// Handle player input
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
// Calculate movement direction
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
movement = movement.normalized * speed * Time.deltaTime;
// Apply movement to the rigidbody
rb.AddForce(movement);
// Rotate the player based on horizontal input
float rotation = moveHorizontal * rotationSpeed * Time.deltaTime;
Quaternion deltaRotation = Quaternion.Euler(new Vector3(0f, rotation, 0f));
rb.MoveRotation(rb.rotation * deltaRotation);
// Handle jump input
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private void FixedUpdate()
{
// Check if the player is grounded
isGrounded = CheckGrounded();
}
private bool CheckGrounded()
{
RaycastHit hit;
Vector3 raycastOrigin = transform.position + Vector3.up * 0.1f; // Offset slightly above the ground
// Perform a raycast downwards to check if the player is touching the ground
if (Physics.Raycast(raycastOrigin, Vector3.down, out hit, groundRaycastDistance))
{
// Adjust the ground detection based on your snowboarding terrain
return true;
}
return false;
}
private void Start()
{
totalScore = 0;
}
private void OnTriggerEnter(Collider other)
{
// Check if the player collects a collectible object
if (other.CompareTag("Collectible"))
{
Collectible collectible = other.GetComponent<Collectible>();
if (collectible != null && !collectible.IsCollected)
{
collectible.Collect();
totalScore += pointsPerCollectible;
Debug.Log("Collectible collected! Current score: " + totalScore);
}
}
}
public void PerformTrick()
{
totalScore += pointsPerTrick;
Debug.Log("Trick performed! Current score: " + totalScore);
}
public void CompleteObjective(int objectivePoints)
{
totalScore += objectivePoints;
Debug.Log("Objective completed! Current score: " + totalScore);
}
}