-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackpack.cpp
More file actions
157 lines (139 loc) · 3.59 KB
/
Copy pathbackpack.cpp
File metadata and controls
157 lines (139 loc) · 3.59 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
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
154
155
156
157
/* Version: 06.1746 */
/* CHANGE LOG */
// ver. 06.1520 : added backpack system
#include "backpack.h"
using namespace std;
char Backpack::getch()
{
char buf = 0;
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ~ICANON");
return buf;
}
void Backpack::printCentered(const std::string &text, int width)
{
int padding = (width - text.size()) / 2;
if (padding > 0)
std::cout << std::string(padding, ' ');
std::cout << text;
}
// render backpack interface
void Backpack::render(Item **items, int cursor, int screenWidth)
{
system("clear");
int titleWidth = screenWidth - 4;
printCentered("======= 背包系統 =======", screenWidth);
std::cout << "\n\n";
for (size_t i = 0; i < backpackCount; ++i)
{
std::cout << (i == cursor ? " > " : " "); // cursor appearance
items[i]->print();
std::cout << "\n";
}
std::cout << "\n";
printCentered("使用方向鍵選擇,Enter 確認使用", screenWidth);
cout << '\n';
printCentered("按【 esc 】回到遊戲", screenWidth);
std::cout << "\n";
}
void Backpack::expandSize(int newBlockCnt)
{
backpackLimit += newBlockCnt;
Item **newBackpack = new Item *[backpackLimit];
for (int i = 0; i < backpackCount; i++)
delete items[i];
delete[] items;
items = newBackpack;
}
Backpack::Backpack()
{
items = nullptr;
backpackCount = 0;
backpackLimit = 0;
}
Backpack::Backpack(int limit)
{
backpackCount = 0;
backpackLimit = 3;
expandSize(3);
}
Backpack::~Backpack()
{
delete[] items;
}
void Backpack::addItem(Item &item)
{
items[backpackCount++] = &item;
}
void Backpack::useItem(int itemIndex, Player &aPlayer)
{
items[itemIndex]->useItem(aPlayer);
// 移除物品
for (int i = itemIndex; i < backpackCount - 1; ++i)
{
items[i] = items[i + 1];
}
backpackCount--;
}
// 主程序
void Backpack::openBackpack(Player &player)
{
int cursor = 0; // cursor init pos
const int screenWidth = 50; // terminal width
while (true)
{
// 渲染界面
render(items, cursor, screenWidth);
// 捕获用户输入
char input = getch();
if (input == '\033')
{
char secondChar = getch(); // skip [
if (secondChar == '[')
{
char thirdChar = getch();
switch (thirdChar)
{
case 'A': // /033[A
cursor = (cursor - 1 + backpackCount) % backpackCount;
case 'B': // /033[B
cursor = (cursor + 1) % backpackCount;
}
}
else
{
break;
}
}
else if (input == '\n')
{ // Enter
if (backpackCount == 0)
{
break;
}
system("clear");
std::cout << "你使用了 ";
useItem(cursor, player);
items[cursor]->print();
std::cout << "!" << std::endl;
cout << "按【 Enter 】回到遊戲" << endl;
while (getch() != '\n')
{
}
return;
}
}
}