Skip to content

Instantly share code, notes, and snippets.

@nate-h
Created October 2, 2017 01:42
Show Gist options
  • Save nate-h/766c8815d49d42065157a5b96879ac7f to your computer and use it in GitHub Desktop.
Save nate-h/766c8815d49d42065157a5b96879ac7f to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////////////////////
// Jump functions used for first game I created.
// Disclaimer: I wrote this code fresh out of college. It's ugly!
////////////////////////////////////////////////////////////////////////////////
// Here's an overview in the rules for gravity in the game.
// 1. Mario is always falling.
// 2. If mario hits his head on an object, set velocity_y to 0
// 3. If mario hits his feet on an object, set velocity_y to 0
// 4. Increment velocity_y by constant amount per-frame.
// Here's why:
// In real life, acceleration due to gravity is ~9.8 m/s^2
// Velocity can be calculated by v = a * t
// Assuming t is constant, and it is in this game per frame, velocity increments by 9.8 per frame.
//
// Bonus that's not implemented:
// Handling variable frame rates can be done by scaling 9.8
// Also I should ceil how much mario gains in acceleration by simulating wind drag.
// Function gets called whenver user hits up-key
void Mario::jump()
{
// Dont allow jump if on the rise.
if (vely < 0)
return;
// Only allow two jumps (I like this feature in other games)
countJumps++;
if (countJumps > 2)
return;
// Magic number for initial velocity. Analogous to a guns muzzle velocity.
jumpDouble = -1.0 * 43 / 3;
vely = jumpDouble;
// play different sound effects
if (countJumps == 1)
Mix_PlayChannel(-1, marioJump1, 0);
if (countJumps == 2)
Mix_PlayChannel(-1, marioJump2, 0);
}
// Called when mario contacts something.
void Mario::contactBottom(std::list<Entity*>::iterator charPointer)
{
Character character = (*charPointer)->getID(); // ---------- characters a crappy name since this includes non-living entities.
Entity* temp = (*charPointer);
switch (character)
{
case brick:
{
int cType = temp->getType(); // -------------------- Should have used enums here!
if (cType > 2)
Mix_PlayChannel(-1, marioImpact, 0);
else if (cType == 1)
{
Mix_PlayChannel(-1, destroyBox, 0);
allCharacters->deleteThis(charPointer);
}
else
{
((Brick*) temp)->changeType();
randomNum = rand() % 5;
// shrooms
if (randomNum == 0)
{
Mushroom* shroom = new Mushroom(1, temp->getX(),
temp->getY() - 55, everything);
allCharacters->addFront(shroom);
Mix_PlayChannel(-1, shroomRising, 0);
}
else
{
randomNum = rand() % 3;
if (randomNum == 0) {
Coin* coin = new Coin(4, temp->getX(), temp->getY() - 55,
everything);
allCharacters->addMe(coin);
score += 500;
coinCount += 5;
} else {
Coin* coin = new Coin(3, temp->getX(), temp->getY() - 55,
everything);
allCharacters->addMe(coin);
score += 100;
coinCount += 1;
}
Mix_PlayChannel(-1, getCoin, 0);
}
}
}
vely = 0;
boundary.y += moveThisMuch;
jumpDouble = 0;
break;
case tntBox:
((TntBox*) (*charPointer))->initiateCountDown();
if (((TntBox*) (*charPointer))->fallThrough())
Mix_PlayChannel(-1, marioImpact, 0);
vely = 0;
boundary.y += moveThisMuch;
jumpDouble = 0;
break;
case coin:
case shroom:
case flagPoll:
case sonic:
boundary.y += jumpDouble;
break;
default:
vely = 0;
boundary.y += moveThisMuch;
jumpDouble = 0;
break;
}
}
void Mario::marioGravity()
{
// Mario is always falling.
jumpDouble = jumpDouble + 1.0 * 5 / 9;
vely = jumpDouble;
// changing y location
copyBounds = boundary;
copyBounds.x -= *xPosition;
int bucket = copyBounds.x / 50;
int start = 0;
int end = 0;
if (bucket - 2 < 0)
start = 0;
else
start = bucket - 2;
if (bucket + 3 > (int) allCharacters->list.size())
end = allCharacters->list.size();
else
end = bucket + 3;
int middle = copyBounds.x + boundary.w/2 + 30;
// falling
if (vely >= 0) {
if(middle >= 0 && middle < (int)allCharacters->list.size() )
for (std::list<Entity*>::iterator it =
allCharacters->list[middle].begin();
it != allCharacters->list[middle].end(); ++it) {
if ((moveThisMuch = (*it)->collisionBottom(&copyBounds, vely))
!= vely) {
contactTop(it);
return;
}
}
for (int i = start; i < end; ++i)
{
for (std::list<Entity*>::iterator it =
allCharacters->list[i].begin();
it != allCharacters->list[i].end(); ++it) {
if ((moveThisMuch = (*it)->collisionBottom(&copyBounds, vely)) != vely) {
contactTop(it);
return;
}
}
}
}
else
{
if(middle >= 0 && middle < (int)allCharacters->list.size() )
for (std::list<Entity*>::iterator it =
allCharacters->list[middle].begin();
it != allCharacters->list[middle].end(); ++it) {
if ((moveThisMuch = (*it)->collisionTop(&copyBounds, vely))
!= vely) {
contactBottom(it);
return;
}
}
for (int i = start; i < end; ++i)
{
for (std::list<Entity*>::iterator it =
allCharacters->list[i].begin();
it != allCharacters->list[i].end(); ++it) {
if ((moveThisMuch = (*it)->collisionTop(&copyBounds, vely))
!= vely) {
contactBottom(it);
return;
}
}
}
}
boundary.y += vely;
}
////////////////////////////////////////////////////////////////////////////////
// Jump functions used for first game I created.
// Disclaimer: I wrote this code fresh out of college. It's ugly!
////////////////////////////////////////////////////////////////////////////////
// Here's an overview in the rules for gravity in the game.
// 1. Mario is always falling.
// 2. If mario hits his head on an object, set velocity_y to 0
// 3. If mario hits his feet on an object, set velocity_y to 0
// 4. Increment velocity_y by constant amount per-frame.
// Here's why:
// In real life, acceleration due to gravity is ~9.8 m/s^2
// Velocity can be calculated by v = a * t
// Assuming t is constant, and it is in this game per frame, velocity increments by 9.8 per frame.
//
// Bonus that's not implemented:
// Handling variable frame rates can be done by scaling 9.8
// Also I should ceil how much mario gains in acceleration by simulating wind drag.
// Function gets called whenver user hits up-key
void Mario::jump()
{
// Dont allow jump if on the rise.
if (vely < 0)
return;
// Only allow two jumps (I like this feature in other games)
countJumps++;
if (countJumps > 2)
return;
// Magic number for initial velocity. Analogous to a guns muzzle velocity.
jumpDouble = -1.0 * 43 / 3;
vely = jumpDouble;
// play different sound effects
if (countJumps == 1)
Mix_PlayChannel(-1, marioJump1, 0);
if (countJumps == 2)
Mix_PlayChannel(-1, marioJump2, 0);
}
// Called when mario contacts something.
void Mario::contactBottom(std::list<Entity*>::iterator charPointer)
{
Character character = (*charPointer)->getID(); // ---------- characters a crappy name since this includes non-living entities.
Entity* temp = (*charPointer);
switch (character)
{
case brick:
{
int cType = temp->getType(); // -------------------- Should have used enums here!
if (cType > 2)
Mix_PlayChannel(-1, marioImpact, 0);
else if (cType == 1)
{
Mix_PlayChannel(-1, destroyBox, 0);
allCharacters->deleteThis(charPointer);
}
else
{
((Brick*) temp)->changeType();
randomNum = rand() % 5;
// shrooms
if (randomNum == 0)
{
Mushroom* shroom = new Mushroom(1, temp->getX(),
temp->getY() - 55, everything);
allCharacters->addFront(shroom);
Mix_PlayChannel(-1, shroomRising, 0);
}
else
{
randomNum = rand() % 3;
if (randomNum == 0) {
Coin* coin = new Coin(4, temp->getX(), temp->getY() - 55,
everything);
allCharacters->addMe(coin);
score += 500;
coinCount += 5;
} else {
Coin* coin = new Coin(3, temp->getX(), temp->getY() - 55,
everything);
allCharacters->addMe(coin);
score += 100;
coinCount += 1;
}
Mix_PlayChannel(-1, getCoin, 0);
}
}
}
vely = 0;
boundary.y += moveThisMuch;
jumpDouble = 0;
break;
case tntBox:
((TntBox*) (*charPointer))->initiateCountDown();
if (((TntBox*) (*charPointer))->fallThrough())
Mix_PlayChannel(-1, marioImpact, 0);
vely = 0;
boundary.y += moveThisMuch;
jumpDouble = 0;
break;
case coin:
case shroom:
case flagPoll:
case sonic:
boundary.y += jumpDouble;
break;
default:
vely = 0;
boundary.y += moveThisMuch;
jumpDouble = 0;
break;
}
}
void Mario::marioGravity()
{
// Mario is always falling.
jumpDouble = jumpDouble + 1.0 * 5 / 9;
vely = jumpDouble;
// changing y location
copyBounds = boundary;
copyBounds.x -= *xPosition;
int bucket = copyBounds.x / 50;
int start = 0;
int end = 0;
if (bucket - 2 < 0)
start = 0;
else
start = bucket - 2;
if (bucket + 3 > (int) allCharacters->list.size())
end = allCharacters->list.size();
else
end = bucket + 3;
int middle = copyBounds.x + boundary.w/2 + 30;
// falling
if (vely >= 0) {
if(middle >= 0 && middle < (int)allCharacters->list.size() )
for (std::list<Entity*>::iterator it =
allCharacters->list[middle].begin();
it != allCharacters->list[middle].end(); ++it) {
if ((moveThisMuch = (*it)->collisionBottom(&copyBounds, vely))
!= vely) {
contactTop(it);
return;
}
}
for (int i = start; i < end; ++i)
{
for (std::list<Entity*>::iterator it =
allCharacters->list[i].begin();
it != allCharacters->list[i].end(); ++it) {
if ((moveThisMuch = (*it)->collisionBottom(&copyBounds, vely)) != vely) {
contactTop(it);
return;
}
}
}
}
else
{
if(middle >= 0 && middle < (int)allCharacters->list.size() )
for (std::list<Entity*>::iterator it =
allCharacters->list[middle].begin();
it != allCharacters->list[middle].end(); ++it) {
if ((moveThisMuch = (*it)->collisionTop(&copyBounds, vely))
!= vely) {
contactBottom(it);
return;
}
}
for (int i = start; i < end; ++i)
{
for (std::list<Entity*>::iterator it =
allCharacters->list[i].begin();
it != allCharacters->list[i].end(); ++it) {
if ((moveThisMuch = (*it)->collisionTop(&copyBounds, vely))
!= vely) {
contactBottom(it);
return;
}
}
}
}
boundary.y += vely;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment