Page 4 of 5 FirstFirst ... 2345 LastLast
Results 31 to 40 of 42

Thread: Making a platform game - few questions

  1. #31

    Making a platform game - few questions

    unit '__pf_simplegame' (class tpf_simplegame is NOT USED ANY MORE)

    Code:
    unit __pf_simplegame;
    
    interface
    
    uses Windows,
         Messages,
         SysUtils,
         Variants,
         Classes,
         Graphics,
         Controls,
         Forms,
         Dialogs,
         DJXTimer,
         DJX,
         ExtCtrls,
         djxclasses,
         d3dx9,
         StdCtrls,
         DJXFonts,
         ComCtrls,
         djxlandscape,
         djxtextures,
         Direct3D9,
         djxrender,
         djxmeshes,
         DJXMaterials,
         DJXLights,
         __dxtexturemanager,
    
         udirectinput,
         directinput,
    
         __pf_simplemap,
         __pf_simpleplayer;
    
    const GUID_NULL: tguid= '{00000000-0000-0000-0000-000000000000}';
    
    type tpf_simplegamestate= (
                               pfs_playing,
                               pfs_quit 
                              );
    
    type tpf_corners= record
                        upperleft,
                        lowerleft,
                        upperright,
                        lowerright: boolean;
                      end;
    
    type tpf_simplegame= class
                           public
                             constructor create(handle: hwnd);
    
                             procedure render;
    
                             procedure process;
    
                             //map-funktionen
                             procedure load_textmap(const filename: string);
                             function map_gettilepos(const pos_x, pos_y: single): tpoint;
                             function map_getwalkable(const pos_x, pos_y: single): boolean;
    
                             //player-funktionen
                             procedure move_player(const dir_x, dir_y: integer);
    
                             procedure di_activate;
                             procedure di_deactivate;
                             procedure di_keydown;
                           private
                             fhandle: hwnd;  //handle des mutterfensters f?ēr di
    
                             fdjx: tdanjetx;
                             fsimplemap: tpf_simplemap;
                             fsimpleplayer: tpf_simpleplayer;
                             ftm: tdxtexturemanager;
    
                             fstretch_x,
                             fstretch_y: single;
    
                             fgamestate: tpf_simplegamestate;
    
                             //dinput
                             di_obj: tdiobject;
                             di_key: tkeyboardinput;
                             di_joy: TJoystickInput;
    
                             //eigene funktionen
                             procedure player_corners(const x, y: single; out corners: tpf_corners);
    
                             //properties
                             procedure setdjx(const value: tdanjetx);
                             procedure settm(const value: tdxtexturemanager);
                             procedure setstretch_x(const value: single);
                             procedure setstretch_y(const value: single);
                             function getplayerpos: tpoint;
                           published
                             property djx: tdanjetx read fdjx write setdjx;
                             property texturemanager: tdxtexturemanager read ftm write settm;
                             property stretch_x: single read fstretch_x write setstretch_x;
                             property stretch_y: single read fstretch_y write setstretch_y;
                             property state: tpf_simplegamestate read fgamestate;
                             property playerpos: tpoint read getplayerpos;
                         end;
    
    type tpf_simplegame_2= class
                             public
                               constructor create(handle: hwnd);
    
                               procedure render;
    
                               procedure process;
    
                               //map-funktionen
                               procedure load_textmap(const filename: string);
                               function map_gettilepos(const pos_x, pos_y: single): tpoint;
                               function map_getwalkable(const pos_x, pos_y: single): boolean;
    
                               //player-funktionen
                               procedure move_player(const dir_x, dir_y: integer);
    
                               procedure di_activate;
                               procedure di_deactivate;
                               procedure di_keydown;
                             private
                               fhandle: hwnd;  //handle des mutterfensters f?ēr di
    
                               fdjx: tdanjetx;
                               fsimplemap: tpf_simplemap;
                               fsimpleplayer: tpf_simpleplayer;
                               ftm: tdxtexturemanager;
    
                               fstretch_x,
                               fstretch_y: single;
    
                               fgamestate: tpf_simplegamestate;
    
                               //dinput
                               di_obj: tdiobject;
                               di_key: tkeyboardinput;
                               di_joy: TJoystickInput;
    
                               //corners
                               CnrUL : TPoint;  // upper left  tile (x,y) player is over 
                               CnrUR : TPoint;  // upper right tile (x,y) player is over
                               CnrLL : TPoint;  // lower left  tile (x,y) player is over
                               CnrLR : TPoint;  // lower right tile (x,y) player is over
    
                               vx,
                               vy: single;
    
                               //eigene funktionen
                               procedure player_corners(const x, y: single; out corners: tpf_corners);
    
                               //properties
                               procedure setdjx(const value: tdanjetx);
                               procedure settm(const value: tdxtexturemanager);
                               procedure setstretch_x(const value: single);
                               procedure setstretch_y(const value: single);
                               function getplayerpos: tpoint;
    
                               function ClampValue(const n, l, u : integer): integer;
                               function  Floor(X : extended): integer;
                               procedure GetCornersAt(const ax, ay : single);
                               procedure update(const ATimeSlice : single);
                               function  TileIsWalkable(const tx,ty : Integer) : boolean;
                             published
                               property djx: tdanjetx read fdjx write setdjx;
                               property texturemanager: tdxtexturemanager read ftm write settm;
                               property stretch_x: single read fstretch_x write setstretch_x;
                               property stretch_y: single read fstretch_y write setstretch_y;
                               property state: tpf_simplegamestate read fgamestate;
                               property playerpos: tpoint read getplayerpos;
                           end;
    
    implementation
    
    { tpf_simplegame }
    
    constructor tpf_simplegame.create(handle: hwnd);
    begin
      fhandle:= handle;
    
      fdjx:= nil;
    
      fsimplemap:= tpf_simplemap.create;
      fsimpleplayer:= tpf_simpleplayer.create;
    
      fsimplemap.nameintl:= 'blackblock_32px';
      fsimpleplayer.nameintl:= 'redblock_64px';
      {
      fsimpleplayer.x:= 5;
      fsimpleplayer.y:= 5;
      }
      fsimpleplayer.pos_x:= 250;
      fsimpleplayer.pos_y:= 250;
    
      fgamestate:= pfs_playing;
    end;
    
    procedure tpf_simplegame.di_activate;
    begin
      di_obj:= tdiobject.create;
      di_key:= tkeyboardinput.create;
      di_key.DIObject:= di_obj;
      di_key.handle:= fhandle;
      di_key.init(GUID_NULL);
      di_key.Acquire;  //jetzt gehts los
    end;
    
    procedure tpf_simplegame.di_deactivate;
    begin
      //freeandnil(di_key.DIData);
      freeandnil(di_key);
      freeandnil(di_joy);
      freeandnil(di_obj);
    end;
    
    procedure tpf_simplegame.di_keydown;
    var s: string;
        temp_dir_x,
        temp_dir_y: integer;
    begin
      //hier die controllerabfragen tun
      s:= '-';
      temp_dir_x:= 0;
      temp_dir_y:= 0;
    
      di_key.DIData.GetState;
      if tkeyboarddata(di_key.didata).keydown(DIK_LCONTROL) then begin
        s:= 'lcontrol';
      end;
      if tkeyboarddata(di_key.didata).keydown(DIK_RCONTROL) then begin
        s:= 'rcontrol';
      end;
      if tkeyboarddata(di_key.didata).keydown(DIK_SPACE) then begin
        s:= 'space';
      end;
      if tkeyboarddata(di_key.didata).keydown(DIK_ESCAPE) then begin
        s:= 'escape';
        fgamestate:= pfs_quit;
      end;
    
      if tkeyboarddata(di_key.didata).keydown(DIK_UPARROW) then begin
        s:= 'UP';
        temp_dir_y:= -1;
      end;
      if tkeyboarddata(di_key.didata).keydown(DIK_DOWNARROW) then begin
        s:= 'DOWN';
        temp_dir_y:= +1;
      end;
      if tkeyboarddata(di_key.didata).keydown(DIK_LEFTARROW) then begin
        s:= 'LEFT';
        temp_dir_x:= -1;
      end;
      if tkeyboarddata(di_key.didata).keydown(DIK_RIGHTARROW) then begin
        s:= 'RIGHT';
        temp_dir_x:= +1;
      end;
    
      move_player(temp_dir_x, temp_dir_y);
    end;
    
    function tpf_simplegame.getplayerpos: tpoint;
    begin
      result.X:= fsimpleplayer.x;
      result.Y:= fsimpleplayer.y;
    end;
    
    procedure tpf_simplegame.load_textmap(const filename: string);
    begin
      fsimplemap.loadfromtextfile(filename);
    end;
    
    function tpf_simplegame.map_gettilepos(const pos_x, pos_y: single): tpoint;
    var p: tpoint;
        temp_x,
        temp_y: integer;
    begin
      p.X:= -1;
      p.Y:= -1;
    
      //***
      temp_x:= round(pos_x/ fsimplemap.tile_width);
      temp_y:= round(pos_y/ fsimplemap.tile_height);
    
      temp_x:= trunc(pos_x/ fsimplemap.tile_width);
      temp_y:= trunc(pos_y/ fsimplemap.tile_height);
      //***
    
      if &#40;temp_x>= low&#40;fsimplemap.pattern&#91;0&#93;&#41;&#41;and &#40;temp_x<high>= low&#40;fsimplemap.pattern&#41;&#41;and &#40;temp_y<high> -1&#41;and &#40;p.Y> -1&#41; then begin
        result&#58;= fsimplemap.pattern&#91;p.y, p.x&#93;.walkable;
      end
      else begin
        result&#58;= false;
      end;
    end;
    
    procedure tpf_simplegame.player_corners&#40;const x, y&#58; single; out corners&#58; tpf_corners&#41;;
    var temp_left,
        temp_right,
        temp_up,
        temp_down&#58; single;
        outofmap&#58; boolean;
    begin
      temp_left&#58;= x- &#40;fsimpleplayer.width/ 2&#41;;
      temp_right&#58;= x+ &#40;fsimpleplayer.width/ 2&#41;;
      temp_up&#58;= y- &#40;fsimpleplayer.height/ 2&#41;;
      temp_down&#58;= y+ &#40;fsimpleplayer.height/ 2&#41;;
    
      corners.upperleft&#58;= map_getwalkable&#40;temp_left, temp_up&#41;;
      corners.upperright&#58;= map_getwalkable&#40;temp_right, temp_up&#41;;
      corners.lowerleft&#58;= map_getwalkable&#40;temp_left, temp_down&#41;;
      corners.lowerright&#58;= map_getwalkable&#40;temp_right, temp_down&#41;;
    end;
    
    procedure tpf_simplegame.move_player&#40;const dir_x, dir_y&#58; integer&#41;;
    var corners&#58; tpf_corners;
        temp_dir_x,
        temp_dir_y&#58; integer;
    begin
      player_corners&#40;fsimpleplayer.pos_x+ dir_x, fsimpleplayer.pos_y+ dir_x, corners&#41;;
    
      temp_dir_x&#58;= 0;
      temp_dir_y&#58;= 0;
    
      if dir_x= -1 then begin
        if corners.upperleft and corners.lowerleft then begin
          temp_dir_x&#58;= dir_x;
        end
        else begin
          temp_dir_x&#58;= 0;
        end;
      end;
    
      if dir_x= +1 then begin
        if corners.upperright and corners.lowerright then begin
          temp_dir_x&#58;= dir_x;
        end
        else begin
          temp_dir_x&#58;= 0;
        end;
      end;
    
      if dir_y= -1 then begin
        if corners.upperleft and corners.upperright then begin
          temp_dir_y&#58;= dir_y;
        end
        else begin
          temp_dir_y&#58;= 0;
        end;
      end;
    
      if dir_y= +1 then begin
        if corners.lowerleft and corners.lowerright then begin
          temp_dir_y&#58;= dir_y;
        end
        else begin
          temp_dir_y&#58;= 0;
        end;
      end;
    
      fsimpleplayer.pos_x&#58;= fsimpleplayer.pos_x+ temp_dir_x;
      fsimpleplayer.pos_y&#58;= fsimpleplayer.pos_y+ temp_dir_y;
    end;
    
    procedure tpf_simplegame.process;
    begin
      //per gettickcount abfragen, abstand ca 10 ms oder was
      //****
      di_keydown;
    end;
    
    procedure tpf_simplegame.render;
    begin
      fsimplemap.render;
      fsimpleplayer.render;
    end;
    
    procedure tpf_simplegame.setdjx&#40;const value&#58; tdanjetx&#41;;
    begin
      fdjx&#58;= value;
    
      fsimplemap.djx&#58;= fdjx;
      fsimpleplayer.djx&#58;= fdjx;
    end;
    
    procedure tpf_simplegame.setstretch_x&#40;const value&#58; single&#41;;
    begin
      fstretch_x&#58;= value;
    
      fsimplemap.stretch_x&#58;= fstretch_x;
      fsimpleplayer.stretch_x&#58;= fstretch_x;
    end;
    
    procedure tpf_simplegame.setstretch_y&#40;const value&#58; single&#41;;
    begin
      fstretch_y&#58;= value;
    
      fsimplemap.stretch_y&#58;= fstretch_y;
      fsimpleplayer.stretch_y&#58;= fstretch_y;
    end;
    
    procedure tpf_simplegame.settm&#40;const value&#58; tdxtexturemanager&#41;;
    begin
      ftm&#58;= value;
    
      fsimplemap.texturemanager&#58;= ftm;
      fsimpleplayer.texturemanager&#58;= ftm;
    end;
    
    
    &#123; tpf_simplegame_2 &#125;
    
    constructor tpf_simplegame_2.create&#40;handle&#58; hwnd&#41;;
    begin
      fhandle&#58;= handle;
    
      fdjx&#58;= nil;
    
      fsimplemap&#58;= tpf_simplemap.create;
      fsimpleplayer&#58;= tpf_simpleplayer.create;
    
      fsimplemap.nameintl&#58;= 'blackblock_32px';
      fsimpleplayer.nameintl&#58;= 'redblock_64px';
      &#123;
      fsimpleplayer.x&#58;= 5;
      fsimpleplayer.y&#58;= 5;
      &#125;
      fsimpleplayer.pos_x&#58;= 150;
      fsimpleplayer.pos_y&#58;= 250;
    
      vx&#58;= 0;
      vy&#58;= 0;
    
      fgamestate&#58;= pfs_playing;
    end;
    
    procedure tpf_simplegame_2.di_activate;
    begin
      di_obj&#58;= tdiobject.create;
      di_key&#58;= tkeyboardinput.create;
      di_key.DIObject&#58;= di_obj;
      di_key.handle&#58;= fhandle;
      di_key.init&#40;GUID_NULL&#41;;
      di_key.Acquire;  //jetzt gehts los
    end;
    
    procedure tpf_simplegame_2.di_deactivate;
    begin
      //freeandnil&#40;di_key.DIData&#41;;
      freeandnil&#40;di_key&#41;;
      freeandnil&#40;di_joy&#41;;
      freeandnil&#40;di_obj&#41;;
    end;
    
    procedure tpf_simplegame_2.di_keydown;
    var s&#58; string;
        temp_dir_x,
        temp_dir_y&#58; integer;
    begin
      //hier die controllerabfragen tun
      s&#58;= '-';
      temp_dir_x&#58;= 0;
      temp_dir_y&#58;= 0;
    
      di_key.DIData.GetState;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_LCONTROL&#41; then begin
        s&#58;= 'lcontrol';
      end;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_RCONTROL&#41; then begin
        s&#58;= 'rcontrol';
      end;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_SPACE&#41; then begin
        s&#58;= 'space';
      end;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_ESCAPE&#41; then begin
        s&#58;= 'escape';
        fgamestate&#58;= pfs_quit;
      end;
    
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_UPARROW&#41; then begin
        s&#58;= 'UP';
        temp_dir_y&#58;= -1;
      end;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_DOWNARROW&#41; then begin
        s&#58;= 'DOWN';
        temp_dir_y&#58;= +1;
      end;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_LEFTARROW&#41; then begin
        s&#58;= 'LEFT';
        temp_dir_x&#58;= -1;
      end;
      if tkeyboarddata&#40;di_key.didata&#41;.keydown&#40;DIK_RIGHTARROW&#41; then begin
        s&#58;= 'RIGHT';
        temp_dir_x&#58;= +1;
      end;
    
      //move_player&#40;temp_dir_x, temp_dir_y&#41;;
      vx&#58;= temp_dir_x;
      vy&#58;= temp_dir_y;
    end;
    
    function tpf_simplegame_2.getplayerpos&#58; tpoint;
    begin
      result.X&#58;= fsimpleplayer.x;
      result.Y&#58;= fsimpleplayer.y;
    end;
    
    procedure tpf_simplegame_2.load_textmap&#40;const filename&#58; string&#41;;
    begin
      fsimplemap.loadfromtextfile&#40;filename&#41;;
    end;
    
    function tpf_simplegame_2.map_gettilepos&#40;const pos_x, pos_y&#58; single&#41;&#58; tpoint;
    var p&#58; tpoint;
        temp_x,
        temp_y&#58; integer;
    begin
      p.X&#58;= -1;
      p.Y&#58;= -1;
    
      //***
      temp_x&#58;= round&#40;pos_x/ fsimplemap.tile_width&#41;;
      temp_y&#58;= round&#40;pos_y/ fsimplemap.tile_height&#41;;
    
      temp_x&#58;= trunc&#40;pos_x/ fsimplemap.tile_width&#41;;
      temp_y&#58;= trunc&#40;pos_y/ fsimplemap.tile_height&#41;;
      //***
    
      if &#40;temp_x>= low&#40;fsimplemap.pattern&#91;0&#93;&#41;&#41;and &#40;temp_x<high>= low&#40;fsimplemap.pattern&#41;&#41;and &#40;temp_y<high> -1&#41;and &#40;p.Y> -1&#41; then begin
        result&#58;= fsimplemap.pattern&#91;p.y, p.x&#93;.walkable;
      end
      else begin
        result&#58;= false;
      end;
    end;
    
    procedure tpf_simplegame_2.player_corners&#40;const x, y&#58; single; out corners&#58; tpf_corners&#41;;
    var temp_left,
        temp_right,
        temp_up,
        temp_down&#58; single;
        outofmap&#58; boolean;
    begin
      temp_left&#58;= x- &#40;fsimpleplayer.width/ 2&#41;;
      temp_right&#58;= x+ &#40;fsimpleplayer.width/ 2&#41;;
      temp_up&#58;= y- &#40;fsimpleplayer.height/ 2&#41;;
      temp_down&#58;= y+ &#40;fsimpleplayer.height/ 2&#41;;
    
      corners.upperleft&#58;= map_getwalkable&#40;temp_left, temp_up&#41;;
      corners.upperright&#58;= map_getwalkable&#40;temp_right, temp_up&#41;;
      corners.lowerleft&#58;= map_getwalkable&#40;temp_left, temp_down&#41;;
      corners.lowerright&#58;= map_getwalkable&#40;temp_right, temp_down&#41;;
    end;
    
    procedure tpf_simplegame_2.move_player&#40;const dir_x, dir_y&#58; integer&#41;;
    var corners&#58; tpf_corners;
        temp_dir_x,
        temp_dir_y&#58; integer;
    begin
      player_corners&#40;fsimpleplayer.pos_x+ dir_x, fsimpleplayer.pos_y+ dir_x, corners&#41;;
    
      temp_dir_x&#58;= 0;
      temp_dir_y&#58;= 0;
    
      if dir_x= -1 then begin
        if corners.upperleft and corners.lowerleft then begin
          temp_dir_x&#58;= dir_x;
        end
        else begin
          temp_dir_x&#58;= 0;
        end;
      end;
    
      if dir_x= +1 then begin
        if corners.upperright and corners.lowerright then begin
          temp_dir_x&#58;= dir_x;
        end
        else begin
          temp_dir_x&#58;= 0;
        end;
      end;
    
      if dir_y= -1 then begin
        if corners.upperleft and corners.upperright then begin
          temp_dir_y&#58;= dir_y;
        end
        else begin
          temp_dir_y&#58;= 0;
        end;
      end;
    
      if dir_y= +1 then begin
        if corners.lowerleft and corners.lowerright then begin
          temp_dir_y&#58;= dir_y;
        end
        else begin
          temp_dir_y&#58;= 0;
        end;
      end;
    
      fsimpleplayer.pos_x&#58;= fsimpleplayer.pos_x+ temp_dir_x;
      fsimpleplayer.pos_y&#58;= fsimpleplayer.pos_y+ temp_dir_y;
    end;
    
    procedure tpf_simplegame_2.process;
    begin
      //per gettickcount abfragen, abstand ca 10 ms oder was
      //****
      di_keydown;
    
      update&#40;1&#41;;
    end;
    
    procedure tpf_simplegame_2.render;
    begin
      fsimplemap.render;
      fsimpleplayer.render;
    end;
    
    procedure tpf_simplegame_2.setdjx&#40;const value&#58; tdanjetx&#41;;
    begin
      fdjx&#58;= value;
    
      fsimplemap.djx&#58;= fdjx;
      fsimpleplayer.djx&#58;= fdjx;
    end;
    
    procedure tpf_simplegame_2.setstretch_x&#40;const value&#58; single&#41;;
    begin
      fstretch_x&#58;= value;
    
      fsimplemap.stretch_x&#58;= fstretch_x;
      fsimpleplayer.stretch_x&#58;= fstretch_x;
    end;
    
    procedure tpf_simplegame_2.setstretch_y&#40;const value&#58; single&#41;;
    begin
      fstretch_y&#58;= value;
    
      fsimplemap.stretch_y&#58;= fstretch_y;
      fsimpleplayer.stretch_y&#58;= fstretch_y;
    end;
    
    procedure tpf_simplegame_2.settm&#40;const value&#58; tdxtexturemanager&#41;;
    begin
      ftm&#58;= value;
    
      fsimplemap.texturemanager&#58;= ftm;
      fsimpleplayer.texturemanager&#58;= ftm;
    end;
    
    function tpf_simplegame_2.TileIsWalkable&#40;const tx, ty&#58; Integer&#41;&#58; boolean;
    begin
      result&#58;= false;
      
      If tx < 0           Then Exit;
      If ty <0>= fsimplemap.width  Then Exit;
      If ty >= fsimplemap.height Then Exit;
    
      if &#40;tx> -1&#41;and &#40;ty> -1&#41; then begin
        result&#58;= fsimplemap.pattern&#91;ty, tx&#93;.walkable;
      end
      else begin
        result&#58;= false;
      end;
    end;
    
    function tpf_simplegame_2.ClampValue&#40;const n, l, u &#58; integer&#41;&#58; integer;
    begin
        Result &#58;= n;
        If Result <l> u Then Result &#58;= u; 
    end;
    
    function tpf_simplegame_2.Floor&#40;X&#58; extended&#41;&#58; integer;
    begin
      Result &#58;= Integer&#40;Trunc&#40;X&#41;&#41;; 
      If Frac&#40;X&#41; < 0 Then Dec&#40;Result&#41;;
    end;
    
    procedure tpf_simplegame_2.GetCornersAt&#40;const ax, ay&#58; single&#41;;
    &#123; 
               | 
               | 
         -,-   |   +,- 
    
        -------|------- 
    
         -,+   |   +,+ 
               | 
               | 
    &#125; 
    begin
        CnrUL.x &#58;= Floor&#40;&#40;ax - fsimpleplayer.width/2&#41; / fsimplemap.tile_width&#41;;
        CnrUL.y &#58;= Floor&#40;&#40;ay - fsimpleplayer.height/2&#41; / fsimplemap.tile_height&#41;;
    
        CnrUR.x &#58;= Floor&#40;&#40;ax + fsimpleplayer.width/2&#41; / fsimplemap.tile_width&#41;;
        CnrUR.y &#58;= Floor&#40;&#40;ay - fsimpleplayer.height/2&#41; / fsimplemap.tile_height&#41;;
    
        CnrLR.x &#58;= Floor&#40;&#40;ax + fsimpleplayer.width/2&#41; / fsimplemap.tile_width&#41;;
        CnrLR.y &#58;= Floor&#40;&#40;ay + fsimpleplayer.height/2&#41; / fsimplemap.tile_height&#41;;
    
        CnrLL.x &#58;= Floor&#40;&#40;ax - fsimpleplayer.width/2&#41; / fsimplemap.tile_width&#41;;
        CnrLL.y &#58;= Floor&#40;&#40;ay + fsimpleplayer.height/2&#41; / fsimplemap.tile_height&#41;;
    
        CnrUL.x &#58;= ClampValue&#40;CnrUL.x,0,fsimplemap.width - 1&#41;;
        CnrUR.x &#58;= ClampValue&#40;CnrUR.x,0,fsimplemap.width - 1&#41;;
        CnrLR.x &#58;= ClampValue&#40;CnrLR.x,0,fsimplemap.width - 1&#41;;
        CnrLL.x &#58;= ClampValue&#40;CnrLL.x,0,fsimplemap.width - 1&#41;;
    
        CnrUL.y &#58;= ClampValue&#40;CnrUL.y,0,fsimplemap.height - 1&#41;;
        CnrUR.y &#58;= ClampValue&#40;CnrUR.y,0,fsimplemap.height - 1&#41;;
        CnrLR.y &#58;= ClampValue&#40;CnrLR.y,0,fsimplemap.height - 1&#41;;
        CnrLL.y &#58;= ClampValue&#40;CnrLL.y,0,fsimplemap.height - 1&#41;; 
    end;
    
    procedure tpf_simplegame_2.update&#40;const ATimeSlice &#58; single&#41;;
    var TileIsWalkableUL &#58; Boolean; // upper left  player corner tile is walkable
        TileIsWalkableUR &#58; Boolean; // upper right player corner tile is walkable 
        TileIsWalkableLL &#58; Boolean; // lower left  player corner tile is walkable 
        TileIsWalkableLR &#58; Boolean; // lower right player corner tile is walkable 
    Begin 
        GetCornersAt&#40;fsimpleplayer.pos_x,fsimpleplayer.pos_y + vy * ATimeSlice&#41;;
        If vy <0> 0 Then 
        //moving down 
        Begin 
            TileIsWalkableLL &#58;= TileIsWalkable&#40;CnrLL.x,CnrLL.y&#41;;
            TileIsWalkableLR &#58;= TileIsWalkable&#40;CnrLR.x,CnrLR.y&#41;;
            If TileIsWalkableLL And TileIsWalkableLR Then 
                fsimpleplayer.pos_y &#58;= fsimpleplayer.pos_y + vy * ATimeSlice
            Else 
                // move player to sit on tile&#40;s&#41; below 
                fsimpleplayer.pos_y  &#58;= CnrLR.y * fsimplemap.tile_height - fsimpleplayer.height/2 - 0.01;
        End; 
    
        GetCornersAt&#40;fsimpleplayer.pos_x + vx * ATimeSlice,fsimpleplayer.pos_y&#41;;
        If vx <0> 0 Then 
        //moving right 
        Begin 
            TileIsWalkableUR &#58;= TileIsWalkable&#40;CnrUR.x,CnrUR.y&#41;;
            TileIsWalkableLR &#58;= TileIsWalkable&#40;CnrLR.x,CnrLR.y&#41;;
            If TileIsWalkableUR And TileIsWalkableLR Then 
                fsimpleplayer.pos_x &#58;= fsimpleplayer.pos_x + vx * ATimeSlice
            Else 
                // move player to touch tile&#40;s&#41; on right 
                fsimpleplayer.pos_x  &#58;= CnrUR.x * fsimplemap.tile_width - fsimpleplayer.width/2 - 0.01;
        End; 
        // add gravity to vy here and cap to max velocity so not faster than tile height; 
    End;
    
    end.

  2. #32

    Making a platform game - few questions

    and the _main unit:

    Code:
    unit _main;
    
    interface
    
    uses Windows,
         Messages,
         SysUtils,
         Variants,
         Classes,
         Graphics,
         Controls,
         Forms,
         Dialogs,
         DJXTimer,
         DJX,
         ExtCtrls,
         djxclasses,
         d3dx9,
         StdCtrls,
         DJXFonts,
         ComCtrls,
         djxlandscape,
         djxtextures,
         Direct3D9,
         djxrender,
         djxmeshes,
         DJXMaterials,
         DJXLights,
         Buttons,
         __dxtexturemanager,
         __pf_simplegame,
         __consts;
    
    type
      Tf_main = class&#40;TForm&#41;
        p_djx&#58; TPanel;
        djx_main&#58; TDanJetX;
        djxt_main&#58; TDJXTimer;
        djxfl_main&#58; TDJXFontList;
        Label1&#58; TLabel;
        procedure FormCreate&#40;Sender&#58; TObject&#41;;
        procedure djxt_mainTimer&#40;Sender&#58; TObject&#41;;
        procedure FormActivate&#40;Sender&#58; TObject&#41;;
        procedure FormDeactivate&#40;Sender&#58; TObject&#41;;
        procedure djxt_mainProcess&#40;Sender&#58; TObject&#41;;
      private
        programmpfad&#58; string;
        texturemanager&#58; tdxtexturemanager;
    
        simplegame&#58; tpf_simplegame_2;
    
        faktor_x,
        faktor_y&#58; single;
    
        procedure renderdebuginfo;
      public
        &#123; Public-Deklarationen &#125;
      end;
    
    var f_main&#58; Tf_main;
    
    implementation
    
    uses __basics;
    
    &#123;$R *.dfm&#125;
    
    procedure Tf_main.FormCreate&#40;Sender&#58; TObject&#41;;
    begin
      programmpfad&#58;= getprogrampath;
    
      //danjetx initialisieren
      djx_main.Width&#58;= p_djx.Width;
      djx_main.Height&#58;= p_djx.Height;
      djx_main.InitParams.VertexProcessing&#58;= vpmixed;
      djx_main.Start&#40;p_djx.Handle&#41;;
    
      if not djx_main.Initialized then exit;
    
      //fonts
      djxfl_main.CreateFont&#40;'courier_14', 'courier new', 14&#41;;
      djxfl_main.CreateFont&#40;'courier_16', 'courier new', 16&#41;;
    
      //seitenverh?§ltnis-faktoren ausrechnen
      faktor_x&#58;= djx_main.Width/ 640;
      faktor_y&#58;= djx_main.Height/ 480;
    
      //texturemanager
      texturemanager&#58;= tdxtexturemanager.create;
      texturemanager.djx&#58;= djx_main;  //ZUERST djx zuweisen
      texturemanager.path_textures&#58;= programmpfad+ 'textures\';  //DANN pfad einstellen
    
      simplegame&#58;= tpf_simplegame_2.create&#40;handle&#41;;
      simplegame.djx&#58;= djx_main;
      simplegame.texturemanager&#58;= texturemanager;
      simplegame.stretch_x&#58;= faktor_x;
      simplegame.stretch_y&#58;= faktor_y;
      simplegame.load_textmap&#40;programmpfad+ 'maps\map1.txt'&#41;;
    
      //und timer an
      djxt_main.enabled&#58;= true;
    end;
    
    procedure Tf_main.FormActivate&#40;Sender&#58; TObject&#41;;
    begin
      simplegame.di_activate;
    end;
    
    procedure Tf_main.FormDeactivate&#40;Sender&#58; TObject&#41;;
    begin
      simplegame.di_deactivate;
    end;
    
    procedure Tf_main.djxt_mainProcess&#40;Sender&#58; TObject&#41;;
    begin
      simplegame.process;
    
      if simplegame.state= pfs_quit then close;
    
      label1.caption&#58;= 'player pos '+ inttostr&#40;simplegame.playerpos.x&#41;+ '/'+ inttostr&#40;simplegame.playerpos.y&#41;;
    end;
    
    procedure Tf_main.djxt_mainTimer&#40;Sender&#58; TObject&#41;;
    begin
      djx_main.BeginRender;
      djx_main.ClearScreen&#40;255, 255, 255&#41;;
    
      //****
      simplegame.render;
    
      renderdebuginfo;
      djx_main.EndRender&#40;&#41;;
    end;
    
    procedure Tf_main.renderdebuginfo;
    begin
      djxfl_main.Find&#40;'courier_16'&#41;.Print&#40;10, 10, 'debug info', djxcolor&#40;cllime&#41;, true&#41;;
      djxfl_main.Find&#40;'courier_14'&#41;.Print&#40;10, 30, 'fps&#58; '+ inttostr&#40;djxt_main.fps&#41;, djxcolor&#40;cllime&#41;&#41;;
    end;
    
    end.

  3. #33

    Making a platform game - few questions

    oh and the map format is quite simple:

    Code:
    11111111111111111111
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000111000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000001
    10000000000000000011
    11111111111111111111


    where 1 is a block and 0 is nothing.

    this __texturemanager-stuff is only a texturelist-handling thing, you can replace it by - in this case - a texture for the player and one texture for a block.

    it's really basic right now, no gravity or different blocks implemented yet

    also, you can replace the whole directinput-stuff by polling the keys in the keydown-events... i just wanted that implemented from the start because it's so much work to do that later on...

  4. #34

    Making a platform game - few questions

    Quote Originally Posted by efilnukefesin
    hi folks!

    after reading that post i fell like i should start making a platformer

    well, i also got stuck after reading tonypa's tutorial while implementing the corners stuff. well, half stuck, my player still could move to the left ops:

    what i want to say is thanks paul nicholls, i implemented your code into mine and it's working fine! now i just have to find out why and clean up the mess i left so thanks a lot!

    different to paul, i have two classes tsimpleplayer and tsimplemap which are both controlled by a class named tsimplegame, where i do all this collision stuff.

    i think i'll post the source, just to copy and paste
    Hi efilnukefesin,
    that was an impressing amount of posts :-)

    @Ixy: Perhaps this site (top part) may help with regards to the corner checks I do for the 'sprite' and the tile grid.

    http://www.harveycartel.org/metanet/...tutorialB.html

    contents:
    SECTION 0: General Introduction
    SECTION 1: Basic Tile Grid
    SECTION 2: Advanced Tile Grid
    SECTION 3: Object Grid
    SECTION 4: Raycasting
    SECTION 5: Conclusion / Source Code


    There is also this tutorial page there as well:

    http://www.harveycartel.org/metanet/...tutorialA.html which explains axis separation and collision stuff too.

    contents:
    SECTION 0: General Introduction
    SECTION 1: Separating Axis Theorem
    SECTION 2: Separating Axis Theorem for AABBs
    SECTION 3: Separating Axis Theorem for Circles
    SECTION 4: Separating Axis Theorem for Points
    SECTION 5: Fast-Moving Objects
    SECTION 6: Conclusion / Source Code


    Unfortunately the source code is using Actionscript and I can't read the downloadable source code

    cheers,
    Paul

  5. #35

    Making a platform game - few questions

    @paul: well somehow you have to increase your number of posts perhaps i was a bit enthusiastic, too! but thanks anyway for showing the right direction!

  6. #36

    Making a platform game - few questions

    Quote Originally Posted by efilnukefesin
    @paul: well somehow you have to increase your number of posts perhaps i was a bit enthusiastic, too! but thanks anyway for showing the right direction!
    lol! No worries

    One day I may try and write a tutorial on this stuff ;-)

    cheers,
    Paul

  7. #37

    Making a platform game - few questions

    yeah that would be definitely great, maybe i could help?
    two people with few time give perhaps enough time to write a whole tutorial

  8. #38

    Making a platform game - few questions

    Quote Originally Posted by efilnukefesin
    yeah that would be definitely great, maybe i could help?
    two people with few time give perhaps enough time to write a whole tutorial
    Thanks for the offer, I will see how I go first
    cheers,
    Paul

  9. #39

    Making a platform game - few questions



    OMG nothing works at first. Everything needs tweaking.
    I can't play sound effects. Music plays nicely, but as soon as I try to play a sound effect, it crashes with exitcode 216. So I commented out MIX_VOLUMECHUNK. And now it returns -1 when loading a wav file.

    I'm sure that the path is correct...

  10. #40

    Making a platform game - few questions

    how do you load and play music/ sound effects?

Page 4 of 5 FirstFirst ... 2345 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •