| 1 | /* Copyright (c) 2007 Scott Lembcke |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | #include "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/ |
| 23 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 24 | |
| 25 | #define PLAYER_VELOCITYcxx . . $ `demo/Player.c:25:9`! 500.0 |
| 26 | |
| 27 | #define PLAYER_GROUND_ACCEL_TIMEcxx . . $ `demo/Player.c:27:9`! 0.1 |
| 28 | #define PLAYER_GROUND_ACCELcxx . . $ `demo/Player.c:28:9`! (PLAYER_VELOCITYcxx . . $ `demo/Player.c:25:9`!/PLAYER_GROUND_ACCEL_TIMEcxx . . $ `demo/Player.c:27:9`!) |
| 29 | |
| 30 | #define PLAYER_AIR_ACCEL_TIMEcxx . . $ `demo/Player.c:30:9`! 0.25 |
| 31 | #define PLAYER_AIR_ACCELcxx . . $ `demo/Player.c:31:9`! (PLAYER_VELOCITYcxx . . $ `demo/Player.c:25:9`!/PLAYER_AIR_ACCEL_TIMEcxx . . $ `demo/Player.c:30:9`!) |
| 32 | |
| 33 | #define JUMP_HEIGHTcxx . . $ `demo/Player.c:33:9`! 50.0 |
| 34 | #define JUMP_BOOST_HEIGHTcxx . . $ `demo/Player.c:34:9`! 55.0 |
| 35 | #define FALL_VELOCITYcxx . . $ `demo/Player.c:35:9`! 900.0 |
| 36 | #define GRAVITYcxx . . $ `demo/Player.c:36:9`! 2000.0 |
| 37 | |
| 38 | static cpBodycxx . . $ cpBody# *playerBodycxx . . $ playerBody. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 39 | static cpShapecxx . . $ cpShape# *playerShapecxx . . $ playerShape. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 40 | |
| 41 | static cpFloatcxx . . $ cpFloat# remainingBoostcxx . . $ remainingBoost. = 0; |
| 42 | static cpBoolcxx . . $ cpBool# groundedcxx . . $ grounded. = cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!; |
| 43 | static cpBoolcxx . . $ cpBool# lastJumpStatecxx . . $ lastJumpState. = cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!; |
| 44 | |
| 45 | static void |
| 46 | SelectPlayerGroundNormalcxx . . $ SelectPlayerGroundNormal(25ac1e1eb88bed0).(cpBodycxx . . $ cpBody# *body, cpArbitercxx . . $ cpArbiter# *arb, cpVectcxx . . $ cpVect# *groundNormal){ |
| 47 | cpVectcxx . . $ cpVect# n = cpvnegcxx . . $ cpvneg(e06142a62109ae08).(cpArbiterGetNormalcxx . . $ cpArbiterGetNormal(bcbe2dc24ab42fef).(arb)); |
| 48 | |
| 49 | if(n.ycxx . . $ cpVect#y. > groundNormal->ycxx . . $ cpVect#y.){ |
| 50 | (*groundNormal) = n; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | playerUpdateVelocitycxx . . $ playerUpdateVelocity(1f531eb4b4f6d01b).(cpBodycxx . . $ cpBody# *body, cpVectcxx . . $ cpVect# gravity, cpFloatcxx . . $ cpFloat# damping, cpFloatcxx . . $ cpFloat# dt) |
| 56 | { |
| 57 | int jumpState = (ChipmunkDemoKeyboardcxx . . $ ChipmunkDemoKeyboard..ycxx . . $ cpVect#y. > 0.0f); |
| 58 | |
| 59 | // Grab the grounding normal from last frame |
| 60 | cpVectcxx . . $ cpVect# groundNormal = cpvzerocxx . . $ cpvzero.; |
| 61 | cpBodyEachArbitercxx . . $ cpBodyEachArbiter(d9db403274f2bd29).(playerBodycxx . . $ playerBody., (cpBodyArbiterIteratorFunccxx . . $ cpBodyArbiterIteratorFunc#)SelectPlayerGroundNormalcxx . . $ SelectPlayerGroundNormal(25ac1e1eb88bed0)., &groundNormal); |
| 62 | |
| 63 | groundedcxx . . $ grounded. = (groundNormal.ycxx . . $ cpVect#y. > 0.0); |
| 64 | if(groundNormal.ycxx . . $ cpVect#y. < 0.0f) remainingBoostcxx . . $ remainingBoost. = 0.0f; |
| 65 | |
| 66 | // Do a normal-ish update |
| 67 | cpBoolcxx . . $ cpBool# boost = (jumpState && remainingBoostcxx . . $ remainingBoost. > 0.0f); |
| 68 | cpVectcxx . . $ cpVect# g = (boost ? cpvzerocxx . . $ cpvzero. : gravity); |
| 69 | cpBodyUpdateVelocitycxx . . $ cpBodyUpdateVelocity(1f531eb4b4f6d01b).(body, g, damping, dt); |
| 70 | |
| 71 | // Target horizontal speed for air/ground control |
| 72 | cpFloatcxx . . $ cpFloat# target_vx = PLAYER_VELOCITYcxx . . $ `demo/Player.c:25:9`!*ChipmunkDemoKeyboardcxx . . $ ChipmunkDemoKeyboard..xcxx . . $ cpVect#x.; |
| 73 | |
| 74 | // Update the surface velocity and friction |
| 75 | // Note that the "feet" move in the opposite direction of the player. |
| 76 | cpVectcxx . . $ cpVect# surface_v = cpvcxx . . $ cpv(3142d57d978b2176).(-target_vx, 0.0); |
| 77 | playerShapecxx . . $ playerShape.->surfaceVcxx . . $ cpShape#surfaceV. = surface_v; |
| 78 | playerShapecxx . . $ playerShape.->ucxx . . $ cpShape#u. = (groundedcxx . . $ grounded. ? PLAYER_GROUND_ACCELcxx . . $ `demo/Player.c:28:9`!/GRAVITYcxx . . $ `demo/Player.c:36:9`! : 0.0); |
| 79 | |
| 80 | // Apply air control if not grounded |
| 81 | if(!groundedcxx . . $ grounded.){ |
| 82 | // Smoothly accelerate the velocity |
| 83 | playerBodycxx . . $ playerBody.->vcxx . . $ cpBody#v..xcxx . . $ cpVect#x. = cpflerpconstcxx . . $ cpflerpconst(7678bba071383ab).(playerBodycxx . . $ playerBody.->vcxx . . $ cpBody#v..xcxx . . $ cpVect#x., target_vx, PLAYER_AIR_ACCELcxx . . $ `demo/Player.c:31:9`!*dt); |
| 84 | } |
| 85 | |
| 86 | body->vcxx . . $ cpBody#v..ycxx . . $ cpVect#y. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(body->vcxx . . $ cpBody#v..ycxx . . $ cpVect#y., -FALL_VELOCITYcxx . . $ `demo/Player.c:35:9`!, INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 91 | { |
| 92 | int jumpState = (ChipmunkDemoKeyboardcxx . . $ ChipmunkDemoKeyboard..ycxx . . $ cpVect#y. > 0.0f); |
| 93 | |
| 94 | // If the jump key was just pressed this frame, jump! |
| 95 | if(jumpState && !lastJumpStatecxx . . $ lastJumpState. && groundedcxx . . $ grounded.){ |
| 96 | cpFloatcxx . . $ cpFloat# jump_v = cpfsqrtcxx . . $ `include/chipmunk/chipmunk_types.h:68:10`!(2.0*JUMP_HEIGHTcxx . . $ `demo/Player.c:33:9`!*GRAVITYcxx . . $ `demo/Player.c:36:9`!); |
| 97 | playerBodycxx . . $ playerBody.->vcxx . . $ cpBody#v. = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(playerBodycxx . . $ playerBody.->vcxx . . $ cpBody#v., cpvcxx . . $ cpv(3142d57d978b2176).(0.0, jump_v)); |
| 98 | |
| 99 | remainingBoostcxx . . $ remainingBoost. = JUMP_BOOST_HEIGHTcxx . . $ `demo/Player.c:34:9`!/jump_v; |
| 100 | } |
| 101 | |
| 102 | // Step the space |
| 103 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 104 | |
| 105 | remainingBoostcxx . . $ remainingBoost. -= dt; |
| 106 | lastJumpStatecxx . . $ lastJumpState. = jumpState; |
| 107 | } |
| 108 | |
| 109 | static cpSpacecxx . . $ cpSpace# * |
| 110 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 111 | { |
| 112 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 113 | space->iterationscxx . . $ cpSpace#iterations. = 10; |
| 114 | space->gravitycxx . . $ cpSpace#gravity. = cpvcxx . . $ cpv(3142d57d978b2176).(0, -GRAVITYcxx . . $ `demo/Player.c:36:9`!); |
| 115 | // space->sleepTimeThreshold = 1000; |
| 116 | |
| 117 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 118 | cpShapecxx . . $ cpShape# *shape; |
| 119 | |
| 120 | // Create segments around the edge of the screen. |
| 121 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), 0.0f)); |
| 122 | shape->ecxx . . $ cpShape#e. = 1.0f; shape->ucxx . . $ cpShape#u. = 1.0f; |
| 123 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 124 | |
| 125 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 126 | shape->ecxx . . $ cpShape#e. = 1.0f; shape->ucxx . . $ cpShape#u. = 1.0f; |
| 127 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 128 | |
| 129 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 130 | shape->ecxx . . $ cpShape#e. = 1.0f; shape->ucxx . . $ cpShape#u. = 1.0f; |
| 131 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 132 | |
| 133 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 134 | shape->ecxx . . $ cpShape#e. = 1.0f; shape->ucxx . . $ cpShape#u. = 1.0f; |
| 135 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 136 | |
| 137 | // Set up the player |
| 138 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(1.0f, INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!)); |
| 139 | body->pcxx . . $ cpBody#p. = cpvcxx . . $ cpv(3142d57d978b2176).(0, -200); |
| 140 | body->velocity_funccxx . . $ cpBody#velocity_func. = playerUpdateVelocitycxx . . $ playerUpdateVelocity(1f531eb4b4f6d01b).; |
| 141 | playerBodycxx . . $ playerBody. = body; |
| 142 | |
| 143 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNew2cxx . . $ cpBoxShapeNew2(ac0258d00568be32).(body, cpBBNewcxx . . $ cpBBNew(f05aa3203068e6d3).(-15.0, -27.5, 15.0, 27.5), 10.0)); |
| 144 | // shape = cpSpaceAddShape(space, cpSegmentShapeNew(playerBody, cpvzero, cpv(0, radius), radius)); |
| 145 | shape->ecxx . . $ cpShape#e. = 0.0f; shape->ucxx . . $ cpShape#u. = 0.0f; |
| 146 | shape->typecxx . . $ cpShape#type. = 1; |
| 147 | playerShapecxx . . $ playerShape. = shape; |
| 148 | |
| 149 | // Add some boxes to jump on |
| 150 | for(int i=0; i<6; i++){ |
| 151 | for(int j=0; j<3; j++){ |
| 152 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(4.0f, INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!)); |
| 153 | body->pcxx . . $ cpBody#p. = cpvcxx . . $ cpv(3142d57d978b2176).(100 + j*60, -200 + i*60); |
| 154 | |
| 155 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, 50, 50, 0.0)); |
| 156 | shape->ecxx . . $ cpShape#e. = 0.0f; shape->ucxx . . $ cpShape#u. = 0.7f; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return space; |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 165 | { |
| 166 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 167 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 168 | } |
| 169 | |
| 170 | ChipmunkDemocxx . . $ ChipmunkDemo# Playercxx . . $ Player. = { |
| 171 | "Platformer Player Controls", |
| 172 | 1.0/180.0, |
| 173 | initcxx . . $ init(1f0082e822a7ce76)., |
| 174 | updatecxx . . $ update(fa66a0a48611083e)., |
| 175 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 176 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 177 | }; |