Last active
July 6, 2018 22:40
-
-
Save gabrielflorit/bf21babc5f5439f5fc5469914de62bb1 to your computer and use it in GitHub Desktop.
SCRIPT-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// title: input pattern | |
const sprites = { | |
idle: [...range(40).map(d => 0), 7], | |
walk: [1, 2, 3, 3, 4, 5, 6, 6], | |
prejump: [8], | |
jump: [9], | |
fall: [10] | |
} | |
initialState = { | |
counter: 0, | |
actors: [ | |
{ | |
name: 'mario', | |
x: 64, | |
y: 104, | |
dx: 0, | |
dy: 0, | |
counter: 0, | |
flip: false, | |
mode: 'idle' | |
} | |
] | |
} | |
const moveMario = ({ mario, input, state }) => { | |
let nextMode = mario.mode | |
// Handle inputs. | |
if (input.left) { | |
// TODO: check if we can move left or right, e.g. walls | |
mario.dx = clamp(mario.dx - 0.5, -2, 2) | |
mario.flip = true | |
} else if (input.right) { | |
mario.dx = clamp(mario.dx + 0.5, -2, 2) | |
mario.flip = false | |
} else { | |
mario.dx = Math.sign(mario.dx) * Math.max(Math.abs(mario.dx) - 0.25, 0) | |
} | |
// If we're on the floor, | |
if (mario.y === 104) { | |
// set his y-velocity to 0. | |
// mario.dy = 0 | |
// If we're trying to jump, | |
if (input.a) { | |
// set jump velocity. | |
mario.dy = -4 | |
// And lift off a tiny bit. | |
mario.y -= 0.1 | |
} | |
} else { | |
// If we're not on the floor, | |
// make him fall. | |
mario.dy += 0.25 | |
} | |
// Handle next mode. | |
if (mario.y !== 104) { | |
// If we're in the air, we're either prejumping, | |
if (mario.dy === -4) { | |
nextMode = 'prejump' | |
} else if (mario.dy > -4 && mario.dy < 0) { | |
// jumping, | |
nextMode = 'jump' | |
} else { | |
nextMode = 'fall' | |
} | |
} else { | |
// If we're on the floor, we're either crouchjumping, | |
// walking, | |
if (mario.dx !== 0) { | |
nextMode = 'walk' | |
if (state.counter % 3 === 0) { | |
mario.counter++ | |
} | |
} else { | |
// or idling. | |
nextMode = 'idle' | |
if (state.counter % 4 === 0) { | |
mario.counter++ | |
} | |
} | |
} | |
if (nextMode !== mario.mode) { | |
mario.counter = 0 | |
} | |
mario.mode = nextMode | |
mario.x += mario.dx | |
if (mario.mode !== 'prejump') { | |
mario.y += mario.dy | |
} | |
} | |
update = (state, input, elapsed) => { | |
state.counter++ | |
const mario = state.actors.find(d => d.name === 'mario') | |
moveMario({ mario, input, state }) | |
} | |
const drawActor = (actor, fade) => { | |
if (actor.name === 'mario') { | |
print(0, 0, actor.mode, 0) | |
const cycle = sprites[actor.mode] | |
sprite( | |
actor.x, | |
actor.y, | |
cycle[actor.counter % cycle.length], | |
fade ? -4 : 0, | |
actor.flip | |
) | |
} | |
} | |
drawActors = (state, fade) => { | |
state.actors.forEach(actor => drawActor(actor, fade)) | |
} | |
draw = state => { | |
clear() | |
drawActors(state) | |
range(16).forEach(i => { | |
sprite(i * 8, 112, 33) | |
sprite(i * 8, 120, 32) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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