-
Notifications
You must be signed in to change notification settings - Fork 10
/
engine.pas
176 lines (163 loc) · 4.89 KB
/
engine.pas
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
unit Engine;
interface
uses
canvas,
outputcolor,
echo,
effects,
conditions,
types;
function Play(locations: TLocations; var status: TStatus): Boolean;
function Fall(locations: TLocations; var status: TStatus; var isPlaying: Boolean): Boolean;
function ChangeLocation(locations: TLocations; var location: TLocation; transition: TTransition; lineTransition: Integer): Boolean;
function ChangeEvent(var currentPosition: TPosition; toEvent: String): Boolean;
function ChooseTransition(hero: THero; antiHero: THero; transitions: TTransitions; var transition: TTransition): Boolean;
function ChooseCommand(commands: TCommands; var cmd: String; var command: TCommand): Boolean;
implementation
function ChooseCommand(commands: TCommands; var cmd: String; var command: TCommand): Boolean;
var
I: Integer;
begin
GotoXY(0, ConsoleHeight);
if Length(commands) = 0 then
begin
readln;
cmd := 'game_over';
Exit(False);
end;
ColorWrite('Введите команду: ', ColorDefault);
ReadLn(cmd);
if cmd = '' then
Exit(True);
LineChar(17, ConsoleHeight, 4, ' ');
for I := 0 to Length(commands) - 1 do
begin
if commands[I].cmd = cmd then
begin
command := commands[I];
Exit(True);
end;
end;
Exit(False);
end;
function ChooseTransition(hero: THero; antiHero: THero; transitions: TTransitions; var transition: TTransition): Boolean;
var
I: Integer;
begin
for I := 0 to Length(transitions) - 1 do
begin
if (CheckConditions(hero, antiHero, transitions[I].conditions)) then
begin
transition := transitions[I];
Exit(True);
end;
end;
end;
function ChangeEvent(var currentPosition: TPosition; toEvent: String): Boolean;
var
I: Integer;
begin
if toEvent <> '' then
begin
for I := 0 to Length(currentPosition.location.events) - 1 do
begin
if currentPosition.location.events[I].name.text = toEvent then
begin
currentPosition.event := currentPosition.location.events[I];
Exit(True);
end;
end;
end;
Exit(False);
end;
function ChangeLocation(locations: TLocations; var location: TLocation; transition: TTransition; lineTransition: Integer): Boolean;
var
I: Integer;
begin
if (transition.toLocation <> '') and (transition.toLocation <> location.name.text) then
begin
for I := 0 to (Length(locations) - 1) do
begin
if locations[I].name.text = transition.toLocation then
begin
location := locations[I];
if transition.name <> 'Over' then
PrintTransLocation(transition, lineTransition);
PrintLocation(location.name);
Exit(True);
end;
end;
end;
Exit(False);
end;
function Fall(locations: TLocations; var status: TStatus; var isPlaying: Boolean): Boolean;
var
isFalling: Boolean;
command: TCommand;
transition: TTransition;
cmd: String;
line: Integer;
lineTransition: Integer;
I: Integer;
begin
cmd := '';
transition.toEvent := status.currentPosition.event.name.text;
line := 0;
lineTransition := 0;
isFalling := ChooseCommand(status.currentPosition.event.commands, cmd, command);
if not isFalling then
begin
if cmd = 'exit' then
Exit(False);
if cmd = 'restart' then
begin
isPlaying := true;
Exit(False);
end;
end;
CanvasFlush();
PrintCommand(command, line);
ChooseTransition(status.hero, status.antiHero, command.transitions, transition);
if transition.name = 'Over' then
begin
CanvasForseFlush();
//CanvasClear();
for I := 0 to Length(command.conditions) - 1 do
begin
if CheckCondition(status.hero, status.antiHero, command.conditions[I]) then
PrintConditionText(command.conditions[I], line);
end;
//ChooseTransition(status.hero, status.antiHero, command.transitions, transition);
PrintTransition(transition, line);
//Affect(status.hero, status.antiHero, transition.effects);
ChangeLocation(locations, status.currentPosition.location, transition, lineTransition);
isFalling := ChangeEvent(status.currentPosition, transition.toEvent);
PrintEvent(status.currentPosition.event, line, True);
ColorWrite('Нажмите Enter для выхода ', ColorDefault);
Read;
ReadLn;
Exit(False);
end;
if transition.name = 'GoodOver' then
status.isCredits := true;
PrintTransition(transition, line);
Affect(status.hero, status.antiHero, transition.effects);
ChangeLocation(locations, status.currentPosition.location, transition, lineTransition);
isFalling := ChangeEvent(status.currentPosition, transition.toEvent);
PrintStatsChangingHero(status.hero);
PrintEvent(status.currentPosition.event, line);
PrintCommands(status.currentPosition.event.commands);
Exit(isFalling);
end;
function Play(locations: TLocations; var status: TStatus): Boolean;
var
isFalling: Boolean;
isPlaying: Boolean;
begin
isPlaying := false;
repeat
isFalling := Fall(locations, status, isPlaying);
until (not isFalling);
Exit(isPlaying);
end;
end.