Exemple #1
0
Enemy* Enemy_createWhiteKnight(Scene* scene, SDL_Point tilePos) {
    Enemy* this = malloc(sizeof(Enemy));
    AnimatedSprite* sprite = AnimatedSprite_create(Sprite_create(TextureCache_get(scene->engine->textureCache, "images/whiteKnight.png")));
    Animation* idleAnimation = Animation_create("idle", true);
    List_pushBack(idleAnimation->frames, Frame_create(0,0, 32, 32, 200));
    List_pushBack(idleAnimation->frames, Frame_create(32,0, 32, 32, 200));
    List_pushBack(idleAnimation->frames, Frame_create(64,0, 32, 32, 200));
    List_pushBack(idleAnimation->frames, Frame_create(32,0, 32, 32, 200));
    List_pushBack(sprite->animations, idleAnimation);

    Animation* attack1PrepareAnimation = Animation_create(ANIMATION_PREPARE_ATTACK1, false);
    List_pushBack(attack1PrepareAnimation->frames, Frame_create(0, 32, 32, 32, 60));
    List_pushBack(attack1PrepareAnimation->frames, Frame_create(32,32, 32, 32, 60));
    List_pushBack(attack1PrepareAnimation->frames, Frame_create(0, 32, 32, 32, 60));
    List_pushBack(sprite->animations, attack1PrepareAnimation);

    Animation* attack1Animation = Animation_create(ANIMATION_ATTACK1, false);
    List_pushBack(attack1Animation->frames, Frame_create(64,32, 36, 32, 120));
    List_pushBack(sprite->animations, attack1Animation);

    AnimatedSprite_setFrame(sprite, ((Frame*)idleAnimation->frames->first->data)->rect);
    sprite->progress.animation = idleAnimation;

    sprite->sprite->flip = false;

    Entity* entity = Entity_create(this, scene, sprite);
    entity->draw = Enemy_draw;
    entity->update = Enemy_update;
    entity->destroy = Enemy_destroy;

    entity->physics.bounds.x = tilePos.x * TILE_W * PHYSICS_SCALE;
    entity->physics.bounds.y = tilePos.y * TILE_H * PHYSICS_SCALE;
    entity->physics.bounds.w = 15 * PHYSICS_SCALE; // Make sure Mr. Fatty get's though those slim trapdoors
    entity->physics.bounds.h = 15 * PHYSICS_SCALE;
    entity->physics.belongsToGroups = COLLISION_GROUP_ENEMY;
    entity->physics.collidesWithGroupMask = COLLISION_GROUP_TERRAIN | COLLISION_GROUP_PLAYER;
    entity->offset.x = -10;
    entity->offset.y = -17;
    this->entity = entity;

    return this;
}
Healthbar* Healthbar_create(TextureCache* tc) {
	Healthbar* this = malloc(sizeof(Healthbar));
	this->background = Sprite_create(TextureCache_get(tc, "images/healthbarBg.png"));


	AnimatedSprite* sprite = AnimatedSprite_create(Sprite_create(TextureCache_get(tc, "images/healthbar.png")));
	Animation* idleAnimation = Animation_create("idle", true);
	List_pushBack(idleAnimation->frames, Frame_create(0, 0, 32, 128, 3000));
	List_pushBack(idleAnimation->frames, Frame_create(32,0, 32, 128, 3000));
	List_pushBack(idleAnimation->frames, Frame_create(64,0, 32, 128, 3000));
	List_pushBack(idleAnimation->frames, Frame_create(96,0, 32, 128, 3000));
	List_pushBack(idleAnimation->frames, Frame_create(128,0, 32, 128, 3000));
	List_pushBack(sprite->animations, idleAnimation);
	AnimatedSprite_setFrame(sprite, ((Frame*)idleAnimation->frames->first->data)->rect);
	sprite->progress.animation = idleAnimation;
	this->liquid = sprite;
	this->fullHeight = this->liquid->sprite->bounds.h;
	Healthbar_set(this, 100, 100);
	return this;
}
static Animation* _SkeletonJson_readAnimation (SkeletonJson* self, Json* root, SkeletonData *skeletonData) {
	Animation* animation;

	Json* bones = Json_getItem(root, "bones");
	int boneCount = bones ? Json_getSize(bones) : 0;

	Json* slots = Json_getItem(root, "slots");
	int slotCount = slots ? Json_getSize(slots) : 0;

	int timelineCount = 0;
	int i, ii, iii;
	for (i = 0; i < boneCount; ++i)
		timelineCount += Json_getSize(Json_getItemAt(bones, i));
	for (i = 0; i < slotCount; ++i)
		timelineCount += Json_getSize(Json_getItemAt(slots, i));
	animation = Animation_create(root->name, timelineCount);
	animation->timelineCount = 0;
	skeletonData->animations[skeletonData->animationCount] = animation;
	skeletonData->animationCount++;

	for (i = 0; i < boneCount; ++i) {
		timelineCount = 0;
		Json* boneMap = Json_getItemAt(bones, i);

		const char* boneName = boneMap->name;

		int boneIndex = SkeletonData_findBoneIndex(skeletonData, boneName);
		if (boneIndex == -1) {
			Animation_dispose(animation);
			_SkeletonJson_setError(self, root, "Bone not found: ", boneName);
			return 0;
		}

		timelineCount = Json_getSize(boneMap);
		for (ii = 0; ii < timelineCount; ++ii) {
			float duration;
			Json* timelineArray = Json_getItemAt(boneMap, ii);
			int frameCount = Json_getSize(timelineArray);
			const char* timelineType = timelineArray->name;

			if (kdStrcmp(timelineType, "rotate") == 0) {
				
				RotateTimeline *timeline = RotateTimeline_create(frameCount);
				timeline->boneIndex = boneIndex;
				for (iii = 0; iii < frameCount; ++iii) {
					Json* frame = Json_getItemAt(timelineArray, iii);
					RotateTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0), Json_getFloat(frame, "angle", 0));
					readCurve(SUPER(timeline), iii, frame);
				}
				animation->timelines[animation->timelineCount++] = (Timeline*)timeline;
				duration = timeline->frames[frameCount * 2 - 2];
				if (duration > animation->duration) animation->duration = duration;

			} else {
				int isScale = kdStrcmp(timelineType, "scale") == 0;
				if (isScale || kdStrcmp(timelineType, "translate") == 0) {
					float scale = isScale ? 1 : self->scale;
					TranslateTimeline *timeline = isScale ? ScaleTimeline_create(frameCount) : TranslateTimeline_create(frameCount);
					timeline->boneIndex = boneIndex;
					for (iii = 0; iii < frameCount; ++iii) {
						Json* frame = Json_getItemAt(timelineArray, iii);
						TranslateTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0), Json_getFloat(frame, "x", 0) * scale,
								Json_getFloat(frame, "y", 0) * scale);
						readCurve(SUPER(timeline), iii, frame);
					}
					animation->timelines[animation->timelineCount++] = (Timeline*)timeline;
					duration = timeline->frames[frameCount * 3 - 3];
					if (duration > animation->duration) animation->duration = duration;
				} else {
					Animation_dispose(animation);
					_SkeletonJson_setError(self, 0, "Invalid timeline type for a bone: ", timelineType);
					return 0;
				}
			}
		}
	}

	for (i = 0; i < slotCount; ++i) {
        timelineCount = 0;
		Json* slotMap = Json_getItemAt(slots, i);
		const char* slotName = slotMap->name;

		int slotIndex = SkeletonData_findSlotIndex(skeletonData, slotName);
		if (slotIndex == -1) {
			Animation_dispose(animation);
			_SkeletonJson_setError(self, root, "Slot not found: ", slotName);
			return 0;
		}

		timelineCount = Json_getSize(slotMap);
		for (ii = 0; ii < timelineCount; ++ii) {
			float duration;
			Json* timelineArray = Json_getItemAt(slotMap, ii);
			int frameCount = Json_getSize(timelineArray);
			const char* timelineType = timelineArray->name;

			if (kdStrcmp(timelineType, "color") == 0) {
				ColorTimeline *timeline = ColorTimeline_create(frameCount);
				timeline->slotIndex = slotIndex;
				for (iii = 0; iii < frameCount; ++iii) {
					Json* frame = Json_getItemAt(timelineArray, iii);
					const char* s = Json_getString(frame, "color", 0);
					ColorTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0), toColor(s, 0), toColor(s, 1), toColor(s, 2),
							toColor(s, 3));
					readCurve(SUPER(timeline), iii, frame);
				}
				animation->timelines[animation->timelineCount++] = (Timeline*)timeline;
				duration = timeline->frames[frameCount * 5 - 5];
				if (duration > animation->duration) animation->duration = duration;

			} else if (kdStrcmp(timelineType, "attachment") == 0) {
				AttachmentTimeline *timeline = AttachmentTimeline_create(frameCount);
				timeline->slotIndex = slotIndex;
				for (iii = 0; iii < frameCount; ++iii) {
					Json* frame = Json_getItemAt(timelineArray, iii);
					Json* name = Json_getItem(frame, "name");
					AttachmentTimeline_setFrame(timeline, iii, Json_getFloat(frame, "time", 0),
							name->type == Json_NULL ? 0 : name->valuestring);
				}
				animation->timelines[animation->timelineCount++] = (Timeline*)timeline;
				duration = timeline->frames[frameCount - 1];
				if (duration > animation->duration) animation->duration = duration;

			} else {
				Animation_dispose(animation);
				_SkeletonJson_setError(self, 0, "Invalid timeline type for a slot: ", timelineType);
				return 0;
			}
		}
	}

	return animation;
}