Related to object-oriented programming. A class is a language construct that allows binding data to methods.
Below is an example class for a game, written in Kotlin. In this example, QuestService handles responsibilities related to questing. The class initializes with a list of possible quests. Most methods scope to a PlayerMob, meaning those particular methods concern only a single player’s quests.
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
| class QuestService(private val quests: List<Quest>) {
fun findByType(type: QuestType): Quest? {
return quests.find { it.type == type }
}
fun getAcceptableQuestsForMob(mob: PlayerMob): List<Quest> {
return quests.filter {
val notSatisfied = it.acceptConditions.find { req -> !req.doesSatisfy(mob) }
!mob.quests.containsKey(it.type) && notSatisfied == null
}
}
fun getAcceptedQuestsForMob(mob: PlayerMob): List<Quest> {
return quests.filter {
mob.quests.containsKey(it.type)
&& mob.quests[it.type]?.status != QuestStatus.SUBMITTED
}
}
fun getSubmittableQuestsForMob(mob: PlayerMob): List<Quest> {
return quests.filter {
val notSatisfied = it.submitConditions.find { req -> !req.doesSatisfy(mob) }
mob.quests.containsKey(it.type) && notSatisfied == null
}
}
fun submit(mob: PlayerMob, quest: Quest) {
mob.quests[quest.type]?.let {
it.status = QuestStatus.SUBMITTED
}
reward(mob, quest)
}
fun accept(mob: PlayerMob, quest: Quest) {
mob.quests[quest.type] = QuestModel()
}
fun abandon(mob: PlayerMob, quest: Quest) {
mob.quests.remove(quest.type)
}
fun reward(mob: PlayerMob, quest: Quest) {
quest.rewards.forEach {
RewardService(mob).award(it)
}
}
fun getLog(mob: PlayerMob): List<Quest> {
return quests.filter { mob.quests[it.type] !== null }.sortedBy { it.level }
}
}
|
When considering the definition at the top of the page, the quests passed in to QuestService on line 1 are the data being bound to the class, and each method either operates on the bound data or on an argument passed in to it.