forked from BuildSucceeded/2D-Platformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
91 lines (72 loc) · 2.34 KB
/
Enemy.cpp
File metadata and controls
91 lines (72 loc) · 2.34 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Settings.h"
#include "Enemy.h"
//#include "Level.h"
#include "Engine.h"
Enemy::Enemy(double initialX, double initialY, double maxX, int type)
{
// initializes the enemy position and maximums
position.x = initialX;
position.y = initialY;
min = initialX;
max = maxX;
enemyType = type;
forward = true;
}
void Enemy::Logic(double elapsedTime)
{
// moves the enemy and turns it around if it gets to the limits
position.x += (forward ? 25 : -25) * elapsedTime;
if (position.x >= max)
{
forward = false;
}
if (position.x <= min)
{
forward = true;
}
}
CollisionDistances Enemy::CharacterCollides(Character* character)
{ // returns collision details if the enemy collides with the character
double charTop = character->GetPosition().y - CHARACTER_HEIGHT;
double charBottom = character->GetPosition().y;
double charLeft = character->GetPosition().x - CHARACTER_WIDTH / 2 + 3;
double charRight = character->GetPosition().x + CHARACTER_WIDTH / 2 - 4;
double enemyTop = position.y - ENEMY_HEIGHT;
double enemyBottom = position.y;
double enemyLeft = position.x - ENEMY_WIDTH / 2 + 3;
double enemyRight = position.x + ENEMY_WIDTH / 2 - 4;
CollisionDistances collision;
collision.top = 0;
collision.bottom= 0;
collision.left = 0;
collision.right = 0;
// If it's a collision
if (charTop < enemyBottom && charBottom > enemyTop&& charRight > enemyLeft&& charLeft < enemyRight)
{
collision.top = abs(charTop - enemyBottom);
collision.bottom = abs(charBottom - enemyTop);
collision.left = abs(charLeft - enemyRight);
collision.right = abs(charRight - enemyLeft);
collision.keepSmallest();
}
return collision;
}
void Enemy::Draw(ID2D1HwndRenderTarget* m_pRenderTarget)
{
if (enemySetImg == NULL) // loads the tileset if needed
{
enemySetImg = engine->LoadImageW(L"enemies.png");
}
D2D1_RECT_F rectangle1 = D2D1::RectF(
position.x - Engine::offset - ENEMY_WIDTH / 2, position.y - ENEMY_HEIGHT,
position.x - Engine::offset + ENEMY_WIDTH / 2 - 1, position.y
);
int posX = (int)position.x % 20 / 10 + (forward ? 2 : 0);
int posY = enemyType;
D2D1_RECT_F rectangle2 = D2D1::RectF(
posX * ENEMY_WIDTH, posY * ENEMY_HEIGHT,
(posX + 1) * ENEMY_WIDTH - 1, (posY + 1) * ENEMY_HEIGHT - 1
);
// drawing the enemy
m_pRenderTarget->DrawBitmap(enemySetImg, rectangle1, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, rectangle2);
}