I feel very proud to say, for the first time ever in life, I didn't find Radnen's sample code useful. Because I already did even better!
// Player data.
var blobX = 0;
var blobY = 0;
var blobYV = 0; // y velocity
var blobYVM = 4; // y velocity max
var blobGravity = 0.25;
var blobJump = 3.5;
var blobAbove //
var blobBelow // obstruction checkers
var jumpYet //
var lastJump = 0; // to prevent rapid jumping
function PlayerMovement() {
// Seed the variables with the player's current data.
blobX = GetPersonX("blob");
blobY = GetPersonY("blob");
blobAbove = IsPersonObstructed("blob",blobX,blobY-12);
blobBelow = IsPersonObstructed("blob",blobX,blobY+1);
// Left and right movement. Need to add animation, directions.
if (IsKeyPressed(KEY_LEFT)) {
QueuePersonCommand("blob",COMMAND_MOVE_WEST,true);
}
if (IsKeyPressed(KEY_RIGHT)) {
QueuePersonCommand("blob",COMMAND_MOVE_EAST,true);
}
// ugh, even _I_ think this is an eyesore, but it stops you falling through the floor
if (blobYV < blobYVM) {
blobYV += blobGravity;
}
if (blobYV > 0 && IsPersonObstructed("blob",blobX,blobY+blobYV+1)) {
var vBuff = 0;
while (!IsPersonObstructed("blob",blobX,blobY+vBuff))
vBuff++;
SetPersonY("blob",blobY+vBuff-1);
blobYV = 0;
}
else {
if (blobYV < 0 && IsPersonObstructed("blob",blobX,blobY+blobYV))
blobYV = 1;
SetPersonY("blob", blobY+blobYV);
}
// lets you jump from the ground if there's no block above you
jumpYet = GetTime() - lastJump; // stops you jumping rapidly up staircases
if (jumpYet > 450 && blobAbove == 0 && blobBelow == 1 && IsKeyPressed(KEY_D)) {
blobYV = -blobJump;
lastJump = GetTime();
}
}
var blah = "";
var font = GetSystemFont();
function mainLoop() {
PlayerMovement();
blah = blobYV.toString();
font.drawText(16, 16, blah);
}
function game() {
CreatePerson("blob", "blob.rss", false);
AttachCamera("blob");
SetRenderScript("mainLoop();");
MapEngine("room1.rmp", 60);
}
It's still a bit messy and disorganized (I still really haven't found a comment style that's quite "me"), and I haven't done it OOP style (though I'm not sure if it's worth the bother considering the miniscule scope of this project), but it works like a mofo -- not only does my fancy new gravity code prevent you from getting stuck in the floor (by using a "buffer" to check for obstructions
ahead of the player and reel back the blobYV if so, thus preventing wonky numbers from shoving you a few pixels into the ground), but I used GetTime to add a subtle delay to jumping, because before I added that, if there was a staircase and you ran at it while holding Jump, you'd just zoom right up the staircase ('cause the second a single pixel of yours had a ground tile under it, it'd let you jump again). Now, with the delay, you'll jump up on stair at a time.
There's two things I don't understand, though.
1) My jump delayer makes you jump shorter than before. Prior to implementing it, a blobJump value of 3 allowed you to just jump one block high. After implementing it, you can't surmount a single block with blobJump = 3. I had to increase it to 3.5, and I just cannot figure out why that should be.
2) I got stuck with my "vertical buffer" code, so I cracked open Vince's Flippin' Matrix code and found something similar and better, and used it to fix what was wrong with mine. I can't figure something out, though. In the last section of the code...
else {
if (blobYV < 0 && IsPersonObstructed("blob",blobX,blobY+blobYV))
blobYV = 1;
SetPersonY("blob", blobY+blobYV);
}
...if you put curly brackets after the "if" like normal, it screws up the code. That is, if you change the above to this...
else {
if (blobYV < 0 && IsPersonObstructed("blob",blobX,blobY+blobYV)) {
blobYV = 1;
SetPersonY("blob", blobY+blobYV);
}
}
...the code breaks. It won't let you jump, and prevents you from falling (you can walk off a cliff onto thin air).
For the life of me, I can't figure out why bracketing that 'if' does that. Because it's inside an else? Is that a thing? Maybe I just never put an 'if' inside an 'else' before, so I never noticed.
Next up, I'm gonna add momentum to jumping. That way, if you jump while running, you can't hold in the opposite direction midair and totally turn around, you can merely modulate how far in the original direction you jump (keep holding in forward, jump farther; hold in backward, jump less far).
Then I'll probably add swimming, and then that'll probably be it for the features. Then I'll pretty-up the code, prolly go OOP after all, if only 'cause I'll be able to more easily re-use the code later.
I'm gonna have fewer "why doesn't this work" questions than I thought, and more "why does this work the way it does" questions and "woo hoo finished some code" showoffmanship. So I've renamed the topic accordingly.
(Vince, you'll get a credit since I hijacked some Flippin' Matrix code to fix my own dumb buffer code.)
((Hee hee! A credit! I'm actually making a game ^_^))