Skip to content

Commit

Permalink
midsave
Browse files Browse the repository at this point in the history
  • Loading branch information
SpazElectro committed Jul 2, 2023
1 parent b271f3b commit ed12334
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
110 changes: 97 additions & 13 deletions levels/chickeninvadersmultiplayer/STVchickeninvadersmultiplayer.j2as
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,51 @@ class Chicken
}
};

enum SpaceshipWeapon {
BORONRAILGUN = 0,
};

int SpaceshipWeaponBehave(SpaceshipWeapon weapon, int x, int y, int energy) {
int firerate = 0;

switch (weapon)
{
case BORONRAILGUN:
if(energy == 1)
bullets.insertLast(Bullet(x, y, false));
else if(energy == 2) {
bullets.insertLast(Bullet(x - 5, y, false));
bullets.insertLast(Bullet(x + 5, y, false));
}

firerate = 0;

break;

default:
break;
}

return firerate;
}

class SpaceshipPlayer {
AnimatedSprite@ sprite;
jjPLAYER@ player;
bool animatingDirection = false;
int prevmx;
bool isAnimating = false;
SpaceshipWeapon weapon = BORONRAILGUN;
int energy = 2;
int canShootAt = 0;
bool canShoot = true;

SpaceshipPlayer(jjPLAYER@ player) {
@this.player = player;
@this.sprite = AnimatedSprite(0, 4, 150, 150, 0.2, false);
this.sprite.animate = false;
this.sprite.setAnimSet(ANIM::CUSTOM[ASSETS_SPACESHIP]);
this.sprite.xScale = 0.5;
this.sprite.yScale = 0.5;
this.sprite.xScale = 0.5; this.sprite.yScale = 0.5;
@this.sprite.animfinishcallback = function(dictionary@ args) {
SpaceshipPlayer@ spaceship;
args.get("spaceship", @spaceship);
Expand All @@ -134,9 +165,9 @@ class SpaceshipPlayer {
@this.sprite.animfinisharguments = {{"spaceship", @this}};
}

string predictMouseDirection(int prevX) {
int deltaX = jjMouseX - prevX;
int tolerance = 1; // Tolerance threshold for movement detection
string predictMouseDirection(int currX, int prevX) {
int deltaX = currX - prevX;
int tolerance = 0; // Tolerance threshold for movement detection

if (deltaX > tolerance)
return "Right";
Expand All @@ -146,24 +177,41 @@ class SpaceshipPlayer {
return "None";
}

void shoot() {
if(canShoot || jjGameTicks >= canShootAt) {
canShootAt = jjGameTicks + SpaceshipWeaponBehave(this.weapon, this.sprite.x - 3, this.sprite.y - 20, this.energy);
canShoot = false;
}
}

void update() {
string mouseDirection = predictMouseDirection(prevmx);
this.prevmx = jjMouseX;
string mouseDirection = predictMouseDirection(this.sprite.x, prevmx);
this.prevmx = this.sprite.x;

if(this.player.keyLeft)
this.sprite.x -= 1;
if(this.player.keyRight)
this.sprite.x += 1;
if(this.player.keyUp)
this.sprite.y -= 1;
if(this.player.keyDown)
this.sprite.y += 1;
if(this.player.keyFire)
this.shoot();

if (!animatingDirection) {
// jjConsole(mouseDirection);
this.sprite.xScale = 0.5; this.sprite.yScale = 0.5;

if (mouseDirection == "Left") {
this.sprite.frame = 0;
this.sprite.angle = 0;
this.sprite.setId(0);
} else if (mouseDirection == "Right") {
this.sprite.frame = 0;
this.sprite.angle = 180;
this.sprite.xScale = -0.5;
this.sprite.setId(0);
// this.sprite.setId(1);
} else {
this.sprite.frame = 4;
this.sprite.angle = 0;
this.sprite.setId(0);
}

Expand All @@ -180,8 +228,9 @@ class SpaceshipPlayer {
}
}

this.sprite.x = jjMouseX;
this.sprite.y = jjMouseY;

// this.sprite.x = jjMouseX;
// this.sprite.y = jjMouseY;
this.sprite.update();
}

Expand All @@ -190,8 +239,29 @@ class SpaceshipPlayer {
}
};

class Bullet
{
float x, y;
bool isEgg;

Bullet(float x, float y, bool isEgg) {
this.x = x;
this.y = y;
this.isEgg = isEgg;
}

void update() {
this.y -= 4;
}

void draw(jjCANVAS@ canvas) {
canvas.drawRectangle(int(this.x), int(this.y), 2, 4, 15);
}
};

array<Chicken@> chickens();
array<SpaceshipPlayer@> players();
array<Bullet@> bullets();

void onLevelBegin() {
jjConsole("Started!");
Expand Down Expand Up @@ -225,6 +295,15 @@ void onMain() {
{
players[playerIndex].update();
}

for (uint bulletIndex = 0; bulletIndex < bullets.length(); bulletIndex++)
{
bullets[bulletIndex].update();

if(bullets[bulletIndex].y <= 0) {
bullets.removeAt(bulletIndex);
}
}
}

bool onDrawAmmo(jjPLAYER@ player, jjCANVAS@ canvas) {
Expand All @@ -248,5 +327,10 @@ bool onDrawAmmo(jjPLAYER@ player, jjCANVAS@ canvas) {
players[playerIndex].draw(canvas);
}

for (uint bulletIndex = 0; bulletIndex < bullets.length(); bulletIndex++)
{
bullets[bulletIndex].draw(canvas);
}

return true;
}
Binary file not shown.
4 changes: 2 additions & 2 deletions scripts/STVutil.asc
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ class AnimatedSprite
{
if (this.visible)
{
int x = 0;
if(direction != SPRITE::FLIPNONE) x = direction; else x = angle;
int x = angle;
if(direction != SPRITE::FLIPNONE) x = direction;

canvas.drawRotatedSprite(
this.x, this.y,
Expand Down

0 comments on commit ed12334

Please sign in to comment.