// // Sliding laser. // The idea here is to have a player slide a laser below a enemy or enemy weapon. When the laser // stops sliding a laser is shot upwards and it should destroy the threat. // // Going on a break.. Maybe I will expand and make it more playable later. // // #include "raylib.h" #include #define MAX_SLIDEBOMBS 10 #define MAX_CEILTURRETS 10 #define MAX_BULLETS 64 #define MAX_EFFECT 1000 int myMap[10][11] = { {1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,0,0,0,0,1,1,1,1}, {1,1,1,0,0,0,0,1,0,0,1}, {1,1,1,0,0,0,0,1,0,0,1}, {1,1,1,0,0,0,0,1,0,0,1}, {1,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1} }; int mapWidth = 11; int mapHeight = 10; float tileWidth; float tileHeight; typedef struct player{ bool active; Vector2 position; int direction; // -1 left, 2 - right int w; int h; int numslidelasers; }player; static player myplayer = {0}; static struct effect2{ bool active; Vector2 position; int startdelay; int radius; int countdown; }effect2; static struct effect2 arr_effect2[MAX_EFFECT]; static struct effect{ bool active; Vector2 position; Vector2 inc; Vector2 incmod; int w; int h; int countdown; }effect; static struct effect arr_effect[MAX_EFFECT]; // design. // shoot x amount of rays into any direction to see if player is there. // if player is there than the bullet is shot. static struct bullet{ bool active; Vector2 position; Vector2 inc; int radius; }bullet; static struct bullet arr_bullet[MAX_BULLETS]; static struct ceilturret{ bool active; Vector2 position; int w; int h; int shootdelay; }aiceilturret; static struct ceilturret arr_ceilturret[MAX_CEILTURRETS]; static struct slidelaser{ bool active; int state; Vector2 position; Vector2 inc; Vector2 incdec; int ceilingloc; int w; int h; }slidelaser; static struct slidelaser arr_slidelaser[MAX_SLIDEBOMBS]; //Unit collide with solid blocks true/false bool recttilecollide(int x, int y, int w, int h, int offsetx,int offsety); // Our rectsoverlap function. Returns true/false. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2); void createeffect(int x, int y); int screenWidth; int screenHeight; int main(void) { // Initialization //-------------------------------------------------------------------------------------- screenWidth = 800; screenHeight = 600; tileWidth = ceil((float)(float)screenWidth/(float)mapWidth); tileHeight = ceil((float)screenHeight/(float)mapHeight); InitWindow(screenWidth, screenHeight, "raylib example."); SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- arr_slidelaser[0].active = false; arr_slidelaser[0].state = 0; arr_slidelaser[0].position = (Vector2){160,530}; arr_slidelaser[0].w = 16; arr_slidelaser[0].h = 10; arr_slidelaser[0].inc.x = 3; arr_slidelaser[0].incdec.x = 0.02; myplayer.active = true; myplayer.w = 16; myplayer.h = 24; myplayer.position = (Vector2){100,516}; myplayer.direction = 2; arr_ceilturret[0].active = true; arr_ceilturret[0].position.x=tileWidth*5; arr_ceilturret[0].position.y=tileHeight*4; arr_ceilturret[0].w = tileWidth/2; arr_ceilturret[0].h = tileHeight/2; // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- // update the effect 2 for(int i=0;i0)continue; int touch;//if collided with map count for(int a=0;a<10;a++){ float angle=(float)GetRandomValue(0,6200)/100; Vector2 position = arr_ceilturret[i].position; for(int b=0;b0){ arr_slidelaser[i].inc.x-=arr_slidelaser[i].incdec.x; }else{ arr_slidelaser[i].inc.x+=arr_slidelaser[i].incdec.x; } if((arr_slidelaser[i].inc.x>-0.2 && arr_slidelaser[i].inc.x<0.2) || recttilecollide(arr_slidelaser[i].position.x,arr_slidelaser[i].position.y,arr_slidelaser[i].w,arr_slidelaser[i].h,0,0)){ arr_slidelaser[i].state = 1; arr_slidelaser[i].inc.x = 0; // find ceiling(top of the laser beam;) int ceilingloc; for(int y=arr_slidelaser[i].position.y;y>0;y--){ if(recttilecollide(arr_slidelaser[i].position.x+4,y,1,1,0,0)){ ceilingloc=y+1; break; } } arr_slidelaser[i].ceilingloc = ceilingloc; } arr_slidelaser[i].position.x+=arr_slidelaser[i].inc.x; } // If the laser cuts into the first! ceiling turret then remove it. if(arr_slidelaser[i].state==1){ if(arr_slidelaser[i].active==true){ if(rectsoverlap(arr_slidelaser[i].position.x,0,arr_slidelaser[i].w,10,arr_ceilturret[0].position.x-arr_ceilturret[0].w,0,arr_ceilturret[0].w*2,10)){ if(arr_ceilturret[0].active)createeffect(arr_ceilturret[i].position.x,arr_ceilturret[i].position.y+tileHeight/4); arr_ceilturret[0].active=false; } } } } // Update the player.. if(myplayer.active==true){ Vector2 oldpos = myplayer.position; int fast=1; if(IsKeyDown(KEY_LEFT_SHIFT)){ fast++; } if(IsKeyDown(KEY_RIGHT)){ myplayer.position.x += fast; myplayer.direction = 2; } if(IsKeyDown(KEY_LEFT)){ myplayer.position.x -= fast; myplayer.direction = 1; } if(recttilecollide(myplayer.position.x,myplayer.position.y,myplayer.w,myplayer.h,0,0)){ myplayer.position = oldpos; } if(IsKeyPressed(KEY_Z)){ // Slide the laser weapon if(myplayer.numslidelasers<3){ int cl=myplayer.numslidelasers; // current sliding laser number arr_slidelaser[cl].active = true; arr_slidelaser[cl].w = 16; arr_slidelaser[cl].h = 10; arr_slidelaser[cl].incdec.x = 0.02; arr_slidelaser[cl].state = 0; arr_slidelaser[cl].position.x = myplayer.position.x; arr_slidelaser[cl].position.y = myplayer.position.y+arr_slidelaser[0].h+4; if(myplayer.direction==1){ arr_slidelaser[cl].inc.x = -1.5*fast; }else{ arr_slidelaser[cl].inc.x = 1.5*fast; } myplayer.numslidelasers++; } } } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(LIGHTGRAY); // Draw the turrets for(int i=0;i0)continue; DrawCircle(arr_effect2[i].position.x,arr_effect2[i].position.y,arr_effect2[i].radius,WHITE); } // some screen info DrawText("Cursor Left and Right. Left Shift = Run. Z key is slide laser weapon.",2,2,22,WHITE); DrawText(FormatText("SlideLasers : %02i",3-myplayer.numslidelasers),2,screenHeight-32,26,WHITE); //DrawText(FormatText("SlideLasers : %f",PI*2.0f),312,screenHeight-32,26,WHITE); EndDrawing(); //------------------------------- -------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; } void createeffect(int posx, int posy){ //int posx = GetRandomValue(0,screenWidth); //int posy = GetRandomValue(0,screenHeight); for(int i=0;i<5;i++){ if(arr_effect2[i].active==true)continue; arr_effect2[i].active = true; arr_effect2[i].position.x = posx+GetRandomValue(-32,32); arr_effect2[i].position.y = posy+GetRandomValue(-32,32); arr_effect2[i].startdelay = GetRandomValue(0,60); arr_effect2[i].countdown = 10+arr_effect2[i].startdelay; arr_effect2[i].radius = GetRandomValue(tileWidth/4,tileWidth); } int i=0; int cnt=0; while(cnt<32){ i++; if(i>MAX_EFFECT){ continue; cnt=51; } if(arr_effect[i].active==true){ continue; } arr_effect[i].active = true; arr_effect[i].position.x = posx; arr_effect[i].position.y = posy; arr_effect[i].w = 16; arr_effect[i].h = 16; arr_effect[i].inc.x = GetRandomValue(-1,1); arr_effect[i].inc.y = GetRandomValue(-5,-2); arr_effect[i].incmod.x = (float)(GetRandomValue(0,100)/3500.0f); arr_effect[i].incmod.y = (float)(GetRandomValue(0,100)/1000.0f);//+0.1f; if(GetRandomValue(0,8)==1){ arr_effect[i].incmod.x*=5; arr_effect[i].inc.y*=1.5; } if(GetRandomValue(0,8)==1){ arr_effect[i].incmod.x*=5; arr_effect[i].inc.y/=2; } arr_effect[i].countdown = GetRandomValue(30,70); cnt++; } } //Unit collide with solid blocks true/false bool recttilecollide(int x, int y,int w, int h, int offsetx,int offsety){ int cx = (x+offsetx)/tileWidth; int cy = (y+offsety)/tileHeight; for(int y2=cy-1; y2=0 && x2=0 && y2= (x2 + w2) || (x1 + w1) <= x2) return false; if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false; return true; }