Downloads containing sword.mut

Downloads
Name Author Game Mode Rating
JJ2+ Only: Szmol Weapon Pack vol. 1 szmol96 Mutator N/A Download file

File preview

#pragma name "Melee sword"
#pragma require "sword.j2a"

/**Melee sword**/
/**Author: szmol96**/

enum packet_type {packet_sendColor, packet_getColors};
uint8 ownSwordColor = 0; //means default, based on fur color
array<uint8> swordColors(32, 0);
uint customAnimID = 0;
int8 fastfireBackup = 0;
bool nowIce = false;
uint8 canStab = 0;

const string saveFile = "swordColor.asdat";
const float pi = 3.14159265;

void findCustomAnimID() { //This function is copied from Bunny Must Die! guns
	while (jjAnimSets[ANIM::CUSTOM[customAnimID]].firstAnim != 0) ++customAnimID;
	
	jjAnimSets[ANIM::CUSTOM[customAnimID]].load(0, "sword.j2a");
}

uint8 getPlayerFur(uint8 playNum, uint8 furNum) {
	array<uint8> furColors(4, 0);
	jjPlayers[playNum].furGet(furColors[0], furColors[1], furColors[2], furColors[3]);
	return furColors[furNum];
}

void sendColorPacket(int pNum, uint8 color) {
	jjSTREAM packet;
	packet.push(uint8(packet_sendColor));
	packet.push(pNum);
	packet.push(color);
	jjSendPacket(packet);
}

void sendColorPacket2(int pNum, uint8 color, int8 toPlayerID) {
	jjSTREAM packet;
	packet.push(uint8(packet_getColors));
	packet.push(pNum);
	packet.push(color);
	packet.push(color);
	jjSendPacket(packet);
}

void swordBullet(jjOBJ@ sword) {
	jjPLAYER@ p = jjPlayers[sword.creatorID];
	
	if (sword.state == STATE::START) {
		sword.age = 0;
		
		if (abs(sword.yAcc) > abs(sword.xAcc)) {
			sword.xAcc = sword.xSpeed = 0;
			sword.yAcc = sword.ySpeed = 10;
		} else {
			sword.xAcc = sword.xSpeed = 0;
			sword.yAcc = sword.ySpeed = 0;
		}
		
		jjSample(sword.xPos, sword.yPos, SOUND::COMMON_FOEW3, 63, 16000);
		sword.state = STATE::FLY;
	}
	
	if (sword.yAcc == 0) {
		sword.xPos = p.xPos + p.direction * (jjSin((sword.age) * 23) * 15);
		sword.yPos = p.yPos + -1 * (jjCos((sword.age) * 23) * 15);
		p.ballTime = 3;
		sword.frameID = sword.age;
		canStab = 35;
		
		if (sword.age > 47 || jjMaskedHLine(p.xPos - 12, 24, p.yPos + 21)) sword.delete();
	} else {
		sword.xPos = p.xPos + p.direction * 20;
		sword.yPos = p.yPos;
		p.buttstomp = 115;
		p.ySpeed = sword.ySpeed;
		sword.frameID = 26;
		canStab = 70;
		
		if (sword.age > 25) sword.delete();
		if (jjMaskedHLine(p.xPos - 12, 24, p.yPos + 21)) {
			addWave(p.xPos + 8, p.yPos + 20 + jjMaskedTopVLine(p.xPos, p.yPos + 21, 32) - 16, 1, sword.creatorID);
			addWave(p.xPos - 8, p.yPos + 20 + jjMaskedTopVLine(p.xPos, p.yPos + 21, 32) - 16, -1, sword.creatorID);
			sword.delete();
		}
	}
	
	sword.direction = p.direction;
	sword.age++;
	sword.determineCurFrame(true);
	
	if (sword.yAcc == 0) {
		for (uint8 i = 4; i > 0; --i) {
			if (sword.age > i) {
				if (swordColors[p.playerID] == 0) {
					jjDrawSpriteFromCurFrame(p.xPos + p.direction * (jjSin((sword.age - i) * 23) * 15), p.yPos + -1 * (jjCos((sword.age - i) * 23) * 15), sword.curFrame - i, sword.direction, SPRITE::TRANSLUCENTCOLOR, getPlayerFur(sword.creatorID, 3) + i, 4, 4, -1);
				} else {
					jjDrawSpriteFromCurFrame(p.xPos + p.direction * (jjSin((sword.age - i) * 23) * 15), p.yPos + -1 * (jjCos((sword.age - i) * 23) * 15), sword.curFrame - i, sword.direction, SPRITE::TRANSLUCENTCOLOR, swordColors[p.playerID] + i, 4, 4, -1);
				}
			}
		}
	}
	sword.draw();
}

