Compare commits
No commits in common. "73eedda4292cb2355a135b73e3120cc45312470d" and "4b4e83ac7ebb785174c6704d7593b230d313f490" have entirely different histories.
73eedda429
...
4b4e83ac7e
6 changed files with 24 additions and 41 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
#include "maploader.h"
|
||||||
|
|
||||||
const int SCREEN_HEIGHT = 320;
|
const int SCREEN_HEIGHT = 320;
|
||||||
const int SCREEN_WIDTH = 320;
|
const int SCREEN_WIDTH = 320;
|
||||||
|
|
@ -14,7 +15,7 @@ int main() {
|
||||||
|
|
||||||
World& world = get_world();
|
World& world = get_world();
|
||||||
|
|
||||||
load_world(world,"assets/map");
|
loadmap(world);
|
||||||
|
|
||||||
world.setup();
|
world.setup();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,22 @@
|
||||||
|
#include "maploader.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
|
||||||
void load_world(World& world,const char* path) {
|
void loadmap(World& world) {
|
||||||
std::ifstream in;
|
std::ifstream in;
|
||||||
in.open(path);
|
in.open("assets/map");
|
||||||
|
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
|
|
||||||
int max_x = 0;
|
|
||||||
int max_y = 0;
|
|
||||||
|
|
||||||
while(in.eof() == false)
|
while(in.eof() == false)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
in.get(c);
|
in.get(c);
|
||||||
|
|
||||||
|
|
||||||
if (max_x < x)
|
|
||||||
max_x = x;
|
|
||||||
if (max_y < y)
|
|
||||||
max_y = y;
|
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
|
||||||
case '#':
|
case '#':
|
||||||
{
|
{
|
||||||
Wall* wall = new Wall;
|
Wall* wall = new Wall;
|
||||||
|
|
@ -44,7 +35,6 @@ void load_world(World& world,const char* path) {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case '\n':
|
case '\n':
|
||||||
|
|
||||||
x = 0;
|
x = 0;
|
||||||
y++;
|
y++;
|
||||||
break;
|
break;
|
||||||
|
|
@ -52,12 +42,6 @@ void load_world(World& world,const char* path) {
|
||||||
x++;
|
x++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
world.height = CELL_SIZE * (max_y-1);
|
|
||||||
world.width = CELL_SIZE * (max_x-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
8
src/maploader.h
Normal file
8
src/maploader.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef MAPLOADER_H
|
||||||
|
#define MAPLOADER_H
|
||||||
|
|
||||||
|
#include "world.h"
|
||||||
|
|
||||||
|
void loadmap(World& world);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
#include <iostream>
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
|
|
@ -20,7 +19,6 @@ Pacman::Pacman() {
|
||||||
this->facing = 0;
|
this->facing = 0;
|
||||||
this->position = {0.,0.};
|
this->position = {0.,0.};
|
||||||
this->frame = 0;
|
this->frame = 0;
|
||||||
this->time = 0;
|
|
||||||
}
|
}
|
||||||
Pacman::Pacman(Vector2 position) {
|
Pacman::Pacman(Vector2 position) {
|
||||||
this->texture = LoadTexture("assets/sprites/pacman.png");
|
this->texture = LoadTexture("assets/sprites/pacman.png");
|
||||||
|
|
@ -42,24 +40,22 @@ void Pacman::tick() {
|
||||||
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
|
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
|
||||||
Vector2 new_position = Vector2Add(this->position,Vector2Scale(direction, (float)speed));
|
Vector2 new_position = Vector2Add(this->position,Vector2Scale(direction, (float)speed));
|
||||||
|
|
||||||
World& world = get_world();
|
if (get_world().grid[indexify_position(new_position)] == nullptr){
|
||||||
|
|
||||||
if (world.grid[indexify_position(new_position)] == nullptr){
|
|
||||||
this->position=new_position;
|
this->position=new_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bound check
|
// Bound check
|
||||||
if (this->position.x < world.bound_offset*CELL_SIZE){
|
if (this->position.x < 0){
|
||||||
this->position.x += world.width-2*world.bound_offset*CELL_SIZE;
|
this->position.x += 320;
|
||||||
}
|
}
|
||||||
if (this->position.y < world.bound_offset*CELL_SIZE){
|
if (this->position.y < 0){
|
||||||
this->position.y += world.height-2*world.bound_offset*CELL_SIZE;
|
this->position.y += 320;
|
||||||
}
|
}
|
||||||
if (this->position.x >= world.width-(world.bound_offset-1)*CELL_SIZE){
|
if (this->position.x >= 320){
|
||||||
this->position.x -= world.width-2*world.bound_offset*CELL_SIZE;
|
this->position.x -= 320;
|
||||||
}
|
}
|
||||||
if (this->position.y >= world.height-(world.bound_offset-1)*CELL_SIZE){
|
if (this->position.y >= 320){
|
||||||
this->position.y -= world.height-2*world.bound_offset*CELL_SIZE;
|
this->position.y -= 320;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ World::World(){
|
||||||
this->seconds_per_tick = 0;
|
this->seconds_per_tick = 0;
|
||||||
this->clock = 0;
|
this->clock = 0;
|
||||||
this->debug = false;
|
this->debug = false;
|
||||||
this->bound_offset = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
World::~World(){
|
World::~World(){
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@
|
||||||
const int GRID_ROWS = 20;
|
const int GRID_ROWS = 20;
|
||||||
const int GRID_COLUMNS = 20;
|
const int GRID_COLUMNS = 20;
|
||||||
const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS;
|
const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS;
|
||||||
const int CELL_SIZE = 16;
|
|
||||||
|
|
||||||
/// Class that holds information about game world
|
/// Class that holds information about game world
|
||||||
class World {
|
class World {
|
||||||
|
|
@ -20,10 +19,7 @@ class World {
|
||||||
std::vector<Entity*> entities; /// Main subjects of game world.
|
std::vector<Entity*> entities; /// Main subjects of game world.
|
||||||
Entity* grid[GRID_CAPACITY]; /// Grid representation
|
Entity* grid[GRID_CAPACITY]; /// Grid representation
|
||||||
float seconds_per_tick; /// Internal clock speed
|
float seconds_per_tick; /// Internal clock speed
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
int bound_offset;
|
|
||||||
|
|
||||||
void setup(); /// Sets up game world.
|
void setup(); /// Sets up game world.
|
||||||
/// Should be called once when entites are set.
|
/// Should be called once when entites are set.
|
||||||
|
|
||||||
|
|
@ -40,6 +36,5 @@ class World {
|
||||||
void create_world_with(float seconds_per_tick);
|
void create_world_with(float seconds_per_tick);
|
||||||
World& get_world(); // Thanks, 2ndbeam, helps a lot
|
World& get_world(); // Thanks, 2ndbeam, helps a lot
|
||||||
int indexify_position(Vector2 vector);
|
int indexify_position(Vector2 vector);
|
||||||
void load_world(World& world, const char* path);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue