blob: 1492f6315accb6eaa8cbb1c2c289011c2a0ddc42 (
plain) (
blame)
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
|
/**
* Copyright (C) David Wolfe, 1999. All rights reserved.
* Ported to Qt and adapted for TDDD86, 2015.
*/
#include "Unit.h"
#include "constants.h"
#include "utilities.h"
#include <cstdlib>
#include <cmath>
Unit::Unit() {
teleport();
}
Unit::Unit(const Unit& u) {
x = u.x;
y = u.y;
}
Unit::Unit(const Point& p) {
x = p.x;
y = p.y;
}
Point Unit::asPoint() const {
return Point{x, y};
}
bool Unit::at(const Unit& u) const {
return (x == u.x && y == u.y);
}
bool Unit::attacks(const Unit& u) const {
return (abs(x - u.x) <= 1 &&
abs(y - u.y) <= 1);
}
void Unit::moveTowards(const Unit& u) {
if (x > u.x) x--;
if (x < u.x) x++;
if (y > u.y) y--;
if (y < u.y) y++;
checkBounds();
}
void Unit::teleport() {
x = rand_int (MIN_X, MAX_X);
y = rand_int (MIN_Y, MAX_Y);
}
double Unit::distanceTo(const Unit& u) const {
double dx = u.x - x;
double dy = u.y - y;
return sqrt(dx * dx + dy * dy);
}
/*
* Put this unit inside playing field if outside
*/
void Unit::checkBounds() {
if (x < MIN_X) x = MIN_X;
if (x > MAX_X) x = MAX_X;
if (y < MIN_Y) y = MIN_Y;
if (y > MAX_Y) y = MAX_Y;
}
|