void swordBulletPU(jjOBJ@ swordpu) {
	jjPLAYER@ p = jjPlayers[swordpu.creatorID];
	
	if (swordpu.state == STATE::START) {
		swordpu.age = 0;
		
		if (abs(swordpu.yAcc) > abs(swordpu.xAcc) && !jjMaskedHLine(p.xPos - 12, 24, p.yPos + 21)) {
			swordpu.yAcc = swordpu.ySpeed = 0;
			swordpu.determineCurAnim(ANIM::CUSTOM[customAnimID], 0, true);
		}
		
		jjSample(swordpu.xPos, swordpu.yPos, SOUND::COMMON_FOEW3, 63, 12000);
		swordpu.state = STATE::FLY;
	}
	
	if (abs(swordpu.yAcc) < abs(swordpu.xAcc)) {
		swordpu.xPos = p.xPos + p.direction * (jjSin((swordpu.age - 12) * 15) * 24 + 8);
		swordpu.yPos = (p.yPos + 4) + -1 * (jjCos((swordpu.age - 12) * 15) * 24);
		
		if (swordpu.age > 30) swordpu.delete();
	} else {
		swordpu.xPos = p.xPos + p.direction * 24;
		swordpu.yPos = p.yPos - swordpu.age / 2;
		
		p.xSpeed = swordpu.xSpeed = swordpu.direction * 3;
		p.ySpeed = swordpu.ySpeed = -12 + swordpu.age / 5;
		
		if (swordpu.age > 16) swordpu.delete();
	}
	
	canStab = 35;
	
	swordpu.direction = p.direction;
	swordpu.frameID = swordpu.age / 2;
	swordpu.age++;
	
	swordpu.determineCurFrame(true);
	
	for (uint8 i = 4; i > 0; --i) {
		if (abs(swordpu.yAcc) < abs(swordpu.xAcc)) {
			if (swordpu.age > i) {
				if (swordColors[p.playerID] == 0) {
					jjDrawSpriteFromCurFrame(p.xPos + p.direction * (jjSin((swordpu.age - 12 - i) * 15) * 24 + 8), (p.yPos + 4) + -1 * (jjCos((swordpu.age - 12 - i) * 15) * 24), swordpu.curFrame - i, swordpu.direction, SPRITE::TRANSLUCENTCOLOR, getPlayerFur(swordpu.creatorID, 3) + i, 4, 4, -1);
				} else {
					jjDrawSpriteFromCurFrame(p.xPos + p.direction * (jjSin((swordpu.age - 12 - i) * 15) * 24 + 8), (p.yPos + 4) + -1 * (jjCos((swordpu.age - 12 - i) * 15) * 24), swordpu.curFrame - i, swordpu.direction, SPRITE::TRANSLUCENTCOLOR, swordColors[p.playerID] + i, 4, 4, -1);
				}
			}
		} else {
			if (swordpu.age > i + 3) {
				if (swordColors[p.playerID] == 0) {
					jjDrawSpriteFromCurFrame(p.xPos + p.direction * 24, p.yPos - (swordpu.age - i) / 2, swordpu.curFrame - i, swordpu.direction, SPRITE::TRANSLUCENTCOLOR, getPlayerFur(swordpu.creatorID, 3) + i, 4, 4, -1);
				} else {
					jjDrawSpriteFromCurFrame(p.xPos + p.direction * 24, p.yPos - (swordpu.age - i) / 2, swordpu.curFrame - i, swordpu.direction, SPRITE::TRANSLUCENTCOLOR, swordColors[p.playerID] + i, 4, 4, -1);
				}
			}
		}
		
	}
	swordpu.draw();
}

