Skip to content

Commit

Permalink
bike velocity and seperate player into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
SpazElectro committed Aug 27, 2024
1 parent 7e02fad commit cd135a4
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"command": "cd levels && cd motorracer && run && exit",
"name": "Run motor racer",
"request": "launch",
"type": "node-terminal"
},
{
"name": "Run Extension",
"type": "extensionHost",
Expand Down
1 change: 1 addition & 0 deletions .vscode/plus.code-snippets.disable

Large diffs are not rendered by default.

175 changes: 175 additions & 0 deletions levels/motorracer/STVmotorracer.j2as
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,181 @@

#include "STVutil.asc"

const ANIM::Set playerAnimSet = ANIM::CUSTOM[1];
Key@ leftKey;
Key@ rightKey;
Key@ jumpKey;

int max(int a, int b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}

class MotorPlayer
{
AnimatedSprite@ sprite;
string name;
bool isLocal;

Vector2@ position;
Vector2@ velocity;

int speed;
int jumpForce;

int jumpHeld;
bool isGrounded;

MotorPlayer() {
@this.sprite = AnimatedSprite(0, 0, 0, 0, 0.15, false);
this.sprite.animSet = playerAnimSet;

@this.position = Vector2((jjResolutionWidth/2)+64, 64);

@this.velocity = Vector2(0, 0);

this.isLocal = false;
this.speed = 2;
this.jumpForce = 25;
this.jumpHeld = 0;
this.isGrounded = false;
}

void update() {
// physics
if(!this.isGrounded)
this.velocity.y += int((9.807)/3);

// Add velocity to the position
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;

// Decrease velocity over time
if(this.velocity.x > 0) {
this.velocity.x -= 1;
} else if(this.velocity.x < 0) { this.velocity.x += 1; }
if(this.velocity.y > 0) {
this.velocity.y -= 1;
} else if(this.velocity.y < 0) { this.velocity.y += 1; }

// Limit the bounds
this.position.x = max(32, this.position.x);
this.position.y = max(32, this.position.y);
this.position.x = min(jjResolutionWidth-32, this.position.x);
this.position.y = min(jjResolutionHeight-32, this.position.y);

this.sprite.x = this.position.x;
this.sprite.y = this.position.y;
this.sprite.update();

// temporary
bool previouslyGrounded = this.isGrounded;
this.isGrounded = this.position.y >= jjResolutionHeight-32;
if(!previouslyGrounded && this.isGrounded)
this.velocity.y = 0; // impact *sparkles*

// controls

if(this.isLocal) {
// dont gain velocity when colliding with the corners
if(!(this.position.x <= 32))
if(isKeyDown(leftKey))
this.velocity.x -= this.speed;
if(!(this.position.x >= jjResolutionWidth-32))
if(isKeyDown(rightKey))
this.velocity.x += this.speed;

// makes the bike tilt up the faster you are
this.sprite.angle = /*abs*/(this.velocity.x)*3;

if(isKeyDown(jumpKey)) {
if(this.isGrounded) {
if(this.jumpHeld == -1)
this.jumpHeld = 0;
this.velocity.y -= this.jumpForce;
this.jumpHeld += 15; // feedback
}
} else {
this.jumpHeld = 0;
}

if(this.jumpHeld > 0) {
this.velocity.y -= 1;
if(this.jumpHeld > 15) {
this.jumpHeld = -1;
}
}
}
}

void draw(jjCANVAS@ canvas) {
int width = jjGetStringWidth(this.name, STRING::SMALL, normalTextAppearance);

canvas.drawString(
this.position.x-(width/2),
this.position.y-32,
this.name,
STRING::SMALL
);

this.sprite.draw(canvas);
}

void _update() {
@motorPlayers[this.name] = this;
}
};

dictionary motorPlayers = {};
MotorPlayer@ localMotorPlayer;

MotorPlayer@ addPlayer(string name) {
MotorPlayer@ motorPlayer = MotorPlayer();
motorPlayer.name = name;
// motorPlayers.insertLast(motorPlayer);
motorPlayers.set(name, @motorPlayer);

return motorPlayer;
}

void onLevelBegin() {
jjConsole("Started!");

@leftKey = getKeyById("A");
@rightKey = getKeyById("D");
@jumpKey = getKeyById("Space");

jjAnimSets[playerAnimSet].load(
jjPIXELMAP("STVmotorracer_split.png"),
frameWidth: 64,
frameHeight: 64,
frameSpacingX: 4,
frameSpacingY: 0,
startY: 0,
firstAnimToOverwrite: jjAnimSets[playerAnimSet]
);

auto p = addPlayer("Steve");
p.isLocal = true;
p._update();

@localMotorPlayer = p;
}

void onMain() {
for(uint i = 0; i < motorPlayers.getKeys().length(); i++)
cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).update();
}

bool onDrawAmmo(jjPLAYER@ player, jjCANVAS@ canvas) {
canvas.drawRectangle(0, 0, jjResolutionWidth, jjResolutionHeight, 0);

for(uint i = 0; i < motorPlayers.getKeys().length(); i++)
cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).draw(canvas);

canvas.drawString(0, 16, "Velocity: "+localMotorPlayer.velocity.x+", "+localMotorPlayer.velocity.y);

return false;
}
Binary file removed levels/motorracer/assets/frame_0_delay-0.1s.gif
Binary file not shown.
Binary file removed levels/motorracer/assets/frame_1_delay-0.1s.gif
Binary file not shown.
Binary file removed levels/motorracer/assets/frame_2_delay-0.1s.gif
Binary file not shown.
Binary file removed levels/motorracer/assets/frame_3_delay-0.1s.gif
Binary file not shown.
Binary file removed levels/motorracer/assets/frame_4_delay-0.1s.gif
Binary file not shown.
Binary file added levels/motorracer/assets/split.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added levels/motorracer/assets/wrooom.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion levels/motorracer/run.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ for %%i in (*.j2l *.j2t) do (
copy "%%i" "%GAME_DIRECTORY%" /y
)
for %%i in (./assets/*.*) do (
copy ".\assets\%%i" "%GAME_DIRECTORY%\STVmotor_racer.j2as_%%i" /y
copy ".\assets\%%i" "%GAME_DIRECTORY%\STVmotorracer_%%i" /y
)
for %%i in (*.j2as *.mut *.asc) do (
python ../../experiments/angelscriptpp/angelscriptpp.py "%%i" "%GAME_DIRECTORY%\%%i"
Expand Down
16 changes: 10 additions & 6 deletions scripts/STVutil.asc
Original file line number Diff line number Diff line change
Expand Up @@ -361,19 +361,19 @@ class AnimatedSprite
canvas.drawRotatedSprite(
this.x, this.y,
this.useIntegerAnimationSet ? this.animSetInteger : this.animSet, this.id, int(this.frame),
// COMPATIBILITY: 5.10, this.angle
this.direction, this.xScale, this.yScale,
// COMPATIBILITY: 5.10, this.direction
this.angle, this.xScale, this.yScale,
this.spriteMode, this.spriteModeParam);
}
}
}

class Vector2
{
uint x;
uint y;
uint width;
uint height;
int x;
int y;
int width;
int height;

// i know that it's incorrect to have width and height in Vector2
// but trust me please :)
Expand Down Expand Up @@ -1398,6 +1398,10 @@ Key @getKeyById(string id)
}
}

if(@foundKey == @null) {
jjConsole("getKeyById("+id+") returned null!");
}

return foundKey;
}

Expand Down

0 comments on commit cd135a4

Please sign in to comment.