-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectile.cpp
57 lines (49 loc) · 1.69 KB
/
projectile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "projectile.h"
#include "player.h"
#include <iostream>
Projectile::Projectile():Interactable2D(QVector2D(0,0),QVector2D(0,0),0),lifespan(3),damage(1),speed(2),direction(QVector2D(0,0)){
alive=true;
initspeed = speed;
setCollider(Hitbox(QVector2D(0,0),1,1));//porte ont un collider spécial
timer.start(lifespan*1000,this);
}
Projectile::Projectile(QVector2D pos,QVector2D text,int collidetime,int lifetime,int dmg,float sp,QVector2D dir): Interactable2D (pos,text,collidetime),lifespan(lifetime),damage(dmg),speed(sp),direction(dir){
alive = true;
initspeed = speed;
setCollider(Hitbox(pos,0.5,0.5));
timer.start(lifespan*1000,this);
}
//timer durée de vie de la pile
void Projectile::timerEvent(QTimerEvent *e){
alive = false;
timer.stop();
}
//permet de réduire la vitesse des projectiles
void Projectile::changeSpeed(float factor){
speed = initspeed * factor;
}
//permet de supprimer un projectile ayant atteint sa portée, sinon de le bouger dans sa direction
int Projectile::Update(){
if (!alive){
return -1;
}
QVector2D movement = direction * speed;
movement.normalize();
movement *= speed;
position += (movement * 0.166667);
collider.setPoint(QVector2D(position.x(),position.y()));
renderer.SetPosition(position);
renderer.CreateGeometry();
speed = initspeed;
return 0;
}
//lorsqu'on trigger avec un joueur, se détruire en lui faisant des dégats
int Projectile::OnTriggerEnter(Interactable2D* other){
Player* player = dynamic_cast<Player*> (other);
if(player != nullptr){
if (player->canCollide)
player->PlayDamageSound();
player->Damage(1);
alive = false;
}
}