void shockWave(jjOBJ@ wave) {
	jjPLAYER@ p = jjPlayers[wave.creatorID];

	if (wave.state == STATE::START) {
		if (jjMaskedVLine(p.xPos + wave.direction * 8, p.yPos + 21, 32)) wave.putOnGround(true);
		wave.age = 0;
		wave.state = STATE::FLY;
	}
	
	wave.xPos = wave.xPos + wave.xSpeed;
	wave.yPos = wave.yPos + wave.ySpeed;
	
	if (jjMaskedVLine(wave.xPos, wave.yPos, 16)) {
		wave.yPos += jjMaskedTopVLine(wave.xPos, wave.yPos - 16, 32) - 16;
	} else {
		wave.delete();
	}
		
	if (wave.age > 36) wave.delete();
	
	wave.frameID = wave.age / 3;
	wave.age++;
	wave.determineCurFrame(true);
	if (swordColors[p.playerID] == 0) {
		jjDrawSpriteFromCurFrame(wave.xPos, wave.yPos, wave.curFrame, wave.direction, SPRITE::SINGLEHUE, getPlayerFur(wave.creatorID, 3), 4, 4, -1);
	} else {
		jjDrawSpriteFromCurFrame(wave.xPos, wave.yPos, wave.curFrame, wave.direction, SPRITE::SINGLEHUE, swordColors[p.playerID], 4, 4, -1);
	}
}

void addWave(float x, float y, int8 dir, uint16 crID) {
	jjOBJ@ wave1;
	
	@wave1 = jjObjects[jjAddObject(OBJECT::BLASTERBULLET, x, y, crID, CREATOR::PLAYER)];
	wave1.direction = dir;
	wave1.xSpeed = dir * 4.5;
	wave1.ySpeed = 0;
	wave1.determineCurAnim(ANIM::CUSTOM[customAnimID], 2, true);
	wave1.playerHandling = HANDLING::PLAYERBULLET;
	wave1.var[6] = 16;
	wave1.behavior = shockWave;
}

void onLevelLoad() {
	findCustomAnimID();
	
	jjAnimations[jjAnimSets[ANIM::AMMO] + 28] = jjAnimations[jjAnimSets[ANIM::CUSTOM[customAnimID]] + 3];
	jjAnimations[jjAnimSets[ANIM::AMMO] + 29] = jjAnimations[jjAnimSets[ANIM::CUSTOM[customAnimID]] + 3];
	jjAnimations[jjAnimSets[ANIM::PICKUPS] + 62] = jjAnimations[jjAnimSets[ANIM::CUSTOM[customAnimID]] + 4];
	
	jjObjectPresets[OBJECT::ICEBULLET].determineCurAnim(ANIM::CUSTOM[customAnimID], 0, true);
	jjObjectPresets[OBJECT::ICEBULLET].special = 0;
	jjObjectPresets[OBJECT::ICEBULLET].bulletHandling = HANDLING::DETECTBULLET;
	jjObjectPresets[OBJECT::ICEBULLET].playerHandling = HANDLING::PLAYERBULLET;
	jjObjectPresets[OBJECT::ICEBULLET].freeze = 0;
	jjObjectPresets[OBJECT::ICEBULLET].lightType = LIGHT::NONE;
	jjObjectPresets[OBJECT::ICEBULLET].var[6] = 8 + 16;
	jjObjectPresets[OBJECT::ICEBULLET].behavior = swordBullet;
	
	jjObjectPresets[OBJECT::ICEBULLETPU].determineCurAnim(ANIM::CUSTOM[customAnimID], 0, true);
	jjObjectPresets[OBJECT::ICEBULLETPU].special = jjObjectPresets[OBJECT::ICEBULLETPU].determineCurAnim(ANIM::CUSTOM[customAnimID], 1, false);
	jjObjectPresets[OBJECT::ICEBULLETPU].bulletHandling = HANDLING::DETECTBULLET;
	jjObjectPresets[OBJECT::ICEBULLETPU].playerHandling = HANDLING::PLAYERBULLET;
	jjObjectPresets[OBJECT::ICEBULLETPU].freeze = 0;
	jjObjectPresets[OBJECT::ICEBULLETPU].lightType = LIGHT::NONE;
	jjObjectPresets[OBJECT::ICEBULLETPU].var[6] = 8 + 16;
	jjObjectPresets[OBJECT::ICEBULLETPU].behavior = swordBulletPU;
	jjWeapons[3].replenishes = true;
	
	jjSTREAM save(saveFile);
	while (!save.isEmpty()) {
		save.pop(ownSwordColor);
	}
}

