Quellcode durchsuchen

Add files via upload

Rudy Boudewijn van Etten vor 4 Jahren
Ursprung
Commit
0fb3ccb55b
2 geänderte Dateien mit 65 neuen und 57 gelöschten Zeilen
  1. 65 57
      ai/Tanks/tanks.c
  2. BIN
      ai/Tanks/tanks.exe

+ 65 - 57
ai/Tanks/tanks.c

@@ -22,6 +22,7 @@ static struct bullet bul[MAXBULLETS];
 
 typedef struct entity{
     bool active;
+    int collisionskip;
     int command[MAXCOMMANDS];
     int value[MAXCOMMANDS];
     int command2[MAXCOMMANDS];
@@ -51,8 +52,13 @@ void drawentities();
 void inientity(int entity,int x, int y);
 void updatebullets();
 void drawbullets();
-void shootbullet(Vector2 position,int angle);
+void shootbullet(Vector2 position,float angle);
 float distance(float x1,float y1,float x2,float y2);
+float getangle(float x1,float y1,float x2,float y2);
+float angledifference(float angle1, float angle2);
+
+
+int debug;
 
 int main(void)
 {
@@ -66,6 +72,15 @@ int main(void)
     SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------
 
+    for(int i=0;i<MAXBULLETS;i++){
+        bul[i].active=false;
+        bul[i].position=(Vector2){320,200};
+        
+    }
+    for(int i=0;i<MAXENTITIES;i++){
+        ent[i].active=false;
+    }
+
 
     int x=200;
     int y=200;
@@ -78,7 +93,6 @@ int main(void)
         }
     }
 
-    int debug=0;
     // Main game loop
     while (!WindowShouldClose())    // Detect window close button or ESC key
     {
@@ -90,6 +104,7 @@ int main(void)
         updateentities();
         entitiescollision();
         updatebullets();
+        //if(GetRandomValue(0,20)==1)shootbullet((Vector2){320,200},GetRandomValue(0,360));
         
 
         // Draw
@@ -104,7 +119,7 @@ int main(void)
         drawentities();
         drawbullets();
 
-        DrawText(FormatText("%i",debug), 100, 20, 20, BLACK);
+        DrawText(FormatText("%i",debug), 0, 0, 20, BLACK);
 
         EndDrawing();
         //----------------------------------------------------------------------------------
@@ -119,23 +134,27 @@ int main(void)
     return 0;
 }
 
