-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bunny.cpp
153 lines (113 loc) · 2.52 KB
/
Bunny.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "StdAfx.h"
#include "Img.h"
#include "Bunny.h"
#include <cstdlib>
int Bunny::m_bunnies_overall = 0;
Bunny::Bunny():
m_image(0)
{
m_name = RandomName();
// 50% chance of male
m_is_male = (rand() % 2 == 0);
m_is_hazardous_vampire = RandomHazardous();
m_age = 0;
m_image = new Img(RandomImgPath());
m_bunnies_overall++;
//MessageBox(NULL, m_name.c_str(), L"New bunny was born", MB_OK);
}
Bunny::~Bunny() {
delete m_image;
m_image = 0;
m_bunnies_overall--;
//std::wstring info_text = L"Bunny " + m_name + L" has died...";
//MessageBox(NULL, info_text.c_str(), L"Sad news", MB_OK);
}
void Bunny::Draw(HDC hDc) {
m_image->Draw(hDc, m_pos->m_x, m_pos->m_y, m_pos->m_width, m_pos->m_height);
// Let's write bunny's name, shall we?
HFONT arial_font;
HFONT old_font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfOrientation = 0;
lf.lfWeight = FW_NORMAL;
lf.lfHeight = -8;
lstrcpy(lf.lfFaceName, L"Arial");
arial_font = CreateFontIndirect(&lf);
old_font = (HFONT)::SelectObject(hDc, arial_font);
std::wstring display_text = m_name;
display_text += L"(";
display_text += helpers::IntToString(m_age);
display_text += L")";
if (m_is_male) {
if (m_age > 2) {
display_text += L"/M";
} else {
display_text += L"/m";
}
} else {
if (m_age > 2) {
display_text += L"/F";
} else {
display_text += L"/f";
}
}
if (m_is_hazardous_vampire) {
display_text += L"/H";
}
TextOutW(hDc, m_pos->m_x, m_pos->m_y - 15, display_text.c_str(), display_text.length());
SelectObject(hDc, old_font);
DeleteObject(arial_font);
}
std::wstring Bunny::RandomName() {
std::wstring names[12] = {
L"Alex",
L"Bailey",
L"Brett",
L"Cameron",
L"Casey",
L"Chris",
L"Cody",
L"Dale",
L"Dee",
L"Drew",
L"Evan",
L"Hunter"
};
return names[rand() % 12];
}
std::wstring Bunny::RandomImgPath() {
std::wstring images_paths[4] = {
L"images/bunny0.bmp",
L"images/bunny1.bmp",
L"images/bunny2.bmp",
L"images/bunny3.bmp"
};
return images_paths[rand() % 4];
}
bool Bunny::RandomHazardous() {
int percentage = rand() % 100;
// Basically making it 2% chance of being hazardous
if (percentage < 2) {
return true;
}
return false;
}
void Bunny::set_img(Img* img) {
delete m_image;
m_image = 0;
m_image = img;
}
Img* Bunny::MakeImgCopy() const {
return m_image->MakeCopy();
}
void Bunny::set_pos(Position* pos) {
pos->m_is_occupied = true;
m_pos = pos;
}
int Bunny::get_bunnies_overall() {
return m_bunnies_overall;
}