-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame-slurp.js
More file actions
157 lines (156 loc) · 4.19 KB
/
game-slurp.js
File metadata and controls
157 lines (156 loc) · 4.19 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
const game = {
bubbleCountMin: 6,
bubbleRadiusMin: 1 / 10,
bubbleRadiusMax: 1 / 5,
bubbleRadiusPop: 1 / 100,
bubbleMinSpeed: global.shipMaxSpeed / 3,
bubbleMaxSpeed: global.shipMaxSpeed / 2,
bubbleConsumptionRate: 1 / 10 / 500,
bubbleLastId: 0,
pointsSlurp: 1 / 10,
durationPlay: 30 * global.ticksPerSecond, // ticks are every 10ms
activate: (players, state) => {
Object.assign(
state,
{
timer: 0,
startCircle: null,
bubbles: []
}
)
game.changeModeToIntro(players, state)
},
changeModeToIntro: (players, state) => {
const now = Date.now()
state.mode = 'intro'
state.timer = game.durationPlay
state.startCircle = global.createActivityCircle({
label: 'Start',
y: 0.8
})
game.populateInitialBubbles(state)
state.ships.forEach(ship => {
ship.meta.score = 0
ship.score = 0
players[ship.id].lastActiveTime = now
})
},
changeModeToPlay: (players, state) => {
state.mode = 'play'
state.startCircle = undefined
game.populateInitialBubbles(state)
state.ships.forEach(ship => {
ship.meta.score = 0
ship.score = 0
})
state.events.emit('start')
},
changeModeToScore: (players, state) => {
delete state.bubbles
global.totalPlayerScores(players, state)
},
tickGame: (now, players, state) => {
if (state.mode !== 'score') {
global.tickPlayers(now, players, state)
game.tickBubbles(now, players, state)
}
if (state.mode === 'intro') {
let startGame = global.circleSelectCountdown(
now,
state.startCircle,
players,
state,
true
)
if (startGame) {
game.changeModeToPlay(players, state)
}
}
if (state.mode === 'play') {
state.timer -= 1
if (state.timer <= 0) {
game.changeModeToScore(players, state)
}
}
if (state.mode === 'score') {
global.animatePlayerScores(players, state)
}
return state
},
tickBubbles: (now, players, state) => {
state.bubbles.forEach(bubble => {
bubble.x += bubble.xVel
bubble.y += bubble.yVel
bubble.hue = null
bubble.hit = false
global.wrap(bubble)
let playerBubbleIntersection = global.playersInCircle(bubble, players, state)
if (playerBubbleIntersection.in.length) {
playerBubbleIntersection.in.forEach((player) => {
player.ship.meta.score += game.pointsSlurp
player.ship.score = player.ship.meta.score.toFixed(1)
bubble.radius -= game.bubbleConsumptionRate
bubble.hue = player.ship.hue
})
if (playerBubbleIntersection.in.length > 1) {
bubble.hue = null
bubble.hit = true
}
const speed = global.mapRange(
game.bubbleRadiusMin,
game.bubbleRadiusMax,
game.bubbleMinSpeed,
game.bubbleMaxSpeed,
bubble.radius
)
bubble.xVel = Math.cos(bubble.angle) * speed
bubble.yVel = Math.sin(bubble.angle) * speed
if (bubble.radius <= game.bubbleRadiusPop) {
bubble.expired = true
}
}
})
state.bubbles = state.bubbles.filter((bubble) => { return !bubble.expired })
game.generateBubbles(state)
},
populateInitialBubbles: (state) => {
state.bubbles = []
game.generateBubbles(state)
},
generateBubbles: (state) => {
while (state.bubbles.length < game.bubbleCountMin) {
state.bubbles.push(game.createBubble())
}
},
createBubble: (
x = null,
y = null,
radius = global.rangeRand(game.bubbleRadiusMin, game.bubbleRadiusMax)
) => {
const angle = Math.random() * global.tau
const speed = global.mapRange(
game.bubbleRadiusMin,
game.bubbleRadiusMax,
game.bubbleMinSpeed,
game.bubbleMaxSpeed,
radius
)
const id = game.bubbleLastId += 1
if (x === null || y === null) {
const edgePosition = global.randomEdgePosition()
x = edgePosition.x
y = edgePosition.y
}
return global.createActivityCircle({
id,
x,
y,
xVel: Math.cos(angle) * speed,
yVel: Math.sin(angle) * speed,
angle,
radius,
hue: null
})
}
}
module.exports = game