-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.ts
160 lines (146 loc) · 6.16 KB
/
player.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
157
158
159
160
namespace Player {
export let ginny: Sprite = null
let isPaused = false
let critterBeingCarried: Critter | null = null
let carryingFoodSprite: Sprite = null
const movement = { up: false, down: false, right: false, left: false }
let previousMovement = { up: false, down: false, right: false, left: false }
export const init = ({ savedGame }: { savedGame?: SaveGame }) => {
ginny = sprites.create(assets.image`ginny`, SpriteKind.Player)
startController()
}
export const startController = () => {
isPaused = false
// TODO maybe there's a more "pro" way to animate and move
controller.moveSprite(ginny, 60, 60)
// Controller events
controller.down.onEvent(ControllerButtonEvent.Released, () => {
movement.down = false
})
controller.down.onEvent(ControllerButtonEvent.Pressed, () => {
movement.down = true
})
controller.up.onEvent(ControllerButtonEvent.Released, () => {
movement.up = false
})
controller.up.onEvent(ControllerButtonEvent.Pressed, () => {
movement.up = true
})
controller.left.onEvent(ControllerButtonEvent.Released, () => {
movement.left = false
})
controller.left.onEvent(ControllerButtonEvent.Pressed, () => {
movement.left = true
})
controller.right.onEvent(ControllerButtonEvent.Released, () => {
movement.right = false
})
controller.right.onEvent(ControllerButtonEvent.Pressed, () => {
movement.right = true
})
// Pickup and Drop critters with A
controller.player1.onButtonEvent(ControllerButton.A, ControllerButtonEvent.Released, function () {
if (isPaused) return
if (critterBeingCarried) {
critterBeingCarried.sprite.follow(null)
critterBeingCarried.sprite.setVelocity(20, 0)
critterBeingCarried = null
} else if (carryingFoodSprite) {
Environment.foodCourts.forEach((foodCourt) => {
if (Utils.isInZone(ginny.x, ginny.y, foodCourt)) {
Environment.addFood(foodCourt)
}
})
// Drop it either way
carryingFoodSprite.destroy()
carryingFoodSprite = null
} else {
Critters.critters.forEach((critter: Critter) => {
if (critter.sprite) {
if (ginny.overlapsWith(critter.sprite) && !critterBeingCarried) {
critterBeingCarried = critter
critter.sprite.follow(ginny)
Critters.onPickup(critter)
}
}
})
// Do signs and other B button things
if (!Events.currentlyEvent) {
if (Utils.isInZone(ginny.x, ginny.y, Environment.phoneZone)) {
Events.onPickupPhone(result => {
if (result === PhoneResult.adopted) {
Computer.increaseAdoptions()
}
})
} else if (Utils.isInZone(ginny.x, ginny.y, Environment.computerZone)) {
stopController()
Computer.startup({ top: ginny.y, left: ginny.x, onClose: () => {
pause(200)
startController()
}
})
} else if (Utils.isInZone(ginny.x, ginny.y, Environment.feedZone)) {
carryingFoodSprite = sprites.create(assets.image`hunger3`)
carryingFoodSprite.setPosition(Environment.feedZone.spawnCoords.x, Environment.feedZone.spawnCoords.y)
carryingFoodSprite.follow(ginny)
}
}
}
})
}
export const stopController = () => {
controller.moveSprite(ginny, 0, 0)
isPaused = true
}
// The blob of stuff to save
export function getSaveJson() {
return {}
}
// This sets the animation based on the resulting moveDirection
// Since it needs to be an "event" we keep track of the previous direction as
// Still not perfect but better
forever(() => {
if (isPaused) return
// If any movements have flipped to false, reset the whole previous to reeval the animation
if ((!movement.left && previousMovement.left) ||
(!movement.right && previousMovement.right) ||
(!movement.up && previousMovement.up) ||
(!movement.down && previousMovement.down)) {
previousMovement = {
up: false, down: false, right: false, left: false
}
}
let moveAnimation: Array<Image> | undefined
// Determine the animation based on direction (only set on change)
if (movement.left !== previousMovement.left) {
moveAnimation = assets.animation`walkLeft`
} else if (movement.right !== previousMovement.right) {
moveAnimation = assets.animation`walkRight`
} else if (movement.up !== previousMovement.up) {
moveAnimation = assets.animation`walkUp`
} else if (movement.down !== previousMovement.down) {
moveAnimation = assets.animation`walkDown`
}
if (moveAnimation) {
animation.runImageAnimation(
ginny,
moveAnimation,
150,
true
)
} else {
// Check if all buttons are released
const shouldStop = !movement.left && !movement.right && !movement.up && !movement.down
if (shouldStop) {
animation.stopAnimation(animation.AnimationTypes.All, ginny)
}
}
// Make a copy (not by reference)
previousMovement = {
left: movement.left,
right: movement.right,
up: movement.up,
down: movement.down
}
})
}