Advertisement
ddto

Untitled

Apr 18th, 2024 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.    Define your custom player character class basing on already defined Game.Chr
  4.    Game.Chr is a class which handles the most basic character methods
  5.    including : movement, animations, physics and actions
  6.  
  7. /******************************************************************************/
  8. MeshPtr mp;
  9.  
  10. class Player : Game.Chr // extend character class by defining a player class based on the character
  11. {
  12.    virtual Bool update() // here we'll update the player (please note that this is a virtual method)
  13.    {
  14.       // here we update character input according to mouse and keyboard
  15.       // before we set the input, we need to check if the character isn't controlled by an automatic action
  16.       if(action)
  17.       {
  18.          // if it's controlled by an action we leave the input with no changes,
  19.          // however we can optionally break that action, by pressing for example movement keys
  20.          if(Kb.b(KB_W) || Kb.b(KB_S) || Kb.b(KB_A) || Kb.b(KB_D) || Kb.b(KB_Q) || Kb.b(KB_E))actionBreak();
  21.       }
  22.  
  23.       if(!action) // if the character isn't controlled by an automatic action, we can set the input
  24.       {
  25.          // turn & move
  26.          input.turn.x=Kb.b(KB_Q)-Kb.b(KB_E);
  27.          input.turn.y=Kb.b(KB_T)-Kb.b(KB_G);
  28.          input.move.x=Kb.b(KB_D)-Kb.b(KB_A);
  29.          input.move.z=Kb.b(KB_W)-Kb.b(KB_S);
  30.          input.move.y=Kb.b(KB_SPACE)-Kb.b(KB_LSHIFT);
  31.  
  32.          // dodge, crouch, walk, jump
  33.          input.dodge = Kb.bd(KB_D)-Kb.bd(KB_A);
  34.          input.crouch= Kb.b (KB_LSHIFT);
  35.          input.walk  = Kb.b (KB_LCTRL );
  36.          input.jump  =(Kb.bp(KB_SPACE) ? 3.5 : 0);
  37.  
  38.          // mouse turn
  39.          
  40.          Flt max=DegToRad(900)*Time.d();
  41.          angle.x-=Mid(Ms.d().x*9.7, -max, max);
  42.          angle.y+=Mid(Ms.d().y*1.7, -max, max);
  43.       }
  44.  
  45.       return super.update(); // call Game.Chr.update on which Player is based on
  46.    }
  47. }
  48. /******************************************************************************/
  49. Actor  ground; // ground actor
  50. Player player; // player
  51. /******************************************************************************/
  52. void InitPre()
  53. {
  54.    INIT();
  55.    Ms.hide();
  56.    Ms.clip(null, 1);
  57.    Cam.dist=2;
  58. }
  59. /******************************************************************************/
  60. bool Init()
  61. {
  62.    Physics.create();
  63.    ground .create(Box(15, 1, 15, Vec(0, -2, 0)), 0);
  64.  
  65.    player.create(*ObjectPtr(UID(2919624831, 1261075521, 753053852, 3651670215))); // create player from object parameters
  66.    
  67.    mp=ObjectPtr(UID(1134879343, 1157937325, 4232119955, 3140023117))->mesh();
  68.    
  69.    return true;
  70. }
  71. /******************************************************************************/
  72. void Shut()
  73. {
  74. }
  75. /******************************************************************************/
  76. bool Update()
  77. {
  78.    if(Kb.bp(KB_ESC))return false;
  79.  
  80.    Physics.startSimulation().stopSimulation();
  81.  
  82.    player.update(); // update player
  83.  
  84.    Cam.updateBegin().setSpherical(player.ctrl.actor.pos()+Vec(0, 1, 0), player.angle.x, player.angle.y, 0, Cam.dist*ScaleFactor(Ms.wheel()*-0.2)).updateEnd().set(); // update camera according to player angles and mouse wheel
  85.  
  86.    return true;
  87. }
  88. /******************************************************************************/
  89. void Render()
  90. {
  91.    switch(Renderer())
  92.    {
  93.       case RM_PREPARE:
  94.       {
  95.          player.drawPrepare();
  96.  
  97.          LightDir(!(Cam.matrix.x-Cam.matrix.y+Cam.matrix.z)).add();
  98.       }break;
  99.    }
  100. }
  101. void Render2()
  102. {
  103.    switch(Renderer())
  104.    {
  105.       case RM_PREPARE:
  106.       {
  107.          mp->draw(MatrixIdentity+Vec(0, -1, -2));
  108.          LightPoint(5, Vec(0, 1, 1)).add();
  109.          
  110.       }break;
  111.    }
  112. }
  113. void Draw()
  114. {
  115.    Renderer(Render);
  116.    ground.draw(); // draw ground actor
  117.    Renderer.combine=true;
  118.    Renderer(Render2);
  119.    Renderer.combine=false;
  120.    
  121.    Renderer.setDepthForDebugDrawing();
  122. }
  123. /******************************************************************************/
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement