-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.ts
156 lines (140 loc) · 6.25 KB
/
events.ts
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
namespace Events {
let theMap: Map
let theCritters: Array<Critter>
let newCreatureOdds = 4 // chance at first, incrementing every slowTick
let adoptOdds = 4 // chance someone will show up, incrementing every slowTick
export let currentlyEvent: boolean = false
export function init({ map, critters }: { map: Map, critters: Array<Critter> }) {
theMap = map
theCritters = critters
}
export function slowTick() {
checkForNewArrival()
checkShouldAdopt()
}
function checkForNewArrival() {
if (currentlyEvent) return
if (Math.percentChance(newCreatureOdds)) {
newArrival()
newCreatureOdds = 4
} else {
// Odds improve over time
newCreatureOdds += 1
}
}
export function newArrival() {
// Don't have more than 6 critters
if (theCritters.length >= 6) {
newCreatureOdds = 4
} else {
currentlyEvent = true
// Create a new creature in the wilderness
story.printDialog('A creature was spotted!', 80, 100, 50, 150, 15, 1, story.TextSpeed.VeryFast)
Critters.generateAndPlaceCritter({ map: theMap })
currentlyEvent = false
}
}
// Make the phone ring?
function checkShouldAdopt() {
// If an event is going on, abort (too bad!)
if (currentlyEvent) return
if (Math.percentChance(adoptOdds)) {
// Now see if there any that can be adopted!
if (getAdpotableCritters().length) {
Environment.setPhoneRinging(true)
}
adoptOdds = 4
} else {
// Adopt odds improve over time
adoptOdds += 1
}
}
// Answer the phone (if it's ringing, we start adoption, if not we attempt to force it`)
export function onPickupPhone(resultCallback?: (result: PhoneResult) => void) {
if (Environment.isPhoneRinging) {
Environment.setPhoneRinging(false)
startAdoption(getAdpotableCritters(), resultCallback)
} else {
// If you call someone when it's not ringing, you have a chance to adopt
if (Math.percentChance(10)) {
const adoptables = getAdpotableCritters()
if (adoptables.length) {
startAdoption(adoptables, resultCallback)
} else {
story.printDialog("I'm intereseted in adopting, but you don't have any that are ready :(", 80, 120, 100, 100)
resultCallback(PhoneResult.canceled)
}
} else {
story.printDialog('No one wants to adopt today', 80, 120, 100, 100)
resultCallback(PhoneResult.canceled)
}
}
}
function getAdpotableCritters() {
// The more critters you have, the harder it is to get adoption
// Note this only works because max critters = 6
let minHappiness = theCritters.length * 10
let minHealth = theCritters.length * 10
// A person has shown up, now what is their chance they will adopt?
let adoptableCritters = theCritters.reduce((acc: Array<Critter>, critter: Critter) => {
if (critter.happiness > minHappiness &&
critter.health > minHealth &&
(
Utils.isInZone(critter.locationX, critter.locationY, Environment.foodCourts[0]) ||
Utils.isInZone(critter.locationX, critter.locationY, Environment.foodCourts[1]) ||
Utils.isInZone(critter.locationX, critter.locationY, Environment.foodCourts[2]) ||
Utils.isInZone(critter.locationX, critter.locationY, Environment.playpen)
)
) {
acc.push(critter)
}
// TODO add another entry if they are higher level
return acc
}, [])
return adoptableCritters
}
function startAdoption(adoptableCritters: Array<Critter>, resultCallback?: (result: PhoneResult) => void) {
// If there are any adoptable critters, pick a random one
if (adoptableCritters.length) {
// Put the happiest/healthiest on top
adoptableCritters.sort((critter1: Critter, critter2: Critter) => {
if (critter1.happiness + critter1.health > critter2.happiness + critter2.health) {
return -1
}
return 1
})
const choices = adoptableCritters.slice(0, 3)
story.startCutscene(() => {
// Pick the top 3 so the person can adopt
let choiceLabels = choices.reduce((acc, choice) => {
acc.push(`${choice.name} ${choice.critterType}`)
return acc
}, [])
choiceLabels.push(`Sorry none`)
currentlyEvent = true
controller.moveSprite(Player.ginny, 0, 0)
const visitorFace = sprites.create(assets.image`visitor1`)
visitorFace.setScale(2)
visitorFace.setPosition(Player.ginny.x + 50, Player.ginny.y + 30)
story.printDialog('Hi Ginny, I would like to adopt a Pokemon.', 60, 100, 100, 100)
story.printDialog('Which ones are you willing to release?', 60, 100, 100, 100)
story.showPlayerChoices(choiceLabels[0], choiceLabels[1], choiceLabels[2], choiceLabels[3], choiceLabels[4])
visitorFace.destroy()
controller.moveSprite(Player.ginny, 60, 60)
// Find the one they picked
const pickedCritter = choices.find(c => story.getLastAnswer().includes(c.name))
if (!pickedCritter) {
// You said no
story.printDialog("That's sad...", 80, 100, 50, 120)
resultCallback(PhoneResult.canceled)
} else {
// Replace the name we took
Critters.adoptCritter(pickedCritter.name)
story.printDialog(`${pickedCritter.name} has been adopted!`, 80, 100, 50, 120)
resultCallback(PhoneResult.adopted)
}
currentlyEvent = false
})
}
}
}