diff options
| author | schererleander <leander@schererleander.de> | 2024-08-20 21:30:13 +0200 |
|---|---|---|
| committer | schererleander <leander@schererleander.de> | 2024-08-20 21:31:54 +0200 |
| commit | f8697ab92aae42d6b4103ac04e152cfa110c646f (patch) | |
| tree | 04fe2857cc9ae2cdb2d0a4c8382fab28a12122af | |
| parent | 3227b015f4d7e14fa501b8dadd163063fda247ba (diff) | |
initial commit
| -rw-r--r-- | Makefile | 22 | ||||
| -rwxr-xr-x | build/main | bin | 0 -> 35320 bytes | |||
| -rw-r--r-- | build/main.dSYM/Contents/Info.plist | 20 | ||||
| -rw-r--r-- | build/main.dSYM/Contents/Resources/DWARF/main | bin | 0 -> 12218 bytes | |||
| -rw-r--r-- | build/main.dSYM/Contents/Resources/Relocations/aarch64/main.yml | 10 | ||||
| -rw-r--r-- | src/main.c | 188 |
6 files changed, 240 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..714e0f9 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +CC = gcc + +SRC = src/main.c + +CFLAGS = -g -I/opt/homebrew/include -L/opt/homebrew/lib -lraylib + +OUT_DIR = build + +OUT = $(OUT_DIR)/main + +all: $(OUT) + +$(OUT_DIR): + mkdir -p $(OUT_DIR) + +$(OUT): $(OUT_DIR) $(SRC) + $(CC) $(CFLAGS) $(SRC) -o $(OUT) + +clean: + rm -rf $(OUT_DIR) + +.PHONY: all clean diff --git a/build/main b/build/main Binary files differnew file mode 100755 index 0000000..433e498 --- /dev/null +++ b/build/main diff --git a/build/main.dSYM/Contents/Info.plist b/build/main.dSYM/Contents/Info.plist new file mode 100644 index 0000000..fe7fecd --- /dev/null +++ b/build/main.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> + <dict> + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + <key>CFBundleIdentifier</key> + <string>com.apple.xcode.dsym.main</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>dSYM</string> + <key>CFBundleSignature</key> + <string>????</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + </dict> +</plist> diff --git a/build/main.dSYM/Contents/Resources/DWARF/main b/build/main.dSYM/Contents/Resources/DWARF/main Binary files differnew file mode 100644 index 0000000..03b14b0 --- /dev/null +++ b/build/main.dSYM/Contents/Resources/DWARF/main diff --git a/build/main.dSYM/Contents/Resources/Relocations/aarch64/main.yml b/build/main.dSYM/Contents/Resources/Relocations/aarch64/main.yml new file mode 100644 index 0000000..fdd35a7 --- /dev/null +++ b/build/main.dSYM/Contents/Resources/Relocations/aarch64/main.yml @@ -0,0 +1,10 @@ +--- +triple: 'arm64-apple-darwin' +binary-path: 'build/main' +relocations: + - { offsetInCU: 0x26, offset: 0x26, size: 0x8, addend: 0x0, symName: _updatePlayerMovement, symObjAddr: 0x0, symBinAddr: 0x10000314C, symSize: 0x2A0 } + - { offsetInCU: 0x56, offset: 0x56, size: 0x8, addend: 0x0, symName: _updatePlayerMovement, symObjAddr: 0x0, symBinAddr: 0x10000314C, symSize: 0x2A0 } + - { offsetInCU: 0xCE, offset: 0xCE, size: 0x8, addend: 0x0, symName: _updateBullet, symObjAddr: 0x2A0, symBinAddr: 0x1000033EC, symSize: 0x20C } + - { offsetInCU: 0x12A, offset: 0x12A, size: 0x8, addend: 0x0, symName: _moveEnemy, symObjAddr: 0x4AC, symBinAddr: 0x1000035F8, symSize: 0x18 } + - { offsetInCU: 0x15C, offset: 0x15C, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x4C4, symBinAddr: 0x100003610, symSize: 0x808 } +... diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..96bb303 --- /dev/null +++ b/src/main.c @@ -0,0 +1,188 @@ +#include <raylib.h> +#include <math.h> +#include <stdbool.h> + +#define MAX_BULLET 100 +#define MAX_ENEMY 100 + +typedef struct { + Vector2 position; + Vector2 velocity; + bool alive; +} Bullet; + +typedef struct { + Vector2 position; + Vector2 movement; +} Enemy; + +void updatePlayerMovement(Vector2* position, Vector2* movement, float speed, int playerWidth, int playerHeight) { + movement->x = 0.0f; + movement->y = 0.0f; + + if (IsKeyDown(KEY_A)) + movement->x -= 1.0f; + if (IsKeyDown(KEY_D)) + movement->x += 1.0f; + if (IsKeyDown(KEY_W)) + movement->y -= 1.0f; + if (IsKeyDown(KEY_S)) + movement->y += 1.0f; + + // Normalize the movement Vector + float magnitude = sqrt(movement->x * movement->x + movement->y * movement->y); + if (magnitude > 0) + { + movement->x /= magnitude; + movement->y /= magnitude; + } + + //Keepl player in player area + if(position->x + (float)playerWidth >= GetScreenWidth()) + { + position->x = GetScreenWidth()-(float)playerWidth; + } + if(position->x <= 0) + { + position->x = 0; + } + if(position->y + (float)playerWidth >= GetScreenHeight()) + { + position->y = GetScreenHeight()-(float)playerHeight; + } + if(position->y <= 0) + { + position->y = 0; + } + + // Update player position based on movement + float deltaTime = GetFrameTime(); + position->x += movement->x * speed * deltaTime; + position->y += movement->y * speed * deltaTime; + +} + +void updateBullet(Bullet bullets[]) { + for(int i = 0; i < MAX_BULLET; i++) + { + bullets[i].position.x += bullets[i].velocity.x; + bullets[i].position.y += bullets[i].velocity.y; + + int margin = 10; + + if(bullets[i].position.x > GetScreenWidth() + margin) + { + bullets[i].alive = false; + } + if(bullets[i].position.x < 0 - margin) + { + bullets[i].alive = false; + } + if(bullets[i].position.y > GetScreenHeight() + margin) + { + bullets[i].alive = false; + } + if(bullets[i].position.y < 0 - margin) + { + bullets[i].alive = false; + } + } +} + +void moveEnemy(Enemy *enemies, Vector2 playerPosition) { + +} + +int main(void) { + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "Wizard"); + + Vector2 playerPosition = { (float)screenWidth / 2, (float)screenHeight / 2 }; + Vector2 playerMovementVec = { 0.0f, 0.0f }; + int playerHeight = 20; + int playerWidth = 20; + float playerSpeed = 200.0f; + int playerHP = 100; + + int currentBullet = 0; + int bulletSpeed = 5; + Bullet bullets[MAX_BULLET] = {0}; + + int currentEnemy = 0; + int numEnemyAlive = 0; + Enemy enemies[MAX_ENEMY] = {0}; + + SetTargetFPS(240); + + // Main game loop + while (!WindowShouldClose()) { // Detect window close button or ESC key + updatePlayerMovement(&playerPosition, &playerMovementVec, playerSpeed, playerWidth, playerHeight); + + + Vector2 mousePosition = GetMousePosition(); + float rotationAngle = atan2f(mousePosition.y - (playerPosition.y + (float)playerHeight/2), mousePosition.x - (playerPosition.x + (float)playerWidth/2)) * RAD2DEG; + + int length = 50; + Vector2 endPoint = { playerPosition.x + (float)playerWidth/2 + length * cosf(rotationAngle * DEG2RAD), playerPosition.y + (float)playerHeight/2 + length * sinf(rotationAngle * DEG2RAD) }; + + if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { + Vector2 direction = {mousePosition.x - (playerPosition.x + (float)playerWidth/2), mousePosition.y - (playerPosition.y + (float)playerHeight/2)}; + float length = sqrtf(direction.x * direction.x + direction.y * direction.y); + if (length != 0) { + direction.x /= length; + direction.y /= length; + } + + Vector2 bulletVelocity = {direction.x * bulletSpeed, direction.y * bulletSpeed}; + bullets[currentBullet].position = endPoint; + bullets[currentBullet].velocity = bulletVelocity; + bullets[currentBullet].alive = true; + currentBullet = (currentBullet + 1) % MAX_BULLET; + } + + updateBullet(bullets); + + + // Draw + BeginDrawing(); + + ClearBackground(RAYWHITE); + + //Draw background lines + for(int i=0; i < GetScreenWidth(); i += 50) { + DrawLineEx((Vector2){i, 0}, (Vector2){i, GetScreenHeight()}, 1, BLACK); + } + + for(int i=0; i < GetScreenHeight(); i += 50) { + DrawLineEx((Vector2){0, i}, (Vector2){GetScreenWidth(), i}, 1, BLACK); + } + + //Draw player and "weapon" + DrawLineEx((Vector2){playerPosition.x+(float)playerWidth/2, playerPosition.y+(float)playerHeight/2}, endPoint, 5, RED); + DrawRectangle(playerPosition.x, playerPosition.y, playerWidth, playerHeight, GREEN); + + //Draw debug info + DrawText(TextFormat("Current FPS: %i", (int)(1.0f / GetFrameTime())), 20, 20, 10, GREEN); + DrawText(TextFormat("Input: (%.2f, %.2f)", playerMovementVec.x, playerMovementVec.y), 20, 30, 10, RED); + DrawText(TextFormat("Player Position: (%.0f, %.0f)", playerPosition.x, playerPosition.y), 20, 40, 10, RED); + DrawText(TextFormat("Bullet Count: %i", currentBullet), 20, 50, 10, GREEN); + + DrawText(TextFormat("HP: %i", playerHP), 20, 60, 10, RED); + + //Draw bullets + for(int i=0; i < MAX_BULLET; i++) { + DrawCircleV(bullets[i].position, 2, RED); + } + + EndDrawing(); + } + + // De-Initialization + CloseWindow(); // Close window and OpenGL context + return 0; +} + |
