This repository was archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
62 lines (48 loc) · 1.45 KB
/
Enemy.cpp
File metadata and controls
62 lines (48 loc) · 1.45 KB
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
58
59
60
61
62
//
// Created by firely-pasha on 6/20/17.
//
#include "Enemy.h"
#include "GameLevel.h"
#include "Texture2D.h"
#include "Bullet.h"
#include <QtDebug>
Enemy::Enemy(QString* name, QVector2D *position, int id) : GameObject(name, position, id)
{
speed = 0.25;
canShoot = true;
int spriteX = 2;
int spriteY = 431;
int spriteWidth = 16;
int spriteHeight = 7;
shouldBeKilled = false;
cooldown = rand() % 200 + 100;
texture = new Texture2D(":resources/sprites/SpriteSheet.png", QRect(spriteX, spriteY, spriteWidth, spriteHeight));
setCollider(new QRect(position->x(), position->y(), texture->getWidth(), texture->getHeight()));
}
void Enemy::update()
{
position->setX(position->x() - speed);
if (position->x() >= 208 || position->x() <= 16)
{
speed *= -1;
position->setY(position->y() - 10);
}
if (position->y() < 42)
{
GameLevel::gameOver = true;
}
updateCollider();
cooldown--;
if (cooldown <= 0)
{
createBullet();
cooldown = rand() % 400 + 300;
}
}
void Enemy::createBullet()
{
Bullet *bullet;
QVector2D *pos = new QVector2D(position->x() + texture->getWidth() / 2, position->y());
bullet = new Bullet(pos, false, GameLevel::gameObjects.rbegin()->first + 1, &canShoot, new QString("Player"), false);
GameLevel::gameObjects.insert(std::pair<int, GameObject*>(GameLevel::gameObjects.rbegin()->first + 1, bullet->getPtr()));
}