void onLevelBegin() {
	jjPLAYER@ p = jjLocalPlayers[0];
	jjWeapons[3].allowed = true;
	jjWeapons[3].allowedPowerup = true;
	jjWeapons[3].infinite = true;
	jjWeapons[3].spread = SPREAD::NORMAL;
	jjWeapons[3].style = WEAPON::CAPPED;
	jjWeapons[3].defaultSample = false;
	
	if (!jjIsServer) {
		sendColorPacket(p.playerID, ownSwordColor);
		jjSTREAM packet;
		packet.push(uint8(packet_getColors));
		jjSendPacket(packet);
	} else {
		swordColors[0] = ownSwordColor;
	}
}

void onPlayer(jjPLAYER@ p) {
	if (!jjMaskedHLine(p.xPos - 12, 24, p.yPos + 21) && (p.xSpeed < -2 || p.xSpeed > 2)) {
		p.powerup[3] = false;
	} else {
		p.powerup[3] = true;
	}
	
	if (p.buttstomp < 41 && p.keyFire && canStab == 0 && p.currWeapon == 3) {
		p.buttstomp = 121;
		p.powerup[3] = false;
		p.fireBullet(WEAPON::ICE, false, false, pi);
	}
	
	if (p.currWeapon == 3) {
		if (!nowIce) {
			fastfireBackup = p.fastfire;
		}
		p.fastfire = 35;
	} else {
		if (nowIce) {
			p.fastfire = fastfireBackup;
		}
	}
	
	if (canStab > 0) canStab--;
	nowIce = (p.currWeapon == 3);
}

bool onLocalChat(string &in stringReceived, CHAT::Type chatType) {
	jjPLAYER@ p = jjLocalPlayers[0];
	array<string> results;
	
	if (jjRegexIsValid(stringReceived) && jjRegexMatch(stringReceived, "!swordcolor\\s+(\\d+)", results, true)) {
		ownSwordColor = parseInt(results[1]);
		if (jjIsServer) swordColors[0] = ownSwordColor;
		sendColorPacket(p.playerID, parseInt(results[1]));
		jjSTREAM stream;
		stream.push(ownSwordColor);
		stream.save(saveFile);
		jjAlert("Sword trail color changed to " + ownSwordColor + ".");
		return true;
	}
	return false;
}

void onReceive(jjSTREAM &in packet, int clientID) {
	jjPLAYER@ p = jjLocalPlayers[0];
	uint8 type;
	packet.pop(type);
	if (jjIsServer) {
		switch (type) {
			case packet_sendColor:
			{
				int pNum;
				uint8 color;
				packet.pop(pNum);
				packet.pop(color);
				swordColors[pNum] = color;
				sendColorPacket(pNum, color);
			}
			break;
			case packet_getColors:
			{
				for (uint8 i = 0; i < 32; ++i) {
					sendColorPacket2(i, swordColors[i], clientID);
				}
			}
		}
	} else {
		switch (type) {
			case packet_sendColor:
			{
				int pNum;
				uint8 color;
				packet.pop(pNum);
				packet.pop(color);
				swordColors[pNum] = color;
            }
			break;
			case packet_getColors:
			{
				int pNum;
				uint8 color;
				int8 toPlayerID;
				packet.pop(pNum);
				packet.pop(color);
				packet.pop(toPlayerID);
				if (toPlayerID == p.playerID) swordColors[pNum] = color;
            }
			break;
      }
   }
}