Project Description

Description

This 2D top down adventure game was one of my first c++ assignments. This project features basic 2D animations and a scrolling background. It takes around 15 minutes to finish the game, the main goal of this project was to work on gameplay logic.

Details

Type Game
Tech C++, SDL, Sprites
Duration ~2 Weeks
Status Finished 2012

Code

#pragma once

class Entity;

#include "base_object.h"
#include "Base_template.h"
using namespace Tmpl8;
using namespace std;

class Entity : public Base_object
{
public:
	Entity();
	~Entity();
	void Init(Surface* sur, int size_x, int size_y,  Surface* screen);
	void Init(Sprite* sprit, int size_x, int size_y,  Surface* screen, int frames);
	void Add_animation(int label, int start, int end, bool loop, int delay_time);
	void Play();
	void Play(int label);
	void Stop();	
	void Stop(int label);	
	void Play_from_frame(int frame);
	void Stop_on_frame(int frame);
	void Update();
	bool Collision(Entity* ent);
	int WIDTH;
	int HEIGHT;
	Sprite* sprite;
private:
	Surface* surface;

	bool got_surface;
	bool got_sprite;
	bool got_animation;
	bool loop;
	int current_animation;
	int current_frame;
	int playing;
	int animations[20][4];
	int delay;
	int cdelay;
	int max_frames;
protected:
	Surface* main_screen;
};
#include "Entity.h"

Entity::Entity()
{
	got_surface = false;
	got_sprite = false;
	got_animation = false;
	loop = false;
	current_animation = 0;
	current_frame = 0;
	playing = false;
	WIDTH = 0; 
	HEIGHT = 0;
	cdelay = 0;
	delay = 0;
}

void Entity::Init(Surface* sur, int size_x, int size_y, Surface* screen)
{
	main_screen = screen;
	surface = sur;
	got_surface = true;
	WIDTH = size_x; 
	HEIGHT = size_y;
}

void Entity::Init(Sprite* sprit, int size_x, int size_y, Surface* screen, int frames)
{
	max_frames = frames;
	main_screen = screen;
	sprite = sprit;
	got_sprite = true;
	WIDTH = size_x; 
	HEIGHT = size_y;
}

void Entity::Add_animation(int label, int start, int end, bool loop, int delay_time)
{

	if(got_sprite)
	{
		delay = delay_time;
		got_animation = true;

		animations[label][0]= start;
		animations[label][1]= end;
		animations[label][2]= loop;
		animations[label][3]= delay_time;

		current_frame = start;
		sprite->SetFrame(current_frame);
	}
}

void Entity::Play()
{
	playing = true;
}

void Entity::Play(int label)
{
	current_animation = label;
	current_frame = animations[current_animation][0];	
	delay = animations[current_animation][3];
	cdelay = 0;
	playing = true;
}

void Entity::Stop()
{
	playing = false;
}

void Entity::Stop(int label)
{
	current_animation = label;
	current_frame = animations[current_animation][0];
	playing = false;
}

void Entity::Play_from_frame(int frame)
{
	current_frame = frame;
	playing = true;
}

void Entity::Stop_on_frame(int frame)
{
	current_frame = frame;
	sprite->SetFrame(current_frame);
	playing = false;
}

void Entity::Update()
{
	if(got_surface)
	{
		surface->CopyTo(main_screen, this->X, this->Y);
	}

	if(got_sprite)
	{
		if(got_animation)
		{
			if(playing)
			{
				if(cdelay >= delay)
				{
					cdelay = 0;
					if(current_frame < max_frames) 					{ 						current_frame++; 					} 					int start = animations[current_animation][0]; 					int end = animations[current_animation][1]; 					int loop = animations[current_animation][2]; 					delay = animations[current_animation][3]; 					if(current_frame > end)
					{
						if(loop)
						{
							current_frame = start;
						}
						else
						{
							Stop();
						}
					}
				}
				else
				{
					cdelay++;
				}				
			}
		}

		sprite->SetFrame(current_frame);
		sprite->Draw(this->X, this->Y, main_screen);
	}
}

bool Entity::Collision(Entity* ent)
{
	if
	( 
		(ent->X + (ent->WIDTH -1)) > this->X   && 
		(ent->X + 1) < (this->X + this->WIDTH)  && 
		(ent->Y + (ent->HEIGHT - 1)) > this->Y && 
		(ent->Y + 1) < (this->Y + this->HEIGHT) 
	)
	{
		return true;
	}
	return false;
}

Entity::~Entity()
{

}