Guest User

Untitled

a guest
Oct 7th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. // new module header
  2.  
  3. //header================================================
  4. #define FAR_4_SPRITE 20
  5. #define CLEAR_SCREEN 25
  6. #define P_X playerChar.x
  7.  
  8. enum PlayerFaceDirection
  9. {
  10. eFaceUp = 0,
  11. eFaceRight,
  12. eFaceDown,
  13. eFaceLeft,
  14. };
  15.  
  16.  
  17. enum MazeType{
  18. eMazeWall,
  19. eMazeFree,
  20. eMazeChest,
  21. };
  22.  
  23. String map[10];
  24.  
  25. struct PlayerController
  26. {
  27. int x;
  28. int y;
  29.  
  30. PlayerFaceDirection faceDir;
  31. int rotation_x;
  32. int rotation_y;
  33.  
  34. import void UpdateRotation();
  35. import void Rotate(int direction);
  36. import void Move(int distance);
  37.  
  38. };
  39.  
  40. import function renderVisionCone();
  41. //==================================end header
  42.  
  43.  
  44.  
  45.  
  46. DynamicSprite* sprite;
  47. int dungeonCounter = 25;
  48. PlayerController playerChar;
  49.  
  50. function clearScreen() //starts a fresh canvas for drawing the walls on
  51. {
  52.  
  53. DynamicSprite.Create(620, 400);
  54. DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
  55. surface.DrawImage(0, 0, CLEAR_SCREEN);
  56. surface.Release();
  57. }
  58.  
  59. int GetCell(int x, int y) { //returns whether a cell is a wall or an open path
  60.  
  61.  
  62. map[0]="###### ###";
  63. map[1]="###### ###";
  64. map[2]="###### ###";
  65. map[3]=" ";
  66. map[4]="###### ###";
  67. map[5]="###### ###";
  68. map[6]="###### ###";
  69. map[7]="###### ###";
  70. map[8]="###### ###";
  71. map[9]="###### ###";
  72. // 0123456789
  73.  
  74.  
  75. // everything outside the maze is wall
  76. if (x < 0 || x >= map[0].Length) return eMazeWall;
  77. if (y < 0 || y >= 9) return eMazeWall;
  78. char c = map[y].Chars[x];
  79. if (c == '#') return eMazeWall;
  80. if (c == ' ') return eMazeFree;
  81. if (c == 'X') return eMazeChest;
  82. return eMazeWall;
  83. }
  84.  
  85. int wallSprites[15];
  86.  
  87. int[] rOffset(int x, int y, Direction dir) {
  88. int r[] = new int[2];
  89. if (dir == eFaceUp) { r[0] = x; r[1] = y; }
  90. if (dir == eFaceDown) { r[0] = -x; r[1] = -y; }
  91. if (dir == eFaceLeft) { r[0] = y; r[1] = -x; }
  92. if (dir == eFaceRight) { r[0] = -y; r[1] = x; }
  93. return r;
  94. }
  95.  
  96. function renderVisionCone() //draws the walls
  97. {
  98. clearScreen();
  99. DrawingSurface * dungeonWalls = Room.GetDrawingSurfaceForBackground();
  100.  
  101. // iterate over cone
  102. for (int y = -2; y <= 0; y++) {
  103. for (int x = -2; x <= 2; x++) {
  104. // rotate offset to find correct cell
  105. int ro[] = rOffset(x, y, playerChar.faceDir);
  106. MazeType mt = GetCell(playerChar.x + ro[0], playerChar.y + ro[1]);
  107. if (mt == eMazeWall) {
  108. int sprite2 = wallSprites[(y + 2) * 5 + (x + 2)];
  109. if (sprite2 > 0){ dungeonWalls.DrawImage(0, 0, sprite2);}
  110. }
  111. }
  112. }
  113. }
  114.  
  115. void PlayerController::UpdateRotation()
  116. {
  117. playerChar.rotation_x = 0;
  118. playerChar.rotation_y = 0;
  119. if(this.faceDir == eFaceDown) playerChar.rotation_y = 1;
  120. if(this.faceDir == eFaceUp) playerChar.rotation_y = -1;
  121. if(this.faceDir == eFaceLeft) playerChar.rotation_x = -1;
  122. if(this.faceDir == eFaceRight) playerChar.rotation_x = 1;
  123. }
  124.  
  125. void PlayerController::Rotate(int direction)
  126. {
  127. this.faceDir += direction;
  128. if(this.faceDir > 3) this.faceDir = eFaceUp;
  129. else if(this.faceDir < 0) this.faceDir = eFaceLeft;
  130. this.UpdateRotation();
  131. renderVisionCone();
  132. }
  133.  
  134.  
  135.  
  136. void PlayerController::Move(int distance)
  137. {
  138. map[0]="###### ###";
  139. map[1]="###### ###";
  140. map[2]="###### ###";
  141. map[3]=" ";
  142. map[4]="###### ###";
  143. map[5]="###### ###";
  144. map[6]="###### ###";
  145. map[7]="###### ###";
  146. map[8]="###### ###";
  147. map[9]="###### ###";
  148. // 0123456789
  149.  
  150. int x = this.x + distance * playerChar.rotation_x;
  151. int y = this.y + distance * playerChar.rotation_y;
  152. if((y < 0 || y > 9) || x < 0 || x > 10) return; //can't move off the map as defined above
  153.  
  154. char tile = map[y].Chars[x];
  155. if(tile != ' ') return;
  156.  
  157. //Move the player
  158. this.x += distance * playerChar.rotation_x;
  159. this.y += distance * playerChar.rotation_y;
  160. renderVisionCone();
  161. }
  162.  
  163.  
  164. function SetPlayerStart(int x, int y, PlayerFaceDirection faceDir)
  165. {
  166. playerChar.x = x;
  167. playerChar.y = y;
  168. playerChar.faceDir = faceDir;
  169. playerChar.UpdateRotation();
  170. //renderVisionCone(); no room loaded yet lol
  171. }
  172.  
  173.  
  174. int mapSize;
  175.  
  176.  
  177. function repeatedly_execute()
  178. {
  179.  
  180. if (playerChar.faceDir==eFaceUp){ labelDirection.Text="Up";}
  181. if (playerChar.faceDir==eFaceDown){ labelDirection.Text="Down";}
  182. if (playerChar.faceDir==eFaceRight){ labelDirection.Text="Right";}
  183. if (playerChar.faceDir==eFaceLeft){ labelDirection.Text="Left";}
  184.  
  185. labelXY.Text=String.Format("x:%d y:%d", playerChar.x, playerChar.y);
  186.  
  187. }
  188.  
  189. function on_key_press(eKeyCode keycode, int mod)
  190. {
  191. if(keycode==eKeyA){
  192. playerChar.Rotate(-1);
  193. }
  194.  
  195. else if (keycode==eKeyD){
  196. playerChar.Rotate(1);
  197. }
  198.  
  199. else if (keycode==eKeyW){
  200. playerChar.Move(1);
  201. }
  202.  
  203. else if (keycode==eKeyS){
  204. playerChar.Move(-1);
  205. }
  206.  
  207. else if (keycode==eKeyP){
  208.  
  209. MazeType dumbtest = GetCell(2, 2);
  210. if (dumbtest==eMazeFree){
  211. Display("Not wall!");
  212. }
  213. else if (dumbtest==eMazeWall){
  214. Display("Wall!");
  215. }
  216.  
  217. }
  218.  
  219. }
  220.  
  221. function game_start() {
  222. /* wall sprites, 3 rows and 5 columns (eFaceUp)
  223. 14 10 9 3 13
  224. 0 8 2 7 0
  225. 0 6 X 5 0
  226. */
  227. wallSprites[0] = 14; wallSprites[1] = 10; wallSprites[2] = 9; wallSprites[3] = 3; wallSprites[4] = 13;
  228. wallSprites[6] = 8; wallSprites[7] = 2; wallSprites[8] = 7; wallSprites[11] = 6; wallSprites[13] = 5;
  229. SetPlayerStart(3, 3, eFaceRight);
  230. }
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment