38 lines
3 KiB
HTML
Executable file
38 lines
3 KiB
HTML
Executable file
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">static</span> <span style="color:#881350;">void</span><br />
|
|
<span style="color:#003369;">postStepRemove</span>(cpSpace *space, cpShape *shape, <span style="color:#881350;">void</span> *unused)<br />
|
|
{<br />
|
|
<span style="color:#003369;">cpSpaceRemoveShape</span>(space, shape);<br />
|
|
<span style="color:#003369;">cpSpaceRemoveBody</span>(space, shape->body);<br />
|
|
<br />
|
|
<span style="color:#003369;">cpShapeFree</span>(shape);<br />
|
|
<span style="color:#003369;">cpBodyFree</span>(shape->body);<br />
|
|
}<br />
|
|
<br />
|
|
<span style="color:#881350;">static</span> <span style="color:#881350;">int</span><br />
|
|
<span style="color:#003369;">begin</span>(cpArbiter *arb, cpSpace *space, <span style="color:#881350;">void</span> *data)<br />
|
|
{<br />
|
|
<span style="color:#236e25;">// Get the cpShapes involved in the collision<br />
|
|
</span> <span style="color:#236e25;">// The order will be the same as you defined in the handler definition<br />
|
|
</span> <span style="color:#236e25;">// a->collision_type will be BULLET_TYPE and b->collision_type will be MONSTER_TYPE<br />
|
|
</span> <span style="color:#003369;">CP_ARBITER_GET_SHAPES</span>(arb, a, b);<br />
|
|
<br />
|
|
<span style="color:#236e25;">// The macro expands exactly as if you had typed this:<br />
|
|
</span> <span style="color:#236e25;">// cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);<br />
|
|
</span> <br />
|
|
<span style="color:#236e25;">// Add a post step callback to safely remove the body and shape from the space.<br />
|
|
</span> <span style="color:#236e25;">// Calling cpSpaceRemove*() directly from a collision handler callback can cause crashes.<br />
|
|
</span> <span style="color:#003369;">cpSpaceAddPostStepCallback</span>(space, (cpPostStepFunc)postStepRemove, b, <span style="color:#881350;">NULL</span>);<br />
|
|
<br />
|
|
<span style="color:#236e25;">// The object is dead, don’t process the collision further<br />
|
|
</span> <span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;<br />
|
|
}<br />
|
|
<br />
|
|
<span style="color:#683821;">#define BULLET_TYPE </span><span style="color:#0000ff;">1</span><span style="color:#683821;"><br />
|
|
#define MONSTER_TYPE </span><span style="color:#0000ff;">2</span><span style="color:#683821;"><br />
|
|
</span><br />
|
|
<span style="color:#236e25;">// Define a collision handler for bullets and monsters<br />
|
|
// Kill the monster by removing it’s shape and body from the space as soon as it’s hit by a bullet <br />
|
|
</span>cpCollisionHandler *handler = <span style="color:#003369;">cpSpaceAddCollisionHandler</span>(space, BULLET_TYPE, MONSTER_TYPE);<br />
|
|
handler->beginFunc = begin;<br />
|
|
<br />
|
|
</div> |