-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathun.Randomlevel.pas
More file actions
224 lines (178 loc) · 9.44 KB
/
un.Randomlevel.pas
File metadata and controls
224 lines (178 loc) · 9.44 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{*****************************************************************************
Copyright (C) 2026 NormalUser
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*****************************************************************************}
unit un.Randomlevel;
{$Mode objfpc} {$H+}
{$COPERATORS OFF}
// ******************** interface *************************
interface
USES SDL2,
un.Structs;
procedure random_level(VAR game1 : TGame_def);
procedure draw_Pixel(screen : PSDL_Renderer; RGB : TSDL_Color; x, y : Integer);
// ******************** implementation ********************
implementation
USES SDL2_image,
un.Defs;
procedure draw_Pixel(screen : PSDL_Renderer; RGB : TSDL_Color; x, y : Integer);
begin
SDL_SetRenderDrawColor(screen, RGB.r, RGB.g, RGB.b, RGB.a);
SDL_RenderDrawPoint(screen, x, y);
end;
procedure draw_Line(screen : PSDL_Renderer; RGB : TSDL_Color; x, y : Integer);
begin
SDL_SetRenderDrawColor(screen, RGB.r, RGB.g, RGB.b, RGB.a);
SDL_RenderDrawLine(screen, x, y, x, ySize);
end;
procedure draw_Terrain_Line(screen : PSDL_Renderer; x, y : Integer);
CONST gradient_iterations = 3;
dark_color = 80;
gradient_color_variance = 40;
VAR gradient_height, height, color, y1 : Integer;
gradient_ratio : Single;
begin
gradient_height := ((ySize - y) DIV gradient_iterations);
for y1 := y to ySize do
begin
height := ySize - y1;
gradient_ratio := Single(height MOD gradient_height) / gradient_height;
color := round(gradient_ratio * gradient_color_variance) + dark_color;
RGB.r := color; RGB.g := color; RGB.b := color; RGB.a := 255;
draw_Pixel(screen, RGB, x, y1);
end;
end;
procedure random_level(VAR game1 : TGame_def);
VAR a, b, s, x, y, yd, w, h, miny, maxy, num,
yfluct, size, speed, xdiff, ydiff : Integer;
//Fmt : PChar;
distance : real;
begin
game1.Boden.Textur := SDL_CreateTexture(app.Renderer,SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, xSize, ySize);
game1.BodenGrey.Textur := SDL_CreateTexture(app.Renderer,SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, xSize, ySize);
SDL_SetRenderTarget(app.Renderer, game1.Boden.Textur); { Renders into texture }
SDL_SetRenderDrawBlendMode(app.Renderer, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(game1.Boden.Textur, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(game1.BodenGrey.Textur, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(app.Renderer, 0, 0, 0, 0);
SDL_RenderClear(app.Renderer);
x := 0; y := 0; yd := 0; miny := 0; maxy := 0; size := 0; game.levelscore := 0;
yfluct := (random(10) - 5);
num := random(Anz_Back) + 1;
backPic.Textur := IMG_LoadTexture(app.Renderer, PChar(backPicNam[num]));
SDL_QueryTexture(backPic.Textur, NIL, NIL, @w, @h);
backPic.Rect.x := 0;
backPic.Rect.y := 0;
backPic.Rect.w := xSize; { Enlarge the images to scale to the width }
if w <> 0 then { of the playing field with variable height. }
backPic.Rect.h := h * xSize DIV w; { DIV by zero !! possible }
game1.TBack := backPic;
//Fmt := 'Loading %s'#13;
//SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, Fmt, [PChar(backPicNam[num])]);
while ((y > (Terrain_ySize2 - 10)) OR (y < 10)) do y := random(Terrain_ySize4);
miny := random(Terrain_ySize4) + 10;
maxy := random(Terrain_ySize4) + Terrain_ySize4 - 10;
if (y > (Terrain_ySize4)) then yd := -1 else yd := 1;
x := 0; { start to draw }
repeat
if ((x mod 3) = 0) then yfluct := (random(11) - 5);
if ( ((y < maxy) AND (y > miny ))
OR ((y > maxy) AND (yfluct < 1))
OR ((y < maxy) AND (yfluct > -1)) ) then y := y + yfluct;
{ draw landscape }
BodenArr[x] := y + Terrain_ySize2; { highest point of "moonsurface" }
draw_Terrain_Line(app.Renderer, x, y + Terrain_ySize2); { Renders into "normal" texture }
SDL_SetRenderTarget(app.Renderer, game1.BodenGrey.Textur);
RGB.r := 80; RGB.g := 80; RGB.b := 80; RGB.a := 255;
draw_Line(app.Renderer, RGB, x, y + Terrain_ySize2);
SDL_SetRenderTarget(app.Renderer, game1.Boden.Textur);
if (yd = 1) then { check for change direction }
begin
if (y > maxy) then
begin
if ( (x < (xSize - 60)) AND (game1.curr_lev.num_landings <= ANZ)) then
begin { landing pad }
s := 15 - game1.difficulty;
if s <= 0 then s := 0; { not negative !! }
size := 45 + s;
game1.curr_lev.landing_x[game1.curr_lev.num_landings] := x;
game1.curr_lev.landing_y[game1.curr_lev.num_landings] := y + Terrain_ySize2;
game1.curr_lev.landing_w[game1.curr_lev.num_landings] := size;
{ get distance from center-top }
xdiff := ((xSizeHalf) - x);
ydiff := (y);
if (xdiff = 0) then begin distance := ydiff end
else if (ydiff = 0) then begin distance := xdiff end
else distance := sqrt((xdiff * xdiff) + (ydiff * ydiff));
game1.curr_lev.landing_speed[game1.curr_lev.num_landings] := land_speed;
game1.curr_lev.landing_score[game1.curr_lev.num_landings] :=
(((16 - (size - 45)) * 100) + (round(distance / 4) * ((game1.difficulty DIV 2) + 1)));
{ Default landing pad color is blue }
RGB := Blue;
{ ----------- bonus colored landing pads if on ------- }
if (game1.opt_lp_bonus) then
begin
if (game1.curr_lev.num_landings <> 0 ) then
begin
speed := random(3);
if(speed = 1 ) then begin RGB := Magenta; end; { Magenta is for landings at 90 % velocity }
if(speed = 2 ) then begin RGB := Green; end; { Green is for landings at 80 % velocity }
{ Set the landing speed and score }
game1.curr_lev.landing_speed[game1.curr_lev.num_landings] := game1.curr_lev.landing_speed[game1.curr_lev.num_landings] - (speed * 0.1);
game1.curr_lev.landing_score[game1.curr_lev.num_landings] := game1.curr_lev.landing_score[game1.curr_lev.num_landings] + TRUNC(speed * 100);
end;
end; { End of Bonus colors Landing Pads }
game1.curr_lev.landing_color[game1.curr_lev.num_landings] := RGB;
for a := 0 to size do { Write the landing pads onto the Terrain }
begin
for b := 0 to BIG do
begin
draw_Pixel(app.Renderer, game1.curr_lev.landing_color[game1.curr_lev.num_landings], x, y + Terrain_ySize2 + b); { Renders into Texture }
SDL_SetRenderTarget(app.Renderer, game1.BodenGrey.Textur);
draw_Pixel(app.Renderer, game1.curr_lev.landing_color[game1.curr_lev.num_landings], x, y + Terrain_ySize2 + b); { Renders into Texture }
SDL_SetRenderTarget(app.Renderer, game1.Boden.Textur);
end;
draw_Terrain_Line(app.Renderer, x, y + Terrain_ySize2 + succ(BIG)); { Renders into Texture }
SDL_SetRenderTarget(app.Renderer, game1.BodenGrey.Textur);
RGB.r := 80; RGB.g := 80; RGB.b := 80; RGB.a := 255;
draw_Line(app.Renderer, RGB, x, y + Terrain_ySize2 + succ(BIG)); { from platform downto ground }
SDL_SetRenderTarget(app.Renderer, game1.Boden.Textur);
BodenArr[x] := y + Terrain_ySize2 - BIG; { highest point of "moonsurface" and thickness of the platform }
INC(x);
end; { End of "Write the landing pads onto the Terrain" }
DEC(X);
INC(game1.curr_lev.num_landings); { game.curr_lev.num_landings++; }
end; { ((x < (xSize - 60)) }
yd := -1;
miny := random(Terrain_ySize4) + 10;
maxy := random(Terrain_ySize4) + (Terrain_ySize4) - 55;
end; { y > maxy }
end; { yd == 1 }
if (yd = -1) then
begin
if (y < miny) then
begin
yd := 1;
miny := random(Terrain_ySize4) + 10;
maxy := random(Terrain_ySize4) + (Terrain_ySize4) - 10;
end; { y := y + yd; }
end; { Ende yd = -1 }
y := TRUNC(y + 0.9 * (1.4 * yd));
INC(x); { outer loop }
until x > (xSize - 1); { start to draw }
DEC(game1.curr_lev.num_landings); { Number of actually existing platforms }
game1.curr_lev.fuel := (Fuel_Full - (game1.difficulty * 25));
if (game1.curr_lev.fuel < min_Fuel) then game1.curr_lev.fuel := min_Fuel;
SDL_SetRenderTarget(app.Renderer, NIL); { Stops rendering to the texture }
end;
end.