Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Last active July 6, 2018 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielflorit/b8badcdcf5532514f84dc80c61ec8382 to your computer and use it in GitHub Desktop.
Save gabrielflorit/b8badcdcf5532514f84dc80c61ec8382 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: platformer
const sprites = {
mario: {
idle: {
cycles: [...range(21).map(d => 0), 7]
},
walk: {
cycles: [1, 2, 3, 3, 4, 5, 6, 6],
dx: 1
},
jump: {
cycles: [8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 8],
dy: [0, 0, -4, -3, -2, -1, 0, 0, 3, 3, 4, 0]
}
}
}
initialState = {
counter: 0,
actors: [
{
name: 'mario',
x: 64,
y: 104,
mode: 'idle',
flip: false,
cycle: 0
}
]
}
const moveMario = ({ mario, input, state }) => {
const prevMode = mario.mode
let nextMode = mario.mode
// const fsm = {
// idle: {
// left: 'walk',
// right: 'walk',
// a: 'jump',
// },
// walk: {
// a: 'jump',
// nil: 'idle'
// },
// jump: {
// ground: 'idle'
// }
// }
// If we're pressing left or right,
if (input.right || input.left) {
// event: left or right
// prev mode: idle
// next mode: walk
if (input.a) {
nextMode = 'jump'
} else if (
prevMode === 'jump' &&
mario.cycle === sprites.mario.jump.cycles.length - 1
) {
nextMode = 'walk'
} else {
if (prevMode === 'idle') {
nextMode = 'walk'
}
}
if (input.right) {
mario.x += sprites.mario.walk.dx
mario.flip = false
} else {
// Otherwise move backward with a flipped sprite.
mario.x -= sprites.mario.walk.dx
mario.flip = true
}
} else if (input.a) {
// event: a
// prev mode: idle
// next mode: jump
// event: a
// prev mode: walk
// next mode: jump
if (prevMode === 'idle') {
nextMode = 'jump'
} else if (prevMode === 'walk') {
nextMode = 'jump'
}
} else if (
prevMode === 'jump' &&
mario.cycle === sprites.mario.jump.cycles.length - 1
) {
// event: groundcollision
// prev mode: 'jump'
// next mode: 'idle'
nextMode = 'idle'
} else if (prevMode === 'walk') {
// event: nil
// prev mode: 'walk'
// next mode: 'idle'
nextMode = 'idle'
}
// If we're changing modes, reset the cycle.
if (prevMode !== nextMode) {
mario.cycle = 0
}
// Things to do regardless of input.
if (nextMode === 'jump') {
mario.y += sprites.mario.jump.dy[mario.cycle]
if (mario.cycle === 0) {
playPhrase(1)
}
} else if (nextMode === 'walk') {
if (state.counter % 10 === 0) {
playPhrase(0)
}
}
// Set new mode.
mario.mode = nextMode
// Update cycle.
if (++state.counter % 2 === 0) {
mario.cycle = (mario.cycle + 1) % sprites.mario[mario.mode].cycles.length
}
}
update = (state, input, elapsed) => {
const mario = state.actors.find(d => d.name === 'mario')
moveMario({ mario, input, state })
}
const drawActor = (actor, fade) => {
if (actor.name === 'mario') {
sprite(
actor.x,
actor.y,
sprites[actor.name][actor.mode].cycles[actor.cycle],
fade ? -4 : 0,
actor.flip
)
}
}
drawActors = (state, fade) => {
state.actors.forEach(actor => drawActor(actor, fade))
}
draw = state => {
clear()
// sky gradient
range(7).forEach(i => {
rectFill(0, 12 * i, 128, 12 + i, 6 - i)
})
// left mountain
range(68).forEach(i => {
line(0, 44 + i, i, 44 + i, 5)
})
line(0, 44, 68, 64 + 48, 6)
// middle mountain
range(32).forEach(i => {
line(60 - i, 61 + i, 60 + i, 61 + i, 5)
})
line(59, 61, 8, 112, 6)
line(60, 61, 105, 111, 6)
// right mountain
range(72).forEach(i => {
line(90 - i, 40 + i, 90 + i, 40 + i, 5)
})
line(90, 40, 19, 111, 6)
line(90, 40, 161, 111, 6)
// floor
range(16).forEach(i => {
sprite(i * 8, 112, 33)
sprite(i * 8, 120, 32)
})
// mario
drawActors(state)
print(6, 2, 'left / right to move, a to jump', 3)
}
{
"0": [
"0c01"
],
"1": [
"0d34",
"1g33",
"2b32"
]
}
{
"0": [
" 2220 ",
" 22222",
" 20270 ",
" 200220",
" 2000 ",
" 221331 ",
"0 333351",
" 2 3 "
],
"1": [
" 2220 ",
" 22222",
" 20270 ",
" 200220",
" 2000 ",
" 22135 ",
" 20335 ",
" 223 "
],
"2": [
" 2222 ",
" 22222",
" 22027 ",
" 220022",
" 2200 ",
" 32215 ",
" 33220 ",
" 2 3 "
],
"3": [
" 2222 ",
" 22222",
" 22027 ",
" 220022",
" 2200 ",
" 322150",
" 233322 ",
" 3 "
],
"4": [
" 2220 ",
" 22222",
" 20270 ",
" 200220",
" 2000 ",
" 22135 ",
" 20335 ",
" 223 "
],
"5": [
" 2202 ",
" 22222",
" 02707 ",
" 002202",
" 0000 ",
" 221331 ",
"0 333351",
" 3 2 "
],
"6": [
" 2202 ",
" 22222",
" 02707 ",
" 002202",
" 220000 ",
"0 213310",
" 333335 ",
" 2 "
],
"7": [
" 2220 ",
" 22222",
" 20200 ",
" 200220",
" 2000 ",
" 221331 ",
"0 333351",
" 2 3 "
],
"8": [
" ",
" 2220 ",
" 22222",
" 20270 ",
" 200220",
" 222000 ",
"0 333351",
" 2 3 "
],
"9": [
" 2220 ",
" 222221",
" 2027072",
" 2002202",
" 220001 ",
"0 21335 ",
" 33353 ",
" 2 "
],
"10": [
" 20 ",
" 222221",
" 2222 2",
" 20270 2",
" 200220 ",
"0220001 ",
" 213353",
" 23335 "
],
"32": [
"00112112",
"02225223",
"12225225",
"12223225",
"13552112",
"01121223",
"12231225",
"25532555"
],
"33": [
"11111114",
"14444445",
"14445445",
"14545445",
"54555455",
"55522553",
"15231525",
"25532555"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment