Exemplo n.º 1
0
/**
 * Initializes a new player.
 * The player is placed in the queue of available players.
 */
struct player *player_init(struct player *p, struct game *g, double x, double y,
                           double w, double h, uint32_t score, uint8_t data) {
	cpVect all[4] = {cpv(0,0), cpv(0,h), cpv(w,h), cpv(w,0)};
	cpBody *body = cpBodyInit(&p->body, 10, cpMomentForBox(10, w, h));
	if (!body) {
		ERR_ERRNO();
		return 0;
	}
	cpBodySetPos(body, cpv(x,y));
	//cpShape *shape = cpPolyShapeNew(body,4,all,cpv((p->l+p->r)/2.0,(p->b+p->t)/2.0));
	cpShape *shape = cpPolyShapeInit(&p->shape, body, 4, all, cpv(0, 0));
	if (!shape) {
		ERR_ERRNO();
		cpBodyDestroy(body);
		return 0;
	}
	shape->data = p;
	shape->collision_type = PLAYER;
	if (linkedlist_add_last(g->p_q, p)) {
		ERR_TRACE();
		cpBodyDestroy(body);
		cpShapeDestroy(shape);
		return 0;
	}
	p->x = x;
	p->y = y;
	p->node = g->p_q->last;
	return p;
}
Exemplo n.º 2
0
ETERM *space_delete(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    // remove all subscribers
    for(erlmunk_subscriber *subscriber = s->subscribers; subscriber != NULL;
        subscriber = subscriber->hh.next) {
        space_remove_subscriber(s, subscriber);
    }
    // remove all bodies
    for(erlmunk_body *b = s->bodies; b != NULL;
        b = b->hh.next) {
        erlmunk_body_data *data = cpBodyGetUserData(b->body);
        if (data->term != NULL)
            erl_free_compound(data->term);
        free(data);

        cpSpaceRemoveBody(s->space, b->body);
        cpBodyDestroy(b->body);
        cpBodyFree(b->body);
        space_remove_body_hash(s, b);
    }

    return NULL;
}
Exemplo n.º 3
0
ETERM *space_remove_body(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    // DEBUGF(("removing body #%d\n", body_id));

    // remove the user data associated with the body
    erlmunk_body_data *data = cpBodyGetUserData(b->body);
    if (data->term != NULL)
        erl_free_compound(data->term);
    free(data);

    cpBodyEachShape(b->body, shapeRemove, NULL);
    cpSpaceRemoveBody(s->space, b->body);

    space_remove_body_hash(s, b);

    cpBodyDestroy(b->body);
    cpBodyFree(b->body);

    return NULL;
}
Exemplo n.º 4
0
void WordTreeScene::updatePhysics(Word* word) {
    cpShape* shape = word->getShape();
    if( shape!=NULL ) {
        cpSpaceRemoveShape(ChipmunkManager::getInstance()->getSpace(), shape);
        cpShapeDestroy(shape);
        word->setShape(NULL);
    }
    
    cpBody* body = word->getBody();
    if( body!=NULL ) {
        cpSpaceRemoveBody(ChipmunkManager::getInstance()->getSpace(), body);
        cpBodyDestroy(body);
        word->setBody(NULL);
    }
    
    CCLabelTTF* targetLabel = word->getLabel();
    CCPoint pos = targetLabel->getPosition();
    CCSize contentSize = targetLabel->getContentSize();
    
    // init body
    body = cpBodyNew(1, INFINITY);
    body->p = cpv(targetLabel->getPosition().x, targetLabel->getPosition().y);
    cpSpaceAddBody(ChipmunkManager::getInstance()->getSpace(), body);
    
    float size = MAX(targetLabel->getContentSize().width, targetLabel->getContentSize().height);
    
    shape = cpBoxShapeNew(body, size+size*0.3f, size+size*0.3f);
    cpSpaceAddShape(ChipmunkManager::getInstance()->getSpace(), shape);
    
    word->setBody(body);
    word->setShape(shape);
}
Exemplo n.º 5
0
void
cpBodyFree(cpBody *body)
{
	if(body){
		cpBodyDestroy(body);
		cpfree(body);
	}
}
Exemplo n.º 6
0
void free_body_full(cpBody *body) {
    cpBodyEachShape(body, (cpBodyShapeIteratorFunc) free_shape, NULL);
    
    point_array_free( (Body_data *) cpBodyGetUserData(body) );
	cpSpace *space = cpBodyGetSpace(body);
	cpSpaceRemoveBody(space, body);
	cpBodyDestroy(body);
    cpBodyFree(body);
}
Exemplo n.º 7
0
/**
 * Initialize a bubble that will bounce around. 
 * \param b The bubble to initialize.
 * \param x The x location of the bubble.
 * \param y The y location of the bubble.
 * \param v_x The x velocity of the bubble.
 * \param v_y The y velocity of the bubble.
 * \param r The radius of the bubble.
 * \param l The bubble's life. 
 */
struct bubble *bubble_init(struct bubble *b, double x, double y, double v_x,
                           double v_y, double r, uint8_t l) {
	cpBody *body = cpBodyInit(&b->body, r,
	                          cpMomentForCircle(r, 0, r, cpv(0, 0)));
	if (!body) {
		ERR_ERRNO();
		return 0;
	}
	cpShape *shape = cpCircleShapeInit(&b->shape, body, r, cpv(0,0));
	if (!shape) {
		cpBodyDestroy(body);
		ERR_ERRNO();
		return 0;
	}
	cpBodySetVel(body, cpv(v_x, v_y));
	cpBodySetPos(body, cpv(x, y));
	shape->e = 1.0;
	shape->data = b;
	shape->collision_type = BUBBLE;
	b->l = l;
	return b;
}
Exemplo n.º 8
0
void
cpBodyFree(cpBody *body)
{
	if(body) cpBodyDestroy(body);
	free(body);
}
Exemplo n.º 9
0
/**
 * Free up any internal memory. 
 */
void player_destroy(struct player *p) {
	if (p->client)
		p->client->p = 0;
	cpShapeDestroy((struct cpShape *)&p->shape);
	cpBodyDestroy(&p->body);
}
Exemplo n.º 10
0
/**
 * Frees any internal memory of the bubble.
 */
void bubble_destroy(struct bubble *b) {
	cpShapeDestroy(&b->shape);
	cpBodyDestroy(&b->body);
}