xxxxxxxxxx
300
//https://p5play.org/learn/sprite.html
//animated sprite (character)
let fireKnight;
//variables
let normalAttackCount = 0;
let rV;
let attackTime = 0;
let smashTime = 120;
//switch
let normalAttackSmash = false;
let attackLaunch = false;
let moveReset = false;
function setup() {
new Canvas(800, 400);
noSmooth();
// noCursor();
//animationFireKnight
fireKnight = new Sprite(width / 2, height / 2);
fireKnight.addAni(
"atk1",
"sprite/fireKnight/atk1/1_atk_1.png",
"sprite/fireKnight/atk1/1_atk_2.png",
"sprite/fireKnight/atk1/1_atk_3.png",
"sprite/fireKnight/atk1/1_atk_4.png",
"sprite/fireKnight/atk1/1_atk_5.png",
"sprite/fireKnight/atk1/1_atk_6.png",
"sprite/fireKnight/atk1/1_atk_7.png", // keyframe to attack 2
"sprite/fireKnight/atk1/1_atk_8.png",
"sprite/fireKnight/atk1/1_atk_9.png",
"sprite/fireKnight/atk1/1_atk_10.png",
"sprite/fireKnight/atk1/1_atk_11.png"
);
fireKnight.addAni(
"atk2",
"sprite/fireKnight/atk2/2_atk_1.png",
"sprite/fireKnight/atk2/2_atk_2.png",
"sprite/fireKnight/atk2/2_atk_3.png",
"sprite/fireKnight/atk2/2_atk_4.png",
"sprite/fireKnight/atk2/2_atk_5.png",
"sprite/fireKnight/atk2/2_atk_6.png",
"sprite/fireKnight/atk2/2_atk_7.png",
"sprite/fireKnight/atk2/2_atk_8.png",
"sprite/fireKnight/atk2/2_atk_9.png",
"sprite/fireKnight/atk2/2_atk_10.png",
"sprite/fireKnight/atk2/2_atk_11.png",
"sprite/fireKnight/atk2/2_atk_12.png",
"sprite/fireKnight/atk2/2_atk_13.png",
"sprite/fireKnight/atk2/2_atk_14.png",
"sprite/fireKnight/atk2/2_atk_15.png",
"sprite/fireKnight/atk2/2_atk_16.png",
"sprite/fireKnight/atk2/2_atk_17.png",
"sprite/fireKnight/atk2/2_atk_18.png", // keyframe to attack 3
"sprite/fireKnight/atk2/2_atk_19.png"
);
fireKnight.addAni(
"atk3",
"sprite/fireKnight/atk3/3_atk_1.png",
"sprite/fireKnight/atk3/3_atk_2.png",
"sprite/fireKnight/atk3/3_atk_3.png",
"sprite/fireKnight/atk3/3_atk_4.png",
"sprite/fireKnight/atk3/3_atk_5.png",
"sprite/fireKnight/atk3/3_atk_6.png",
"sprite/fireKnight/atk3/3_atk_7.png",
"sprite/fireKnight/atk3/3_atk_8.png",
"sprite/fireKnight/atk3/3_atk_9.png",
"sprite/fireKnight/atk3/3_atk_10.png",
"sprite/fireKnight/atk3/3_atk_11.png",
"sprite/fireKnight/atk3/3_atk_12.png",
"sprite/fireKnight/atk3/3_atk_13.png",
"sprite/fireKnight/atk3/3_atk_14.png",
"sprite/fireKnight/atk3/3_atk_15.png",
"sprite/fireKnight/atk3/3_atk_16.png",
"sprite/fireKnight/atk3/3_atk_17.png",
"sprite/fireKnight/atk3/3_atk_18.png",
"sprite/fireKnight/atk3/3_atk_19.png",
"sprite/fireKnight/atk3/3_atk_20.png",
"sprite/fireKnight/atk3/3_atk_21.png",
"sprite/fireKnight/atk3/3_atk_22.png",
"sprite/fireKnight/atk3/3_atk_23.png",
"sprite/fireKnight/atk3/3_atk_24.png",
"sprite/fireKnight/atk3/3_atk_25.png",
"sprite/fireKnight/atk3/3_atk_26.png",
"sprite/fireKnight/atk3/3_atk_27.png",
"sprite/fireKnight/atk3/3_atk_28.png"
);
fireKnight.addAni(
"atksp",
"sprite/fireKnight/atksp/sp_atk_1.png",
"sprite/fireKnight/atksp/sp_atk_2.png",
"sprite/fireKnight/atksp/sp_atk_3.png",
"sprite/fireKnight/atksp/sp_atk_4.png",
"sprite/fireKnight/atksp/sp_atk_5.png",
"sprite/fireKnight/atksp/sp_atk_6.png",
"sprite/fireKnight/atksp/sp_atk_7.png",
"sprite/fireKnight/atksp/sp_atk_8.png",
"sprite/fireKnight/atksp/sp_atk_9.png",
"sprite/fireKnight/atksp/sp_atk_10.png",
"sprite/fireKnight/atksp/sp_atk_11.png",
"sprite/fireKnight/atksp/sp_atk_12.png",
"sprite/fireKnight/atksp/sp_atk_13.png",
"sprite/fireKnight/atksp/sp_atk_14.png",
"sprite/fireKnight/atksp/sp_atk_15.png",
"sprite/fireKnight/atksp/sp_atk_16.png",
"sprite/fireKnight/atksp/sp_atk_17.png",
"sprite/fireKnight/atksp/sp_atk_18.png"
);
fireKnight.addAni(
"idle",
"sprite/fireKnight/idle/idle_1.png",
"sprite/fireKnight/idle/idle_2.png",
"sprite/fireKnight/idle/idle_3.png",
"sprite/fireKnight/idle/idle_4.png",
"sprite/fireKnight/idle/idle_5.png",
"sprite/fireKnight/idle/idle_6.png",
"sprite/fireKnight/idle/idle_7.png",
"sprite/fireKnight/idle/idle_8.png"
);
// fireKnight.addAni(
// "block",
// "sprite/fireKnight/block/defend_1.png",
// "sprite/fireKnight/block/defend_2.png",
// "sprite/fireKnight/block/defend_3.png",
// "sprite/fireKnight/block/defend_4.png",
// "sprite/fireKnight/block/defend_5.png",
// "sprite/fireKnight/block/defend_6.png",
// "sprite/fireKnight/block/defend_7.png",
// "sprite/fireKnight/block/defend_8.png",
// "sprite/fireKnight/block/defend_9.png",
// "sprite/fireKnight/block/defend_10.png"
// )
// fireKnight.addAni(
// "hit",
// "sprite/fireKnight/hit/take_hit_1.png",
// "sprite/fireKnight/hit/take_hit_2.png",
// "sprite/fireKnight/hit/take_hit_3.png",
// "sprite/fireKnight/hit/take_hit_4.png",
// "sprite/fireKnight/hit/take_hit_5.png",
// "sprite/fireKnight/hit/take_hit_6.png"
// )
// fireKnight.addAni(
// "down",
// "sprite/fireKnight/down/death_1.png",
// "sprite/fireKnight/down/death_2.png",
// "sprite/fireKnight/down/death_3.png",
// "sprite/fireKnight/down/death_4.png",
// "sprite/fireKnight/down/death_5.png",
// "sprite/fireKnight/down/death_6.png",
// "sprite/fireKnight/down/death_7.png",
// "sprite/fireKnight/down/death_8.png",
// "sprite/fireKnight/down/death_9.png",
// "sprite/fireKnight/down/death_10.png",
// "sprite/fireKnight/down/death_11.png",
// "sprite/fireKnight/down/death_12.png",
// "sprite/fireKnight/down/death_13.png"
// )
fireKnight.ani = "idle";
fireKnight.scale = 2;
}
function draw() {
imageMode(CENTER);
//set frame delay
fireKnight.ani.frameDelay = 8;
toAnotherAni(fireKnight,"atk1",10,"idle");
toAnotherAni(fireKnight,"atk2",18,"idle");
toAnotherAni(fireKnight,"atk3",27,"idle");
toAnotherAni(fireKnight,"atksp",17,"idle");
toAnotherAni(fireKnight,"block",9,"idle");
toAnotherAni(fireKnight,"hit",5,"idle");
fireKnightController();
fill(255)
textAlign(CENTER,CENTER);
textSize(30);
text("Welcome to Fire Knight's Sword Practice!",width / 2, 100)
textSize(15);
text("Press Up button to do a special attck!",width / 2, 150)
text("Press Down buttun to do a normal attck!",width / 2, 170)
text("In a normal attack, smash your spacebar to make the attack stronger!", width / 2, 190)
}
function fireKnightController()
{
if (kb.presses("ArrowUp") && normalAttackSmash == false) // press up button to do special attack
{
specialAttack(fireKnight);
}
else if (kb.presses("ArrowDown") && normalAttackSmash == false) // press down button to do normal attack
{
normalAttackSmash = true;
}
if (normalAttackSmash == true)
{
normalSmash(fireKnight);
console.log(normalAttackCount)
}
if (normalAttackCount > 0 && normalAttackCount <= 10)
{
rV = map(normalAttackCount,0,10,0,205)
background(50 + rV,50,50)
}
else if (normalAttackCount > 10)
{
background(255,0,0);
}
else
{
background(50);
}
if (attackLaunch == true)
{
normalAttack(fireKnight);
// fireKnight.ani.frame = 0;
}
}
function toAnotherAni(character,name,endFrame,nextAni)
{
if(character.ani.frame >= endFrame && character.ani.name == name)
{
character.ani = nextAni;
character.ani.frame = 0;
attackLaunch = false;
attackTime = 0;
normalAttackCount = 0;
normalAttackSmash = false;
moveReset = false;
}
}
function activateAnimation(character,name)
{
character.ani = name;
character.ani.frame = 0;
}
function normalSmash(character){
attackTime++;
if (kb.presses(' ') && normalAttackSmash == true) {
normalAttackCount ++;
}
if (attackTime >= 120)
{
normalAttackSmash = false;
attackLaunch = true;
character.ani.frame = 0;
}
}
function normalAttack(character){
// console.log("attackLaunch is" + attackLaunch);
// console.log("attackTime is" + attackTime);
// console.log("moveReset is" + moveReset);
// console.log("normalAttackCount is" + normalAttackCount);
// console.log("normalAttackSmash is" + normalAttackSmash);
if (moveReset == false)
{
character.ani.frame = 0;
moveReset = true;
}
if (normalAttackCount <= 5)
{
character.ani = "atk1";
}
else if (normalAttackCount > 5 && normalAttackCount < 10)
{
character.ani = "atk2";
}
else if (normalAttackCount >= 10)
{
character.ani = "atk3";
}
}
function specialAttack(character){
if (character.ani == "atksp")
{
}
else
{
activateAnimation(character,"atksp")
}
}
// function moveChara(){
// }