-void shootbullet(Vector2 position, int angle){
+void shootbullet(Vector2 position, float angle){
     for(int i=0;i<MAXBULLETS;i++){
         if(bul[i].active==false){
             bul[i].active = true;
             bul[i].position = position;
-            bul[i].inc.x = cos(angle*DEG2RAD)*3;
-            bul[i].inc.y = sin(angle*DEG2RAD)*3;
+            bul[i].inc.x = (cos(angle*DEG2RAD));
+            bul[i].inc.y = (sin(angle*DEG2RAD));
+            bul[i].inc.x *=3;
+            bul[i].inc.y *=3;
+
             bul[i].position.x += cos(angle*DEG2RAD)*16;
             bul[i].position.y += sin(angle*DEG2RAD)*16;
 
-            bul[i].countdown = 256;
+            bul[i].countdown = 350;
             return;
         }
     }
 }
 
 void updatebullets(){
+    
     for(int i=0;i<MAXBULLETS;i++){
         if(bul[i].active==false)continue;        
         bul[i].position.x += bul[i].inc.x;
@@ -205,54 +224,21 @@ void entitiescollision(){
     for(int i=0;i<MAXENTITIES;i++){
     for(int j=0;j<MAXENTITIES;j++){
         if(i==j)continue;
-            if(distance(ent[i].position.x,ent[i].position.y,ent[j].position.x,ent[j].position.y)<36){
-                //ent[i].active=false;
-                int cnt=999;
-                float angle=ent[i].angle*DEG2RAD;
-                int x=ent[i].position.x;
-                int y=ent[i].position.y;
-                while(distance(x,y,ent[j].position.x,ent[j].position.y)<46){
-                    x+=cos(angle)*2;
-                    y+=sin(angle)*2;
-                    cnt--;
-                    if(cnt<0)break;
-                }
-                if(cnt>0){
-                    ent[i].command[0]=1;
-                    ent[i].value[0]=50;
-                    ent[i].command[1]=2;
-                    ent[i].value[1]=30;
-                    ent[i].pos=0;
-                    ent[i].valuecount=-1;
-
-                }
-                if(cnt<0){
-                    x = ent[i].position.x;
-                    y = ent[i].position.y;
-                    angle=ent[i].angle*DEG2RAD;
-                    cnt=999;
-                    while(distance(x,y,ent[j].position.x,ent[j].position.y)<46){
-                        x-=cos(angle)*2;
-                        y-=sin(angle)*2;
-                        cnt--;
-                        if(cnt<0)break;
-                    }
-                    if(cnt>0){
-                        ent[i].command[0]=4;
-                        ent[i].value[0]=50;                   
-                        ent[i].command[1]=3;
-                        ent[i].value[1]=50;
-                        ent[i].pos=0;
-                        ent[i].valuecount=-1;
-                        
-                    }   
-                }
-                
-                    
+            if(ent[i].collisionskip>0)ent[i].collisionskip--;
+            if(ent[i].collisionskip==0 && distance(ent[i].position.x,ent[i].position.y,ent[j].position.x,ent[j].position.y)<36){
                 
+                //ent[i].active=false;
+                float an=getangle(ent[i].position.x,ent[i].position.y,ent[j].position.x,ent[j].position.y);
+                ent[i].angle=an*RAD2DEG;
+                ent[i].command[0]=4;
+                ent[i].value[0]=40;
+                ent[i].pos=0;
+                ent[i].valuecount=-1;
+
+                }                 
             }
     }
-    }
+    
 }
 
 void updateentities(){
@@ -265,17 +251,21 @@ void updateentities(){
 
 
 
-        if(ent[i].pos==ent[i].maxcommand){
+        if(ent[i].pos>=ent[i].maxcommand){
             ent[i].pos=0;
-            for(int ii=0;i<ent[i].maxcommand;i++){
+            for(int ii=0;ii<ent[i].maxcommand;ii++){
                 ent[i].command[ii]=GetRandomValue(1,4);
                 ent[i].value[ii]=GetRandomValue(0,10);
             }
+            
+                ent[i].command[0]=1;
+                ent[i].value[0]=GetRandomValue(60,260);
+            
         }
-        if(ent[i].pos2==ent[i].maxcommand2){
+        if(ent[i].pos2>=ent[i].maxcommand2){
             
             ent[i].pos2=0;
-            for(int ii=0;i<ent[i].maxcommand2;i++){
+            for(int ii=0;ii<ent[i].maxcommand2;ii++){
                 ent[i].command2[ii]=GetRandomValue(2,3);
                 ent[i].value2[ii]=GetRandomValue(10,100);
                 if(GetRandomValue(0,10)==1){
@@ -287,6 +277,7 @@ void updateentities(){
 
         ent[i].time++;
         if(ent[i].time>ent[i].timemax && ent[i].pos<ent[i].maxcommand){
+            
             ent[i].time=0;
             //
             switch (ent[i].command[ent[i].pos]){
@@ -345,7 +336,7 @@ void updateentities(){
                         ent[i].valuecount=-1;
                     }
                 break;
-
+                    
             }
         }
     
@@ -407,4 +398,21 @@ void updateentities(){
 // Manhattan Distance (less precise)
 float distance(float x1,float y1,float x2,float y2){
     return (float)abs(x2-x1)+abs(y2-y1);
+}
+// Return the angle from - to in float
+float getangle(float x1,float y1,float x2,float y2){
+    return (float)atan2(y2-y1, x2-x1);
+}
+// takes radian iput! <0 is left is shorter else right turn is shorter.
+// When it outputs >3 you can asume it aligns with the target(2) angle.
+float angledifference(float angle1, float angle2){
+    float difference = angle1 - angle2;
+    while (difference < -PI){
+        difference += (PI*2);
+    }
+    while (difference > PI){ 
+        difference -= (PI*2);
+    }
+    return difference;
+
 }

BIN
ai/Tanks/tanks.exe