-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTypingGameController.swift
142 lines (102 loc) · 3.47 KB
/
TypingGameController.swift
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
//
// TypingGameController.swift
// funTyping
//
// Created by Chatchai Wangwiwattana on 2/8/17.
// Copyright © 2017 Chatchai Wangwiwattana. All rights reserved.
//
import Foundation
/*! -----------------------------------------------
* \brief: This game states will sent out events to UI
*/
protocol TypingGameProtocol {
func onGameStarted()
func onNextLevel()
func onGameEnded()
}
/*! -----------------------------------------------
* \brief: This manages the Typing Game.
* It controls game logic from start to finish.
*/
class TypingGameController{
var lessonSets = [""]
var currentLesson = 0
var currentLocation = 0
var mistakes = 0
var key = ""
var gameEvent:TypingGameProtocol?
init(){
print( "game object is created" )
}
/*! -----------------------------------------------
* \brief reset game state variables
*/
func initGame(){
/*! init game state */
currentLesson = 0
currentLocation = 0
mistakes = 0
lessonSets = ["default game level, something went wrong on loading a real game level"]
loadGameLevels()
print( "init game" )
gameEvent?.onGameStarted()
}
/*! -----------------------------------------------
* \brief: load game level file to game controller
*/
func loadGameLevels(){
let url = NSBundle.mainBundle().URLForResource("gameLevel", withExtension: "json")
let data = NSData(contentsOfURL: url!)
do {
let object = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let dictionary = object as? [String: AnyObject] {
guard let levels = dictionary["GameLevels"] as? [String] else { return }
self.lessonSets = levels
}
} catch {
// Handle Error
print("Error parsing JSON file")
}
}
/*! -----------------------------------------------
* \brief store input, so the game can update
*/
func input(key:String){
self.key = key;
}
/*! -----------------------------------------------
* \brief Update game state
*/
func update(){
if(self.key == String(self.lessonSets[self.currentLesson][self.currentLocation])){
if(self.currentLocation < self.lessonSets[self.currentLesson].characters.count-1 )
{
self.currentLocation += 1
}
else
{
if(self.currentLesson < self.lessonSets.count-1){
self.currentLocation = 0
self.currentLesson += 1
print("Next lesson \(self.currentLesson)")
gameEvent?.onNextLevel()
}
else
{
self.currentLesson = self.lessonSets.count-1
print("Done")
gameEvent?.onGameEnded()
}
}
}
else{
self.mistakes += 1
}
}
/*! -----------------------------------------------
* \brief distroy game state
*/
func exit(){
print("exit game, clear vairables")
}
}