-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
81 lines (66 loc) · 1.51 KB
/
Enemy.cpp
File metadata and controls
81 lines (66 loc) · 1.51 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
/*
Enemy.cpp
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - Data Structures and Algorithms Project
*/
#include "Enemy.h"
#include <stdlib.h>
#include <time.h>
// Constructor
Enemy::Enemy() {
x = y = 300;
dx = 4 - (rand() % 8);
dy = 4 - (rand() % 8);
frozen = false;
freezeTimer = 0;
}
// Move the enemy
bool Enemy::move(int grid[][40], int tileSize) {
if (frozen) return false;
x += dx;
// Check if hit a wall
if (grid[y / tileSize][x / tileSize] == 1) {
dx = -dx;
x += dx;
}
// Check if hit a trail
if (grid[y / tileSize][x / tileSize] == 2 || grid[y / tileSize][x / tileSize] == 3) {
return true; // Hit a trail
}
y += dy;
// Check if hit a wall
if (grid[y / tileSize][x / tileSize] == 1) {
dy = -dy;
y += dy;
}
// Check if hit a trail
if (grid[y / tileSize][x / tileSize] == 2 || grid[y / tileSize][x / tileSize] == 3) {
return true; // Hit a trail
}
return false; // No collision with trail
}
// Update enemy state
void Enemy::update(float deltaTime) {
if (frozen) {
freezeTimer -= deltaTime;
if (freezeTimer <= 0) {
frozen = false;
}
}
}
// Freeze the enemy for a duration
void Enemy::freeze(float duration) {
frozen = true;
freezeTimer = duration;
}
// Getters
int Enemy::getX() const {
return x;
}
int Enemy::getY() const {
return y;
}
bool Enemy::isFrozen() const {
return frozen;
}