Skip to content

Commit

Permalink
Refactor code into multiple source files
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewL246 committed Nov 15, 2023
1 parent 8513bfd commit d9f6a93
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 248 deletions.
57 changes: 57 additions & 0 deletions source/bullet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <math.h>

#include "bullet.h"
#include "drawing.h"

Bullet bullets[MAX_BULLETS];
int bulletCount = 0;

void AddBullet(Tower fromTower)
{
if (bulletCount < MAX_BULLETS)
{
Bullet newBullet = *fromTower.bulletsFired;
newBullet.position = fromTower.position;

// Calculate the direction vector from the bullet to the target
Point direction;
direction.x = fromTower.targetPosition.x - newBullet.position.x;
direction.y = fromTower.targetPosition.y - newBullet.position.y;

// Normalize the direction vector to get a unit vector
float length = sqrt(direction.x * direction.x + direction.y * direction.y);
float directionX = direction.x / length;
float directionY = direction.y / length;

// Multiply the unit direction vector by the bullet's speed to get the velocity vector
Point velocity;
velocity.x = directionX * newBullet.speed;
velocity.y = directionY * newBullet.speed;

newBullet.velocity = velocity;

bullets[bulletCount] = newBullet;
bulletCount++;
}
}

void MoveAllBullets()
{
for (int i = 0; i < bulletCount; i++)
{
// Move the bullet
bullets[i].position.x += bullets[i].velocity.x;
bullets[i].position.y += bullets[i].velocity.y;

if (bullets[i].position.x >= DRC_SCREEN_WIDTH || bullets[i].position.y >= DRC_SCREEN_HEIGHT ||
bullets[i].position.x < 0 || bullets[i].position.y < 0)
{
for (int j = i; j < bulletCount - 1; j++)
{
bullets[j] = bullets[j + 1];
}
bulletCount--;
i--;
}
}
}
25 changes: 25 additions & 0 deletions source/bullet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BULLET_H
#define BULLET_H

#include "structs.h"
#include "tower.h"

typedef struct Bullet
{
Point position;
Point velocity;
int size;
int speed;
int damage;
int health;
} Bullet;

#define MAX_BULLETS 1000
extern Bullet bullets[MAX_BULLETS];
extern int bulletCount;

void AddBullet(Tower fromTower);

void MoveAllBullets();

#endif
80 changes: 80 additions & 0 deletions source/drawing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
#include <coreinit/screen.h>

#include "drawing.h"
#include "tower.h"
#include "bullet.h"
#include "enemy.h"

const int DRC_TOUCH_TOP = 3900;
const int DRC_TOUCH_BOTTOM = 180;
const int DRC_TOUCH_LEFT = 100;
const int DRC_TOUCH_RIGHT = 3960;

Point MapTouchToDrcScreen(int touchX, int touchY)
{
Point screenPoint;
screenPoint.x = ((touchX - DRC_TOUCH_LEFT) / (float)(DRC_TOUCH_RIGHT - DRC_TOUCH_LEFT)) * DRC_SCREEN_WIDTH;
screenPoint.y = DRC_SCREEN_HEIGHT - (((touchY - DRC_TOUCH_BOTTOM) / (float)(DRC_TOUCH_TOP - DRC_TOUCH_BOTTOM)) * DRC_SCREEN_HEIGHT);

return screenPoint;
}

const int TV_WIDTH = 1280;
const int TV_HEIGHT = 720;

void DrawPoint(Point point, Color color, int size, bool drawOnBothScreens)
{
int halfSize = size / 2;
for (int dx = -halfSize; dx <= halfSize; dx++)
{
for (int dy = -halfSize; dy <= halfSize; dy++)
{
if (sqrt(dx * dx + dy * dy) <= halfSize)
{
OSScreenPutPixelEx(SCREEN_DRC, point.x + dx, point.y + dy, color.r << 24 | color.g << 16 | color.b << 8);
if (drawOnBothScreens)
{
float tvScaleX = (float)TV_WIDTH / DRC_SCREEN_WIDTH;
float tvScaleY = (float)TV_HEIGHT / DRC_SCREEN_HEIGHT;
int tvX = (point.x + dx) * tvScaleX;
int tvY = (point.y + dy) * tvScaleY;
for (int tvDx = -1; tvDx <= 1; tvDx++)
{
for (int tvDy = -1; tvDy <= 1; tvDy++)
{
OSScreenPutPixelEx(SCREEN_TV, tvX + tvDx, tvY + tvDy, color.r << 24 | color.g << 16 | color.b << 8);
}
}
}
}
}
}
}

void DrawAllTowers()
{
for (int i = 0; i < towerCount; i++)
{
DrawPoint(towers[i].position, towers[i].color, towers[i].size, true);
DrawPoint(towers[i].targetPosition, (Color){255, 155, 0}, 10, true);
}
}

void DrawAllBullets()
{
for (int i = 0; i < bulletCount; i++)
{
DrawPoint(bullets[i].position, (Color){150, 150, 150}, bullets[i].size, true);
}
}

void DrawAllEnemies()
{
for (int i = 0; i < enemyCount; i++)
{
DrawPoint(enemies[i].position, enemies[i].color, enemies[i].size, true);
}
}
22 changes: 22 additions & 0 deletions source/drawing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef DRAWING_H
#define DRAWING_H

#include <stdbool.h>

#include "structs.h"
#include <coreinit/time.h>

#define DRC_SCREEN_WIDTH 854
#define DRC_SCREEN_HEIGHT 480

Point MapTouchToDrcScreen(int touchX, int touchY);

void DrawPoint(Point point, Color color, int size, bool drawOnBothScreens);

void DrawAllTowers();

void DrawAllBullets();

void DrawAllEnemies();

#endif
39 changes: 39 additions & 0 deletions source/enemy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "enemy.h"
#include "drawing.h"

Enemy enemies[MAX_ENEMIES];
int enemyCount = 0;

void AddEnemy()
{
if (enemyCount < MAX_ENEMIES)
{
Enemy newEnemy;
newEnemy.position = (Point){0, DRC_SCREEN_HEIGHT / 2};
newEnemy.color = (Color){255, 0, 0};
newEnemy.size = 20;
newEnemy.speed = 1;
newEnemy.health = 1;

enemies[enemyCount] = newEnemy;
enemyCount++;
}
}

void MoveAllEnemies()
{
for (int i = 0; i < enemyCount; i++)
{
enemies[i].position.x += enemies[i].speed;

if (enemies[i].position.x >= DRC_SCREEN_WIDTH)
{
for (int j = i; j < enemyCount - 1; j++)
{
enemies[j] = enemies[j + 1];
}
enemyCount--;
i--;
}
}
}
23 changes: 23 additions & 0 deletions source/enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ENEMY_H
#define ENEMY_H

#include "structs.h"

typedef struct Enemy
{
Point position;
Color color;
int size;
int speed;
int health;
} Enemy;

#define MAX_ENEMIES 200
extern Enemy enemies[MAX_ENEMIES];
extern int enemyCount;

void AddEnemy();

void MoveAllEnemies();

#endif
Loading

0 comments on commit d9f6a93

Please sign in to comment.