Browse Source

variable jump height

Kajetan Johannes Hammerle 3 months ago
parent
commit
4ad03000f5
3 changed files with 9 additions and 2 deletions
  1. 1 1
      Entity.cpp
  2. 1 0
      Entity.h
  3. 7 1
      Game.cpp

+ 1 - 1
Entity.cpp

@@ -1,7 +1,7 @@
 #include "Entity.h"
 
 Entity::Entity(const Vector2& size, float mass)
-    : size(size), inverseMass(1.0f / mass), onGround(false) {
+    : size(size), inverseMass(1.0f / mass), onGround(false), jumpTicks(0) {
 }
 
 void Entity::addForce(const Vector2& force) {

+ 1 - 0
Entity.h

@@ -12,6 +12,7 @@ struct Entity {
     Vector2 force;
     float inverseMass;
     bool onGround;
+    int jumpTicks;
 
     Entity(const Vector2& size, float mass);
 

+ 7 - 1
Game.cpp

@@ -15,8 +15,9 @@ void Game::tick() {
     player.preTick();
     if(physicsToggle) {
         // forces from the player
+        std::cout << controller.a.getDownTime() << "\n";
         if(controller.a.isDown() && player.onGround) {
-            player.addForce(Vector2(0.0f, 70.0f));
+            player.jumpTicks = 10;
             player.onGround = false;
         }
         if(controller.right.isDown()) {
@@ -25,6 +26,11 @@ void Game::tick() {
         if(controller.left.isDown()) {
             player.addForce(Vector2(-2.5f, 0.0f));
         }
+        if(player.jumpTicks > 0) {
+            player.jumpTicks--;
+            player.addForce(Vector2(0.0f, 10.0f));
+        }
+        player.jumpTicks *= controller.a.isDown();
         // pseudo drag
         player.addForce(Vector2(-player.velocity[0] * 0.5f, 0.0f));
     } else {