SHARE
TWEET

CAVERUSH by HeroQbe #GPPCC6 (source code)

a guest Jan 3rd, 2016 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************************/
  2. /* GAME++ COMMUNITY CHALLENGE 6 */
  3. /*                              */
  4. /*           CAVERUSH           */
  5. /*             v0.1             */
  6. /*          by HeroQbe          */
  7. /********************************/
  8.  
  9.  
  10.  
  11. #include <tigcclib.h>
  12. #include "extgraph.h"
  13.  
  14. // debug
  15. //#define FAST_START
  16.  
  17.  
  18. typedef struct {
  19.     float X, Y;
  20. } Vector2;
  21.  
  22. // screen buffer
  23. LCD_BUFFER light, dark,
  24.            chunk_light, chunk_dark;
  25.  
  26. #define TILES 7
  27. #define ENTITY_TYPES 3
  28. #define DIRECTIONS 2
  29. #define FRAMES 2
  30.  
  31. enum Menu {NEW_GAME, LOAD_GAME, EXIT};
  32. enum Tiles {TILE_AIR, TILE_GRASS, TILE_STONE, TILE_STALACTIT, TILE_VINE, TILE_CEILING, TILE_LADDER};
  33. enum EntityTypes {ENTITY_NONE, ENTITY_PLAYER, ENTITY_SPIDER};
  34. enum Directions {DIR_LEFT, DIR_RIGHT};
  35. enum Planes {PLANE_LIGHT, PLANE_DARK, PLANE_MASK};
  36.  
  37. /***********************/
  38. /* SPRITE DECLERATIONS */
  39. /***********************/
  40. // button
  41. unsigned long button_off_light[] = {0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000};
  42. unsigned long button_off_dark[]  = {0x00000000,0x7FFFFFFE,0x7FFFFFFE,0x60000006,0x60000006,0x60000006,0x60000006,0x60000006,0x60000006,0x60000006,0x60000006,0x60000006,0x60000006,0x7FFFFFFE,0x7FFFFFFE,0x00000000};
  43. unsigned long button_on_light[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
  44. unsigned long button_on_dark[]  = {0xFFFFFFFF,0xFFFFFFFF,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xC0000003,0xFFFFFFFF,0xFFFFFFFF};
  45. // player
  46. unsigned short player_left0_light[] = {0x0FC0,0x1FF0,0x1FF8,0x0FF8,0x0FF0,0x0FF0,0x0FF0,0x07E0,0x0850,0x3AE8,0x3CF0,0x1820,0x0FE0,0x0920,0x1320,0x3DF0};
  47. unsigned short player_left0_dark[]  = {0x0FC0,0x1FF0,0x1838,0x0A98,0x0A90,0x0810,0x0810,0x07E0,0x0FF0,0x3FB8,0x2FF0,0x1FE0,0x0FE0,0x0FE0,0x1FE0,0x3DF0};
  48. unsigned short player_left0_mask[]  = {~0x0FC0,~0x1FF0,~0x1FF8,~0x0FF8,~0x0FF0,~0x0FF0,~0x0FF0,~0x07E0,~0x0FF0,~0x3FF8,~0x3FF0,~0x1FE0,~0x0FE0,~0x0FE0,~0x1FE0,~0x3DF0};
  49. unsigned short player_left1_light[] = {0x0FC0,0x1FF0,0x1FF8,0x0FF8,0x0FF0,0x0FF0,0x0FF0,0x07E0,0x1810,0x3A68,0x3CF0,0x0860,0x0FE0,0x0920,0x0640,0x0FC0};
  50. unsigned short player_left1_dark[]  = {0x0FC0,0x1FF0,0x1838,0x0A98,0x0A90,0x0810,0x0810,0x07E0,0x1FF0,0x2FF8,0x3FB0,0x0FE0,0x0FE0,0x0FE0,0x07C0,0x0FC0};
  51. unsigned short player_left1_mask[]  = {~0x0FC0,~0x1FF0,~0x1FF8,~0x0FF8,~0x0FF0,~0x0FF0,~0x0FF0,~0x07E0,~0x1FF0,~0x3FF8,~0x3FF0,~0x0FE0,~0x0FE0,~0x0FE0,~0x07C0,~0x0FC0};
  52. unsigned short player_right0_light[] = {0x03F0,0x0FF8,0x1FF8,0x1FF0,0x0FF0,0x0FF0,0x0FF0,0x07E0,0x0A10,0x175C,0x0F3C,0x0418,0x07F0,0x0490,0x04C8,0x0FBC};
  53. unsigned short player_right0_dark[]  = {0x03F0,0x0FF8,0x1C18,0x1950,0x0950,0x0810,0x0810,0x07E0,0x0FF0,0x1DFC,0x0FF4,0x07F8,0x07F0,0x07F0,0x07F8,0x0FBC};
  54. unsigned short player_right0_mask[]  = {~0x03F0,~0x0FF8,~0x1FF8,~0x1FF0,~0x0FF0,~0x0FF0,~0x0FF0,~0x07E0,~0x0FF0,~0x1FFC,~0x0FFC,~0x07F8,~0x07F0,~0x07F0,~0x07F8,~0x0FBC};
  55. unsigned short player_right1_light[] = {0x03F0,0x0FF8,0x1FF8,0x1FF0,0x0FF0,0x0FF0,0x0FF0,0x07E0,0x0818,0x165C,0x0F3C,0x0610,0x07F0,0x0490,0x0260,0x03F0};
  56. unsigned short player_right1_dark[]  = {0x03F0,0x0FF8,0x1C18,0x1950,0x0950,0x0810,0x0810,0x07E0,0x0FF8,0x1FF4,0x0DFC,0x07F0,0x07F0,0x07F0,0x03E0,0x03F0};
  57. unsigned short player_right1_mask[]  = {~0x03F0,~0x0FF8,~0x1FF8,~0x1FF0,~0x0FF0,~0x0FF0,~0x0FF0,~0x07E0,~0x0FF8,~0x1FFC,~0x0FFC,~0x07F0,~0x07F0,~0x07F0,~0x03E0,~0x03F0};
  58. // spider
  59. unsigned short spider_0_light[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1818,0x2424,0x43C2,0x1DB8,0x27E4,0x4DB2,0x53CA,0x9429,0xB66D};
  60. unsigned short spider_0_dark[]  = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1818,0x2424,0x4242,0x1BD8,0x27E4,0x4BD2,0x524A,0x9429,0x9429};
  61. unsigned short spider_0_mask[]  = {~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x1818,~0x2424,~0x43C2,~0x1FF8,~0x27E4,~0x4FF2,~0x53CA,~0x9429,~0xB66D};
  62. unsigned short spider_1_light[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x381C,0x4422,0x03C0,0x3DBC,0x47E2,0x9FF9,0xA425,0xFA5F};
  63. unsigned short spider_1_dark[]  = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x381C,0x4422,0x0240,0x3BDC,0x47E2,0x9BD9,0xA5A5,0xA815};
  64. unsigned short spider_1_mask[]  = {~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x0000,~0x381C,~0x4422,~0x03C0,~0x3FFC,~0x47E2,~0x9FF9,~0xA5A5,~0xFA5F};
  65. // tiles
  66. unsigned short tile1_light[] = {0x6BB7,0x9448,0x0001,0xD327,0xFF7F,0xFFFF,0xFFFF,0xFFFF,0xEA7F,0x8006,0x0000,0x0000,0x3E78,0xFFFF,0xFFFF,0xFFFF};
  67. unsigned short tile1_dark[]  = {0x6BB7,0xFFFF,0xFFFE,0x2CD8,0x0080,0x0000,0x0000,0x0000,0x1580,0x7FF9,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  68. unsigned short tile2_light[] = {0xFFFF,0xFFE3,0xFFC3,0xE1E7,0xC0FF,0xC03F,0xC03F,0xE07F,0xF0FF,0xFFE3,0xFFC0,0xF3C0,0xE1E0,0xE0F1,0xE0FF,0xF1FF};
  69. unsigned short tile2_dark[]  = {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  70. unsigned short tile3_light[] = {0x8FFA,0xEA2A,0x79AA,0x51AC,0x5104,0x6144,0x2140,0x20C0,0x20C0,0x2080,0x0080,0x0080,0x0080,0x0000,0x0000,0x0000};
  71. unsigned short tile3_dark[]  = {0xFFFE,0xDBEE,0x596E,0x716C,0x71C4,0x61C4,0x21C0,0x20C0,0x20C0,0x2080,0x0080,0x0080,0x0080,0x0000,0x0000,0x0000};
  72. unsigned short tile4_light[] = {0xFFFF,0x74DE,0x3090,0x0902,0x4442,0x2011,0x1710,0x0228,0x1101,0x3228,0x0049,0x1041,0x2610,0x0000,0x0364,0x544A};
  73. unsigned short tile4_dark[]  = {0xFFFF,0x75DA,0x6088,0x3088,0x108C,0x18C4,0x0846,0x0842,0x0C42,0x0482,0x0486,0x0D06,0x090A,0x088B,0x1411,0x2020};
  74. unsigned short tile5[]       = {0xFFFF,0x4CF6,0x0040,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
  75. unsigned short tile6_light[] = {0x1004,0x1184,0x5FF6,0x1004,0x3004,0x5704,0x5FF6,0x1004,0x3004,0x1386,0x5FF6,0x1004,0x3004,0x11C4,0x7FF6,0x3004};
  76. unsigned short tile6_dark[]  = {0x300C,0x7FFE,0x7FFE,0x300C,0x300C,0x7FFE,0x7FFE,0x300C,0x300C,0x7FFE,0x7FFE,0x300C,0x300C,0x7FFE,0x7FFE,0x300C};
  77.  
  78. struct entitySprt {
  79.     unsigned short *light, *dark, *mask;
  80. } EntitySprite[ENTITY_TYPES][DIRECTIONS][FRAMES] = {
  81.     {{{NULL,NULL,NULL},{NULL,NULL,NULL}},{{NULL,NULL,NULL},{NULL,NULL,NULL}}},
  82.     {{{player_left0_light,player_left0_dark,player_left0_mask},{player_left1_light,player_left1_dark,player_left1_mask}},{{player_right0_light,player_right0_dark,player_right0_mask},{player_right1_light,player_right1_dark,player_right1_mask}}},
  83.     {{{spider_0_light,spider_0_dark,spider_0_mask},{spider_1_light,spider_1_dark,spider_1_mask}},{{spider_0_light,spider_0_dark,spider_0_mask},{spider_1_light,spider_1_dark,spider_1_mask}}},
  84. };
  85.  
  86. struct tileSprt {
  87.     unsigned short *light, *dark;
  88. } TileSprite[TILES] = {
  89.     {NULL,NULL},
  90.     {tile1_light,tile1_dark},
  91.     {tile2_light,tile2_dark},
  92.     {tile3_light,tile3_dark},
  93.     {tile4_light,tile4_dark},
  94.     {tile5      ,tile5     },
  95.     {tile6_light,tile6_dark}
  96. };
  97.  
  98. // functions
  99. void waitKey(void) {
  100.     while (_rowread(0));
  101.     while (!_rowread(0));
  102. }
  103. void ClearBuffer(void) {
  104.     ClearGrayScreen2B(light, dark);
  105. }
  106. void FillBuffer(LCD_BUFFER *light_plane, LCD_BUFFER *dark_plane) {
  107.     FastCopyScreen(light_plane, light);
  108.     FastCopyScreen(dark_plane, dark);
  109. }
  110. void Draw(void) {
  111.     FastCopyScreen(light, GetPlane(LIGHT_PLANE));
  112.     FastCopyScreen(dark, GetPlane(DARK_PLANE));
  113. }
  114. void ClearDraw(void) {
  115.     ClearBuffer();
  116.     Draw();
  117. }
  118.  
  119. short TitleScreen(void);
  120. void newGame(void);
  121. void Game(void);
  122. void DrawMap(void);
  123.  
  124. static signed char savegame = 0;
  125.  
  126. void _main(void)
  127. {
  128.     INT_HANDLER save_int_1, save_int_5;
  129.     save_int_1 = GetIntVec(AUTO_INT_1);
  130.     save_int_5 = GetIntVec(AUTO_INT_5);
  131.     SetIntVec(AUTO_INT_1, DUMMY_HANDLER);
  132.     SetIntVec(AUTO_INT_5, DUMMY_HANDLER);
  133.    
  134.     if (!GrayOn())
  135.         return;
  136.    
  137.     short menu;
  138.     while (TRUE) {
  139.         #ifdef FAST_START
  140.             menu = NEW_GAME;
  141.         #else
  142.             menu = TitleScreen();
  143.         #endif
  144.         if (menu == NEW_GAME) {
  145.             newGame();
  146.             Game();
  147.         } else if (menu == LOAD_GAME) {
  148.             if (savegame)
  149.                 Game();
  150.             else
  151.                 GrayDrawStrExt2B(96, 100, "No game!", A_NORMAL, F_6x8, light, dark);
  152.         } else if (menu == EXIT)
  153.             break; // exit
  154.     }
  155.    
  156.     GrayOff();
  157.    
  158.     SetIntVec(AUTO_INT_1, save_int_1);
  159.     SetIntVec(AUTO_INT_5, save_int_5);
  160. }
  161.  
  162. short TitleScreen(void) {
  163.     static signed char selected = LOAD_GAME;
  164.     struct Buttons {
  165.         short textPosX;
  166.         const char * text;
  167.     } MenuButton[3] = {{110, "New"},
  168.                        {107, "Load"},
  169.                        {107, "Exit"}};
  170.     while (1) {
  171.         signed char i, y;
  172.         for (i = 0; i < 3; i++) {
  173.             y = 39+i*18;
  174.             if (i == selected)
  175.                 GraySprite32_OR(104, y, 16, button_on_light, button_on_dark, light, dark);
  176.             else
  177.                 GraySprite32_OR(104, y, 16, button_off_light, button_off_dark, light, dark);
  178.             GrayDrawStrExt2B(MenuButton[i].textPosX, y+5, MenuButton[i].text, A_NORMAL, F_6x8, light, dark);
  179.         }
  180.         GrayDrawStrExt2B(0, 120, "CAVERUSH v0.1 - by HeroQbe", A_NORMAL, F_6x8, light, dark);
  181.        
  182.         Draw();
  183.         ClearBuffer();
  184.        
  185.         waitKey();
  186.         if (_keytest(RR_UP) && selected > 0)
  187.             selected--;
  188.         else if (_keytest(RR_DOWN) && selected < 2)
  189.             selected++;
  190.         else if (_keytest(RR_ENTER) || _keytest(RR_ENTER2))
  191.             return selected;
  192.     }
  193.     return -1;
  194. }
  195.  
  196. #define MIN_POSX 1
  197. #define MAX_POSX 223
  198. #define MIN_POSY 1
  199. #define MAX_POSY 111
  200.  
  201. #define CHUNK_WIDTH 15
  202. #define CHUNK_HEIGHT 8
  203. #define WORLD_WIDTH 8
  204. #define WORLD_HEIGHT 8
  205. #define MAX_ENTITIES 4
  206.  
  207. #define HURT_TIME 20
  208.  
  209. static const float velocity[ENTITY_TYPES]  = {0, 3.0, 1.0},
  210.                    jumpBoost[ENTITY_TYPES] = {0, 6.0, 8.0},
  211.                    climbingSpeed = 2.0,
  212.                    gravity = 0.5;
  213.  
  214. typedef struct {
  215.     unsigned char Type, inAir, frame, direction;
  216.     signed char Health, maxHealth, Defense, Attack, HurtTime;
  217.     Vector2 Pos, Mot;
  218. } Entity;
  219.  
  220. static Entity player;
  221. static struct {
  222.     unsigned char Tile[CHUNK_HEIGHT][CHUNK_WIDTH];
  223.     Entity entity[MAX_ENTITIES];
  224. } Chunk[WORLD_HEIGHT][WORLD_WIDTH];
  225. unsigned char SolidTile[CHUNK_HEIGHT][CHUNK_WIDTH];
  226. static unsigned char chunkX, chunkY;
  227.  
  228. static signed char attack;
  229.  
  230. void EntityLogic(Entity * entity) {
  231.     if (SolidTile[(short)(entity->Pos.Y-1)/16][(short)(entity->Pos.X+8)/16]) {
  232.         entity->Mot.Y = 0;
  233.         entity->inAir = TRUE;
  234.     }
  235.    
  236.     if (entity->inAir)
  237.         entity->Mot.Y += gravity;
  238.     if (entity->Mot.Y > 0 && SolidTile[(short)(entity->Pos.Y+16)/16][(short)(entity->Pos.X+8)/16]) {
  239.         entity->inAir = FALSE;
  240.         entity->Mot.X = entity->Mot.Y = 0;
  241.         entity->Pos.Y = (short)(entity->Pos.Y/16)*16;
  242.     } else {
  243.         entity->inAir = TRUE;
  244.     }
  245.    
  246.     entity->Pos.X += entity->Mot.X;
  247.     entity->Pos.Y += entity->Mot.Y;
  248. }
  249. void EntityDraw(Entity * entity) {
  250.     static struct entitySprt es;
  251.     if (entity->Type) {
  252.         es = EntitySprite[entity->Type][entity->direction][entity->frame];
  253.         GraySprite16_MASK(entity->Pos.X, entity->Pos.Y, 16, es.light, es.dark, es.mask, es.mask, light, dark);
  254.         if (entity->HurtTime) {
  255.             if (entity->HurtTime%2)
  256.                 GraySprite16_XOR(entity->Pos.X, entity->Pos.Y, 16, es.dark, es.light, light, dark);
  257.             entity->HurtTime--;
  258.         }
  259.     }
  260. }
  261.  
  262. void nextFrame(Entity * entity) {
  263.     entity->frame++;
  264.     if (entity->frame == FRAMES)
  265.         entity->frame = 0;
  266. }
  267.  
  268. short getTile(signed char chunkX, signed char chunkY, signed char tileX, signed char tileY) {
  269.     return Chunk[chunkY][chunkX].Tile[tileY][tileX];
  270. }
  271. void setTile(signed char chunkX, signed char chunkY, signed char tileX, signed char tileY, unsigned char tile) {
  272.     Chunk[chunkY][chunkX].Tile[tileY][tileX] = tile;
  273. }
  274. Entity * getEntity(signed char chunkX, signed char chunkY, unsigned char entity) {
  275.     return &Chunk[chunkX][chunkY].entity[entity];
  276. }
  277. void entityJump(Entity * entity) {
  278.     entity->Mot.Y = -jumpBoost[entity->Type];
  279.     entity->inAir = TRUE;
  280. }
  281. short doesCollide(Entity * entity, signed char negativeX, signed char positiveX, signed char negativeY, signed char positiveY) {
  282.     return entity->Pos.X-player.Pos.X > -negativeX && entity->Pos.X-player.Pos.X < positiveX && entity->Pos.Y-player.Pos.Y > -negativeY && entity->Pos.Y-player.Pos.Y < positiveY;
  283. }
  284. void entityHit(Entity * entity1, Entity * entity2) {
  285.     entity2->Health -= entity1->Attack;
  286.     entity2->HurtTime = HURT_TIME;
  287.     entity2->Mot.X = (entity2->Pos.X-entity1->Pos.X)/2;
  288.     entity2->Mot.Y = (entity2->Pos.Y-2-entity1->Pos.Y)/2;
  289. }
  290.  
  291. void newChunk(signed char new_chunkX, signed char new_chunkY) {
  292.     struct tileSprt ts;
  293.     if (new_chunkX == -1 || new_chunkX == 8 || new_chunkY == -1 || new_chunkY == 8)
  294.         return;
  295.     chunkX = new_chunkX;
  296.     chunkY = new_chunkY;
  297.     GrayClearScreen2B(chunk_light, chunk_dark);
  298.     unsigned char tileX, tileY, tile;
  299.     for (tileY = 0; tileY < CHUNK_HEIGHT; tileY++) {
  300.         for (tileX = 0; tileX < CHUNK_WIDTH; tileX++) {
  301.             tile = getTile(chunkX, chunkY, tileX, tileY);
  302.             if (tile != TILE_AIR) {
  303.                 ts = TileSprite[tile];
  304.                 GraySprite16_OR(tileX*16, tileY*16, 16, ts.light, ts.dark, chunk_light, chunk_dark); // render screen
  305.             }
  306.            
  307.             if (tile == TILE_GRASS || tile == TILE_STONE) // create solid tilemap
  308.                 SolidTile[tileY][tileX] = TRUE;
  309.             else
  310.                 SolidTile[tileY][tileX] = FALSE;
  311.         }
  312.     }
  313. }
  314.  
  315. void newGame(void) {
  316.     /*******************/
  317.     /* WORLD GENERATOR */
  318.     /*******************/
  319.     // prototype
  320.     short i;
  321.     for (i = 0; i < CHUNK_WIDTH; i++) {
  322.         setTile(0,0,i,0,TILE_STONE);
  323.     }
  324.     for (i = 0; i < CHUNK_WIDTH; i++) {
  325.         setTile(0,0,i,1,TILE_CEILING);
  326.     }
  327.     for (i = 0; i < CHUNK_WIDTH; i++) {
  328.         setTile(0,0,i,7,TILE_STONE);
  329.     }
  330.     setTile(0,0,7,6,TILE_STONE);
  331.     setTile(0,0,7,5,TILE_GRASS);
  332.     setTile(0,0,1,1,TILE_VINE);
  333.     for (i = 8; i < CHUNK_WIDTH; i++) {
  334.         setTile(0,0,i,6,TILE_GRASS);
  335.     }
  336.     for (i = 2; i < 7; i++) {
  337.         setTile(0,0,3,i,TILE_LADDER);
  338.     }
  339.     setTile(0,0,0,3,TILE_GRASS);
  340.     setTile(0,0,1,3,TILE_GRASS);
  341.     setTile(0,0,2,3,TILE_GRASS);
  342.    
  343.    
  344.     getEntity(0,0,0)->Type = ENTITY_SPIDER;
  345.     getEntity(0,0,0)->Pos.X = 16*1;
  346.     getEntity(0,0,0)->Pos.Y = 16*2;
  347.     getEntity(0,0,0)->Attack = 2;
  348.    
  349.     getEntity(0,0,1)->Type = ENTITY_SPIDER;
  350.     getEntity(0,0,1)->Pos.X = 16*8;
  351.     getEntity(0,0,1)->Pos.Y = 16*2;
  352.     getEntity(0,0,1)->Attack = 2;
  353.    
  354.     getEntity(0,0,2)->Type = ENTITY_SPIDER;
  355.     getEntity(0,0,2)->Pos.X = 16*12;
  356.     getEntity(0,0,2)->Pos.Y = 16*2;
  357.     getEntity(0,0,2)->Attack = 2;
  358.    
  359.    
  360.     player.Type = ENTITY_PLAYER;
  361.     player.Pos.X = 16*4;
  362.     player.Pos.Y = 16*4;
  363.     player.maxHealth = 10;
  364.     player.Health = player.maxHealth;
  365.    
  366.     chunkX = 0;
  367.     chunkY = 0;
  368.    
  369.     savegame = 1;
  370. }
  371.  
  372. void Game(void) {
  373.     Entity * eptr;
  374.    
  375.     short en;
  376.     newChunk(chunkX, chunkY);
  377.     while (!_keytest(RR_ESC)) {
  378.         BEGIN_KEYTEST
  379.         if (_keytest_optimized(RR_RIGHT) && !SolidTile[(short)(player.Pos.Y+15)/16][(short)(player.Pos.X+12)/16] && !SolidTile[(short)(player.Pos.Y-1)/16][(short)(player.Pos.X+12)/16]) { // move right
  380.             player.Pos.X += velocity[ENTITY_PLAYER];
  381.             if (!_keytest_optimized(RR_SHIFT)) // strafe
  382.                 player.direction = DIR_RIGHT;
  383.             nextFrame(&player);
  384.         } else if (_keytest_optimized(RR_LEFT) && !SolidTile[(short)(player.Pos.Y+15)/16][(short)(player.Pos.X+4)/16] && !SolidTile[(short)(player.Pos.Y-1)/16][(short)(player.Pos.X+4)/16]) { // move left
  385.             player.Pos.X -= velocity[ENTITY_PLAYER];
  386.             if (!_keytest_optimized(RR_SHIFT)) // strafe
  387.                 player.direction = DIR_LEFT;
  388.             nextFrame(&player);
  389.         } if (_keytest_optimized(RR_UP) && !player.inAir) { // jump
  390.             entityJump(&player);
  391.         } if (_keytest_optimized(RR_HAND) && attack) { // attack
  392.             for (en = 0; en < MAX_ENTITIES; en++) {
  393.                 eptr = &Chunk[chunkY][chunkX].entity[en];
  394.                 if (player.direction == DIR_LEFT && doesCollide(eptr, 20, 4, 8, 8))
  395.                     entityHit(&player, eptr);
  396.                 else if (player.direction == DIR_RIGHT && doesCollide(eptr, 4, 20, 8, 8))
  397.                     entityHit(&player, eptr);
  398.             }
  399.             attack = FALSE;
  400.         } if (!_keytest_optimized(RR_HAND)) {
  401.             attack = TRUE;
  402.         } if (_keytest_optimized(RR_2ND)) { // special
  403.            
  404.         }
  405.        
  406.         if (getTile(chunkX, chunkY, (short)(player.Pos.X+8)/16, (short)(player.Pos.Y+15)/16) != TILE_LADDER)
  407.             EntityLogic(&player);
  408.         else {
  409.             if (_keytest_optimized(RR_UP))
  410.                 player.Pos.Y -= climbingSpeed;
  411.             if (_keytest_optimized(RR_DOWN))
  412.                 player.Pos.Y += climbingSpeed;
  413.         }
  414.         END_KEYTEST
  415.        
  416.         for (en = 0; en < MAX_ENTITIES; en++) {
  417.             eptr = &Chunk[chunkY][chunkX].entity[en];
  418.             if (eptr->Type) {
  419.                 if (eptr->Pos.X < player.Pos.X) {
  420.                     if (!SolidTile[(short)(eptr->Pos.Y+15)/16][(short)(eptr->Pos.X+12)/16]) {
  421.                         eptr->Pos.X += velocity[eptr->Type];
  422.                         nextFrame(eptr);
  423.                     } else if (!eptr->inAir)
  424.                         entityJump(eptr);
  425.                 } else {
  426.                     if (!SolidTile[(short)(eptr->Pos.Y+15)/16][(short)(eptr->Pos.X+4)/16]) {
  427.                         eptr->Pos.X -= velocity[eptr->Type];
  428.                         nextFrame(eptr);
  429.                     } else if (!eptr->inAir)
  430.                         entityJump(eptr);
  431.                 }
  432.             }
  433.             EntityLogic(eptr);
  434.         }
  435.        
  436.        
  437.         // entity collision detection
  438.         for (en = 0; en < MAX_ENTITIES; en++) {
  439.             eptr = &Chunk[chunkY][chunkX].entity[en];
  440.             if (eptr->Type && !player.HurtTime && doesCollide(eptr, 8, 8, 8, 8))
  441.                 entityHit(eptr, &player);
  442.         }
  443.        
  444.         // new chunk detection
  445.         if ((short)player.Pos.X < MIN_POSX) {
  446.             newChunk(chunkX-1, chunkY);
  447.             player.Pos.X = MAX_POSX;
  448.         } if ((short)player.Pos.X > MAX_POSX) {
  449.             newChunk(chunkX+1, chunkY);
  450.             player.Pos.X = MIN_POSX;
  451.         } if ((short)player.Pos.Y < MIN_POSY) {
  452.             newChunk(chunkX, chunkY-1);
  453.             player.Pos.Y = MAX_POSY;
  454.         } if ((short)player.Pos.Y > MAX_POSY) {
  455.             newChunk(chunkX, chunkY+1);
  456.             player.Pos.Y = MIN_POSY;
  457.         }
  458.        
  459.         for (en = 0; en < MAX_ENTITIES; en++) {
  460.             eptr = &Chunk[chunkY][chunkX].entity[en];
  461.             if ((short)eptr->Pos.X < MIN_POSX)
  462.                 eptr->Pos.X = MIN_POSX;
  463.             if ((short)eptr->Pos.X > MAX_POSX)
  464.                 eptr->Pos.X = MAX_POSX;
  465.             if ((short)eptr->Pos.Y < MIN_POSY)
  466.                 eptr->Pos.Y = MIN_POSY;
  467.             if ((short)eptr->Pos.Y > MAX_POSY)
  468.                 eptr->Pos.Y = MAX_POSY;
  469.         }  
  470.        
  471.        
  472.         // copy rendered chunk to buffer
  473.         FastCopyScreen(chunk_light, light);
  474.         FastCopyScreen(chunk_dark, dark);
  475.         // draw to buffer
  476.         for (en = 0; en < MAX_ENTITIES; en++) {
  477.             EntityDraw(&Chunk[chunkY][chunkX].entity[en]);
  478.         }
  479.         EntityDraw(&player);
  480.         // draw gui
  481.        
  482.         Draw();
  483.         ClearBuffer();
  484.     }
  485. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top