{$mode objfpc}
program mygame;
uses crt;
type
IStatePlayer = interface
function speed(): IStatePlayer;
function slow(): IStatePlayer;
end;
TPlayerNormal = class(TInterfacedObject, IStatePlayer)
vel: byte;
constructor create();
function speed(): IStatePlayer;
function slow(): IStatePlayer;
end;
TPlayerSpeed = class(TInterfacedObject, IStatePlayer)
vel: byte;
constructor create();
function speed(): IStatePlayer;
function slow(): IStatePlayer;
end;
TPlayer = class
x: byte;
dir: boolean;
state: IStatePlayer;
constructor create();
procedure move();
procedure speed();
procedure slow();
end;
{ TPlayerNormal }
constructor TPlayerNormal.create();
begin
self.vel := 1;
end;
function TPlayerNormal.speed(): IStatePlayer;
begin
result := TPlayerSpeed.create;
end;
function TPlayerNormal.slow(): IStatePlayer;
begin
result := self;
end;
{ TPlayerSpeed }
constructor TPlayerSpeed.create();
begin
self.vel := 2;
end;
function TPlayerSpeed.speed(): IStatePlayer;
begin
result := self;
end;
function TPlayerSpeed.slow(): IStatePlayer;
begin
result := TPlayerNormal.create;
end;
{ TPlayer }
constructor TPlayer.create();
begin
x := 1;
dir := true;
state := TPlayerNormal.create;
end;
procedure TPlayer.speed();
begin
state := TPlayerSpeed.create;
end;
procedure TPlayer.slow();
begin
state := TPlayerNormal.create;
end;
procedure TPlayer.move();
begin
case (dir) of
TRUE : dir := not (x >= 80);
FALSE: dir := not (x <= 1);
end;
case (dir) of
TRUE : x := x + state.vel;
FALSE: x := x - state.vel;
end;
gotoxy(x, 1); write('V');
end;
var Player: TPlayer;
begin
highvideo; cursoroff; clrscr;
Player := TPlayer.create;
repeat
if (keypressed()) then
begin
clrscr;
case (readkey()) of
'w': Player.speed;
's': Player.slow;
' ': exit;
end;
Player.move; writeln; write(Player.state.vel);
end;
until (false);
end.