]> git.mcshandy.xyz Git - assteroids/commitdiff
initial commit. fundamentals are in
authorAynRandDuran <lpj1496@gmail.com>
Fri, 2 Jul 2021 21:20:42 +0000 (17:20 -0400)
committerAynRandDuran <lpj1496@gmail.com>
Fri, 2 Jul 2021 21:20:42 +0000 (17:20 -0400)
assteroids.h [new file with mode: 0644]
build.sh [new file with mode: 0644]
game.cpp [new file with mode: 0644]
vectormath.hpp [new file with mode: 0644]

diff --git a/assteroids.h b/assteroids.h
new file mode 100644 (file)
index 0000000..f2f2f32
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef __ass
+#define __ass
+
+#include <raylib.h>
+#include <cstdio>
+#include <ctime>
+int scrW = 800;
+int scrH = 650;
+
+Color Space = {21, 0, 26};
+Vector4 nose, port, starboard, center;
+float ship_bearing;
+bool throttle = false;
+bool ship_alive = true;
+
+Vector4* bullets;
+const int MAX_BULLETS = 32;
+unsigned int score = 0;
+Rectangle screenspace = {-10, -10, scrW+10, scrH+10};
+
+// x/y pos, z heading, w size/life
+Vector4* asteroids;
+const int MAX_ASTEROIDS = 16;
+#endif //__ass
+
diff --git a/build.sh b/build.sh
new file mode 100644 (file)
index 0000000..25fd021
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+g++ game.cpp -lraylib -lgdi32 -lwinmm -o assteroids
diff --git a/game.cpp b/game.cpp
new file mode 100644 (file)
index 0000000..cd6fc74
--- /dev/null
+++ b/game.cpp
@@ -0,0 +1,198 @@
+#include "assteroids.h"
+#include <cstring>
+#include <math.h>
+#include "vectormath.hpp"
+
+Vector2 flatten(Vector4 pV){
+    Vector2 stan = {pV.x, pV.y};
+    return stan;
+}
+
+Vector4 translate(Vector4 v1, Vector4 v2) {
+    Vector4 translation = {v1.x+v2.x, v1.y+v2.y, v1.y+v2.y, v1.z+v2.z};
+    return translation;
+}
+
+void init_ship(){
+    nose.z = 270;
+    port.z = 55;
+    starboard.z = 125;
+    
+    nose.x = 0; nose.y = -15;
+    port.x = -10; port.y = 15;
+    starboard.x = 10; starboard.y = 15;
+    center.x = scrW/2; center.y = scrH/2;
+    ship_bearing = 0;
+    
+    //allocate bullets;
+    bullets = (Vector4*)malloc(sizeof(Vector4)*MAX_BULLETS);
+    memset(bullets, 0, sizeof(Vector4) * MAX_BULLETS);
+
+    //allocate bullets;
+    asteroids = (Vector4*)malloc(sizeof(Vector4)*MAX_ASTEROIDS);
+    memset(asteroids, 0, sizeof(Vector4) * MAX_ASTEROIDS);
+}
+
+void die(){
+    ship_alive = false;
+    init_ship();
+}
+
+void spin_ship(int az_delta){
+    nose.z += az_delta; port.z += az_delta; starboard.z += az_delta;
+    nose.x = cos(nose.z * (PI/180)) * 15;
+    nose.y = sin(nose.z * (PI/180)) * 15;
+
+    port.x = cos(port.z * (PI/180)) * 15;
+    port.y = sin(port.z * (PI/180)) * 15;
+    
+    starboard.x = cos(starboard.z * (PI/180)) * 15;
+    starboard.y = sin(starboard.z * (PI/180)) * 15;
+
+}
+
+bool onscreen(Vector2 v) {
+    // Ensure v is still onscreen
+    return CheckCollisionPointRec(v, screenspace); 
+}
+
+void update_v4(Vector4* origin, Vector4* v_body, int speed){
+    Vector2 heading = v2_normal(flatten(*origin));
+    v_body->x += heading.x * (speed*throttle); 
+    v_body->y += heading.y * (speed*throttle);
+}
+
+void shoot(){
+    for(int i = 0; i < MAX_BULLETS; i++){
+        // find available bullet
+        if(bullets[i].w == 0) {
+            bullets[i].x = center.x;
+            bullets[i].y = center.y;
+            bullets[i].z = nose.z;
+            bullets[i].w = 1; 
+            break;
+        }
+    }
+}
+
+void update_bullets(){
+    Vector4* b;
+    for(int i = 0; i < MAX_BULLETS; i++){
+        if(!onscreen(flatten(bullets[i])))
+            bullets[i].w = 0;
+        
+        if(bullets[i].w == 1) {
+            b = &bullets[i];
+            b->x += cos(b->z * (PI/180)) * 15;
+            b->y += sin(b->z * (PI/180)) * 15;
+            DrawCircleV(flatten(bullets[i]), 2, WHITE);
+            for(int a = 0; a < MAX_ASTEROIDS; a++) {
+                if(CheckCollisionPointCircle(flatten(bullets[i]), flatten(asteroids[a]), asteroids[a].w)) {
+                    asteroids[a].w = 0;
+                    bullets[i].w = 0;
+                    score++;
+                    break;
+                }
+            } 
+        }
+    }
+}
+
+void spawn_astr() {
+    for(int i = 0; i < MAX_ASTEROIDS; i++){
+        // find available asteroid
+        // no size means you're fuckin dead, gyro
+        if(asteroids[i].w == 0) {
+            asteroids[i].x = 9;
+            asteroids[i].y = scrH/2;
+            asteroids[i].z = 0; //head to right I think 
+            asteroids[i].w = 64; //size 
+            break;
+        }
+    }
+}
+
+bool ship_collision(Vector4* body) {
+    return (
+            CheckCollisionPointCircle(flatten(translate(nose, center)), flatten(*body), body->w * 0.9f) ||
+            CheckCollisionPointCircle(flatten(translate(port, center)), flatten(*body), body->w * 0.9f) ||
+            CheckCollisionPointCircle(flatten(translate(starboard, center)), flatten(*body), body->w * 0.9f)
+            );
+
+}
+
+void update_astrs() {
+    Vector4* astr;
+    for(int i = 0; i < MAX_ASTEROIDS; i++) {
+        astr = &asteroids[i];
+        if(astr->w != 0) {
+            astr->x += cos(astr->z * (PI/180)) *3;
+            astr->y += sin(astr->z * (PI/180)) *3;
+            DrawPolyLines(flatten(*astr), 7, astr->w, 0, RAYWHITE);
+        }
+        if(!onscreen(flatten(*astr)))
+            astr->w = 0;
+
+        if(ship_collision(astr)){
+            die();
+        }
+        
+    }
+
+}
+
+int main(void) {
+    srand(time(NULL));
+
+    InitWindow(scrW, scrH, "Assteroids Raylib");
+    SetTargetFPS(50);
+    init_ship();
+    spin_ship(0);
+    while(!WindowShouldClose()){
+        BeginDrawing();
+        ClearBackground(Space);
+
+        if(IsKeyDown('J')) {
+            spin_ship(-4);
+        }
+        if(IsKeyDown('L')) {
+            spin_ship(4);
+        }
+        if(IsKeyPressed('I')) {
+            throttle = true;
+        }
+        if(IsKeyPressed('S')) {
+            shoot();
+        }
+        if(IsKeyPressed('K')) {
+            spawn_astr();
+        }
+
+        if(!onscreen(flatten(center)))
+            die();
+
+        if(ship_alive) {
+            update_v4(&nose, &center, 3);
+            DrawTriangleLines(
+                flatten(translate(nose, center)),
+                flatten(translate(port, center)),
+                flatten(translate(starboard, center)),
+                RAYWHITE);
+            update_bullets();
+            update_astrs();
+        } else {
+            DrawText("YOU DIED", (scrW/2)-(MeasureText("YOU DIED", 64)/2), (scrH/2)-64, 64, RED);
+            memset(bullets, 0, sizeof(Vector4) * MAX_BULLETS);
+            memset(asteroids, 0, sizeof(Vector4) * MAX_ASTEROIDS);
+        }
+        DrawText(TextFormat("%d", score), 4, scrH-34, 32, WHITE);
+        DrawFPS(0, 0);
+        EndDrawing();
+
+    }
+    CloseWindow();
+    free(bullets);
+    free(asteroids);
+
+    return 0;
+}
diff --git a/vectormath.hpp b/vectormath.hpp
new file mode 100644 (file)
index 0000000..7f399e7
--- /dev/null
@@ -0,0 +1,12 @@
+// Some vector math that Raylib didn't give me
+//
+
+float v2_magnitude(Vector2 v) {
+       return sqrt((v.x*v.x)+(v.y*v.y));
+}
+
+Vector2 v2_normal(Vector2 v) {
+       float len = v2_magnitude(v);
+       Vector2 normal = {v.x/len, v.y/len};
+       return normal;
+}