Hello everyone, this is the first time I'm writing to this forum and the first time I'm trying to make a game using c++ so I apologize in case I am oblivious to some known game development concepts. Anyway the first thing I tried to address was movement handling. Instead of having to litter the main procedure with variables, I thought I should make a wrapper class possibly deriving from the sprite class in order to handle movement and general functions better.
Below is a small example-class showing what I came up. Is this a good approach for general movement? The real class will of course address other things like collision and camera view but I would like some feedback first to see if this is a good approach. Take note that I'm using the 1.6 library because of some technical reasons and since this is an educational project I thought it shouldn't matter much. In the example I'm also deriving from the Shape class since I don't really need any of the Drawable class functionalities.
Here is the main file:
#include <SFML/Graphics.hpp>
#include "Object.h"
int main(int argc, char** argv) {
double frt;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Event Event;
Object Rect = sf::Shape::Rectangle(0, 0, 10, 10, sf::Color::Red);
Rect.bottomBoundary(590);
Rect.rightBoundary(790);
Rect.gravity(500);
Rect.SetPosition(0,590);
Rect.onGround(true);
while (App.IsOpened()) {
frt = App.GetFrameTime();
if (App.GetInput().IsKeyDown(sf::Key::Left)) {
if (Rect.horizontalSpeed() == 0) {
Rect.horizontalSpeed(-5);
}
Rect.acceleration(-500);
}
else if (App.GetInput().IsKeyDown(sf::Key::Right)) {
if (Rect.horizontalSpeed() == 0) {
Rect.horizontalSpeed(5);
}
Rect.acceleration(500);
}
else {
if (Rect.horizontalSpeed() > 0) {
Rect.acceleration(-500);
}
else if (Rect.horizontalSpeed() < 0) {
Rect.acceleration(500);
}
}
if (Rect.horizontalSpeed() < 0.10 && Rect.horizontalSpeed() > -0.10) {
Rect.acceleration(0);
Rect.horizontalSpeed(0);
}
if (App.GetInput().IsKeyDown(sf::Key::Up) && Rect.onGround()) {
Rect.jump(590);
Rect.onGround(false);
}
while (App.GetEvent(Event)) {
if (Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape) {
App.Close();
}
}
Rect.updateMovement(frt);
App.Clear();
App.Draw(Rect);
App.Display();
}
return EXIT_SUCCESS;
}
And here is the wrapper class:
#include <SFML/Graphics.hpp>
#include "Object.h"
#include <math.h>
Object::Object(const sf::Shape& other) :
sf::Shape(other) , _gravity(0), _accel(0), _uBound(0), _bBound(0),
_lBound(0), _rBound(0), _vSpeed(0), _hSpeed(0), _onGround(false) {}
void Object::gravity(double gravity) {
_gravity = gravity;
}
double Object::acceleration() {
return _accel;
}
void Object::acceleration(double accel) {
_accel = accel;
}
void Object::upperBoundary(double bound) {
_uBound = bound;
}
void Object::bottomBoundary(double bound) {
_bBound = bound;
}
void Object::leftBoundary(double bound) {
_lBound = bound;
}
void Object::rightBoundary(double bound) {
_rBound = bound;
}
void Object::verticalSpeed(double speed) {
_vSpeed = speed;
}
void Object::horizontalSpeed(double speed) {
_hSpeed = speed;
}
void Object::jump(double meters) {
_vSpeed = -std::sqrt(2*_gravity*meters);
}
double Object::horizontalSpeed() {
return _hSpeed;
}
double Object::verticalSpeed() {
return _vSpeed;
}
bool Object::onGround() {
return _onGround;
}
void Object::onGround(bool onGround) {
_onGround = onGround;
}
void Object::updateMovement(double frt) {
if (!_onGround) {
_vSpeed += _gravity * frt;
}
if (GetPosition().y + _vSpeed * frt >= _bBound) {
SetY(_bBound);
_vSpeed = 0;
_onGround = true;
}
if (GetPosition().y +_vSpeed * frt <= _uBound) {
SetY(_uBound);
_vSpeed = 0;
}
if (GetPosition().x + _hSpeed * frt >= _rBound) {
SetX(_rBound);
_hSpeed = 0;
}
if (GetPosition().x + _hSpeed * frt <= _lBound) {
SetX(_lBound);
_hSpeed = 0;
}
_hSpeed += _accel * frt;
Move(_hSpeed * frt, _vSpeed * frt);
}