📄 main.pas
字号:
if Key = ord('z') then v := v + 1;
end;
procedure TMainForm.DXTimerTimer(Sender: TObject; LagCount: Integer);
var jox,joy,sgox,sgoy : Single;
pt : Tpoint;
updateall : Boolean;
begin
if not DXDraw.CanDraw then Exit;
// DXDraw.Surface.Fill(0); // clear screen
mx := Mouse.CursorPos.x;
my := Mouse.CursorPos.y;
sgox := sgx + Cos256(sga) * sgv;
sgoy := sgy + Sin256(sga) * sgv;
if (sgox <> sgx) or (sgoy<> sgy) then
begin
pt.x := round(sgox) + (64 div 2);
pt.y := Round(sgoy) + (48 div 2);
sgcell := DxDraw.CellAt(pt);
DxDraw.FIsoMap.UpdateArea(pt.x - 32,pt.y - 32,pt.x + 32,pt.y + 32);
// DxDraw.FIsoMap.AddState(sgCell.X,sgCell.Y,[tsDirty]);
sgx := sgox;
sgy := sgoy;
if (Random(150)<5) then
sga := Random(256);
end;
// *** key move sprite map ***
if kdown then jv := jv - 0.03;
if kup then jv := jv + 0.02;
if kleft then dec(ja);
if kright then inc(ja);
// *** basic friction ***
jv := jv - 0.01;
if jv < 0 then jv := 0;
if jv > 1 then jv := 1;
// *** basic motion ***
jox := jx;
joy := jy;
jx := jx + cos256(ja) * jv;
jy := jy + sin256(ja) * jv;
if (jox<>jx) or (joy<>jy) then // recalc jeep position
begin
pt.x := round(jx)+(62 div 2);
pt.y := round(jy)+(51 div 2);
JeepCell := DXDraw.CellAt(pt); // cell location of the jeep
DxDraw.FIsoMap.UpdateArea(JeepCell.x - 32,JeepCell.y - 32,JeepCell.x + 32,JeepCell.y + 32);
DxDraw.FIsoMap.AddState(JeepCell.X,JeepCell.Y,[tsDirty]);
end;
// *** mouse scroll map ***
updateall := False;
if my > DXDraw.Display.Height - 3 then
begin
DXDraw.YOffset := DXDraw.YOffset + 3;
updateall := True;
end;
if my < 3 then
begin
DXDraw.YOffset := DXDraw.YOffset - 3;
updateall := True;
end;
if mx < 3 then
begin
DXDraw.XOffset := DXDraw.XOffset - 3;
updateall := True;
end;
if mx > DXDraw.Display.Width - 3 then
begin
DXDraw.XOffset := DXDraw.XOffset + 3;
updateall := True;
end;
if (Updateall) then
DxDraw.FIsomap.AddAllState([tsDirty]);
// *** draw map ***
DXDraw.DrawIsoMap;
end;
procedure TMainForm.BeforeFlipSprites(Sender: TObject);
var s : string;
w,h : integer;
r : TRect;
begin
// *** draw player ***
// DXImageList.items.find('jeep').draw(DXDraw.Surface,
// trunc(jx) - DXDraw.XOffset, trunc(jy) - DXDraw.YOffset, 15 - (ja div 16));
// *** FPS counter ***
with DXDraw.Surface.Canvas do
begin
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 12;
s := 'FPS: ' + inttostr(DXTimer.FrameRate)+ ' jeep x,y:' + inttostr(JeepCell.X)+','+inttostr(JeepCell.Y) + ' DirtyCellCount = ' + IntToStr(DxDraw.FIsoMap.DirtyCount)+' ';
w := TextWidth(s);
h := TextHeight('Yy');
r.Left := 0;
r.Top := 0;
r.Right := w;
r.Bottom := h;
FillRect(r);
Textout(0, 0, s);
textout(0,100,'sgv: ' + floattostr(sgv) + ' sga: ' + inttostr(sga) + ' Jeep angle:' + IntToStr(ja));
Release;
end;
// *** draw sprites over map ***
DXImageList.items.find('mouse').draw(DXDraw.Surface, mx, my, 0);
end;
procedure TMainForm.DrawImage(var IsoCell : TIsoCell; var ImageIndex : integer;cellx,celly : TGridInt; x, y, layer,
PatternIndex: Integer);
var rx,ry,ii,sgi : integer;
begin
if (layer<>2) then
Exit;
rx := 0; ry := 0;
// this should get the cells immediatly around the someguy (otherwise just draw normally)
if (Self.CellsAjacent(cellx,celly,sgCell.X,sgCell.Y)) then
begin
ii := ImageIndex;
// ii := -1; // uncomment this line to show the cells in question
if (cellx = sgCell.X) and (celly = sgCell.Y) then
begin
if (not (tsPassable in IsoCell.State)) then
sgv := 0.1
else sgv := 0.2;
if (sgv<>0.0) and (tsRough in IsoCell.State) then
begin rx := Random(3)-1; ry := Random(3)-1; end; // rough road
end;
if (CellY-1 <= sgCell.Y) then
DxDraw.DrawImage(Isocell,ii{ImageIndex},cellx,celly,x,y,layer,PatternIndex);
{ sgi := sga div 32;
case sgi of
0 : sgi := 7;
1 : sgi := 0;
2 : sgi := 1;
3 : sgi := 2;
4 : sgi := 3;
5 : sgi := 4;
6 : sgi := 5;
7 : sgi := 6;
end;
}
sgi := ((sga div 32) + 7) and 7; // this line is equivalent to the case statement above
// draw the jeep
DXImageList.items.find('someguy').draw(DXDraw.Surface,
rx + trunc(sgx) - DXDraw.XOffset, ry + trunc(sgy) - DXDraw.YOffset,sgi);
// if cell > jeep (along y axis) then draw the cell after the jeep
if (CellY>=sgCell.y){ and (ImageIndex > BASEIMAGE)} then
DxDraw.DrawImage(IsoCell,ii{ImageIndex},cellx,celly,x,y,layer,PatternIndex);
{ with DXDraw.Surface.Canvas do
begin
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 12;
Textout(x, y, IntToStr(cellx)+','+IntToStr(celly));
Release;
end;
}
// ImageIndex := -1; // image is handled noone else needs to display it
end;
// this should get the cells immediatly around the jeep (otherwise just draw normally)
if (Self.CellsAjacent(cellx,celly,JeepCell.X,JeepCell.Y)) then
begin
ii := ImageIndex;
// ii := -1; // uncomment this line to show the cells in question
if (cellx = JeepCell.X) and (celly = JeepCell.Y) then
begin
if (not (tsPassable in IsoCell.State)) then
jv := 0.0;
if (jv<>0.0) and (tsRough in IsoCell.State) then
begin rx := Random(3)-1; ry := Random(3)-1; end; // rough road
end;
if (CellY-1 <= JeepCell.Y) then
DxDraw.DrawImage(Isocell,ii{ImageIndex},cellx,celly,x,y,layer,PatternIndex);
// draw the jeep
DXImageList.items.find('jeep').draw(DXDraw.Surface,
rx + trunc(jx) - DXDraw.XOffset, ry + trunc(jy) - DXDraw.YOffset, 15 - (ja div 16));
// if cell > jeep (along y axis) then draw the cell after the jeep
if (CellY>=JeepCell.y){ and (ImageIndex > BASEIMAGE)} then
DxDraw.DrawImage(IsoCell,ii{ImageIndex},cellx,celly,x,y,layer,PatternIndex);
{ with DXDraw.Surface.Canvas do
begin
Brush.Style := bsClear;
Font.Color := clWhite;
Font.Size := 12;
Textout(x, y, IntToStr(cellx)+','+IntToStr(celly));
Release;
end;
}
ImageIndex := -1; // image is handled noone else needs to display it
end;
//else just draw normally
end;
function TMainForm.CellsAjacent(c1x, c1y, c2x, c2y: integer): boolean;
begin
Result := False;
if (c1x = c2x)then
begin
result := (c1y = c2y) or (c1y = c2y-1) or (c1y= c2y-2) or (c1y = c2y +1) or (c1y = c2y+2);
end;
if (c1x = c2x-1) then
begin
result := (c1y = c2y) or (c1y = c2y -1) or (c1y = c2y+1);
end;
if (c1x = c2x+1) then
begin
result := (c1y = c2y -1) or (c1y = c2y+1) or (c1y = c2y);
end;
end;
procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var cell : TCellsCoord;
p : TPoint;
begin
if (DxDraw=nil) then Exit;
DXDraw.FIsoMap.SubAllState([tsSelected]);
p.x := X;
p.y := Y;
DxDraw.FIsoMap.UpdateArea(p.x-30,p.y-32,p.x+30,p.y+30);
Cell := DxDraw.FIsoMap.CellAt(p);
DxDraw.FIsoMap.AddState(cell.X,cell.Y,[tsSelected]);
end;
procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var cell : TCellsCoord;
begin
if (DxDraw=nil) then Exit;
cell := dxdraw.FIsoMap.CellAt(x,y);
DxDraw.FIsoMap.IsoMap[cell.X,cell.Y].ImageIndexes[0].ImageIndex := 0;
DxDraw.FIsoMap.IsoMap[cell.X,cell.Y].ImageIndexes[1].ImageIndex := 0;
DxDraw.FIsoMap.IsoMap[cell.X,cell.Y].ImageIndexes[2].ImageIndex := 0;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -