bool RuntimeSpriteObject::ExtraInitializationFromInitialInstance(const gd::InitialInstance & position) { if ( position.floatInfos.find("animation") != position.floatInfos.end() ) SetCurrentAnimation(position.floatInfos.find("animation")->second); return true; }
void WoodEnemyObject::OnCollision(Entity* collision_entity, Type enemy_type, Vector2 offset, Alignment enemy_alignment) { if(enemy_alignment == FRIENDBULLET) { if(enemy_type == WATER) { hitpoints--; } else if(enemy_type == FIRE) { hitpoints -= 3; can_collide = false; if(collision_entity->getDirection().x == 1 && current_animations_name != HITLEFT) SetCurrentAnimation(HITLEFT); else if(collision_entity->getDirection().x == -1 && current_animations_name != HITRIGHT) SetCurrentAnimation(HITRIGHT); } else if(enemy_type == WOOD) { hitpoints -= 2; } } else { } //Death if(hitpoints <= 0) { if(hasCollider()) { delete collider; collider = nullptr; } dead = true; if(current_animations_name != DEATHLEFT) SetCurrentAnimation(DEATHLEFT); current_animation->getSprite()->setPosition(position.x, position.y); } }
void TreasureObject::_LoadState() { if(!_treasure) return; // If the event exists, the treasure has already been opened if(vt_global::GlobalManager->GetGameEvents().DoesEventExist("treasures", _treasure_name)) { SetCurrentAnimation(TREASURE_OPEN_ANIM); _treasure->SetTaken(true); } }
void WoodEnemyObject::Attack() { if(shooting_delay == 0.001f && !created_projectile) { float distanceX = 500; float distanceY = 500; //X distance if(direction.x == 1) { distanceX = fabs((player->GetSprite()->getPosition().x + player->GetSprite()->getTextureRect().width/2) - (collider->position.x + collider->extension.x)); } else if(direction.x == -1) { distanceX = fabs((player->GetSprite()->getPosition().x + player->GetSprite()->getTextureRect().width/2) - collider->position.x); } //Y distance distanceY = fabs((player->getPosition().y + player->GetOffsetY() + (player->getHeight()/2)) - (collider->position.y + (height/2))); if(distanceX < collider->extension.x && !player->IsDead()) { if(distanceY > -(height/2) && distanceY < (height/2)) { created_projectile = true; if(direction.x == 1 && current_animations_name != ATTACKRIGHT) { SetCurrentAnimation(ATTACKRIGHT); } else if(direction.x == -1 && current_animations_name != ATTACKLEFT) { SetCurrentAnimation(ATTACKLEFT); } } } } }
bool RuntimeSpriteObject::ChangeProperty(std::size_t propertyNb, gd::String newValue) { if ( propertyNb == 0 ) { return SetCurrentAnimation(newValue.To<int>()); } else if ( propertyNb == 1 ) { if ( currentAnimation >= GetAnimationsCount() ) return false; return animations[currentAnimation].Get().useMultipleDirections ? SetDirection(newValue.To<std::size_t>()) : SetAngle(newValue.To<float>()); } else if ( propertyNb == 2 ) { return SetSprite(newValue.To<int>()); } else if ( propertyNb == 3 ) { SetOpacity(newValue.To<float>()); } else if ( propertyNb == 4 ) { SetBlendMode(newValue.To<int>()); } else if ( propertyNb == 5 ) {SetScaleX(newValue.To<float>());} else if ( propertyNb == 6 ) {SetScaleY(newValue.To<float>());} return true; }
bool RuntimeSpriteObject::ChangeProperty(unsigned int propertyNb, std::string newValue) { if ( propertyNb == 0 ) { return SetCurrentAnimation(ToInt(newValue)); } else if ( propertyNb == 1 ) { if ( currentAnimation >= GetAnimationsCount() ) return false; return animations[currentAnimation].Get().useMultipleDirections ? SetDirection(ToInt(newValue)) : SetAngle(ToFloat(newValue)); } else if ( propertyNb == 2 ) { return SetSprite(ToInt(newValue)); } else if ( propertyNb == 3 ) { SetOpacity(ToFloat(newValue)); } else if ( propertyNb == 4 ) { SetBlendMode(ToInt(newValue)); } else if ( propertyNb == 5 ) {SetScaleX(ToFloat(newValue));} else if ( propertyNb == 6 ) {SetScaleY(ToFloat(newValue));} return true; }
void TreasureObject::Open() { if(!_treasure) { PRINT_ERROR << "Can't open treasure with invalid treasure content." << std::endl; return; } if(_treasure->IsTaken()) { IF_PRINT_WARNING(MAP_DEBUG) << "attempted to retrieve an already taken treasure: " << _object_id << std::endl; return; } // Test whether events should be triggered if (_events.empty()) _events_triggered = true; SetCurrentAnimation(TREASURE_OPENING_ANIM); _is_opening = true; }
void TreasureObject::Update() { PhysicalObject::Update(); if ((GetCurrentAnimationId() == TREASURE_OPENING_ANIM) && (_animations[TREASURE_OPENING_ANIM].IsAnimationFinished())) SetCurrentAnimation(TREASURE_OPEN_ANIM); if (!_is_opening || GetCurrentAnimationId() != TREASURE_OPEN_ANIM) return; MapMode* mm = MapMode::CurrentInstance(); // Once opened, we handle potential events and the display of the treasure supervisor EventSupervisor* event_manager = mm->GetEventSupervisor(); if (!_events_triggered) { // Trigger potential events after opening for (uint32_t i = 0; i < _events.size(); ++i) { if (!event_manager->IsEventActive(_events[i])) event_manager->StartEvent(_events[i]); } _events_triggered = true; } else if (!_events.empty()) { // Test whether the events have finished std::vector<std::string>::iterator it = _events.begin(); for (; it != _events.end();) { // Once the event has finished, we forget it if (!event_manager->IsEventActive(*it)) it = _events.erase(it); else ++it; } } else { // Once all events are finished, we can open the treasure supervisor mm->GetTreasureSupervisor()->Initialize(this); // Add an event to the treasures group indicating that the treasure has now been opened vt_global::GlobalManager->GetGameEvents().SetEventValue("treasures", _treasure_name, 1); // End the opening sequence _is_opening = false; } }
//--------------------------AI FUNCTIONS--------------------------------// void WoodEnemyObject::Movement(float Deltatime) { float deltaY, deltaX, distance; if(Intercept) { deltaY = (position.y + (current_animation->getSprite()->getTextureRect().height - height/2)) - (player->getPosition().y + player->GetOffsetY() + player->getHeight()/2); deltaX = (position.x + (current_animation->getSprite()->getTextureRect().width/2)) - (player->getPosition().x + player->GetCurrentAnimation()->getSprite()->getTextureRect().width + (collider->extension.x*3)); if(deltaY < player->getHeight()) { if(deltaX < collider->extension.x*3) Intercept = false; else if(deltaX > -(collider->extension.x*3)) Intercept = false; } if(deltaY > -(player->getHeight())) { if(deltaX < collider->extension.x*3) Intercept = false; else if(deltaX > -(collider->extension.x*3)) Intercept = false; } } else { deltaY = (position.y + (current_animation->getSprite()->getTextureRect().height - height/2)) - //Middle of wood enemy's hitbox y (player->getPosition().y + player->GetOffsetY() + player->getHeight()/2); //Middle of players hitbox y deltaX = (position.x + current_animation->getSprite()->getTextureRect().width/2) - //Middle of wood enemy's x (player->getPosition().x + player->GetCurrentAnimation()->getSprite()->getTextureRect().width/2); //Middle of players x if(deltaY > player->getHeight()/2) { if(deltaX > collider->extension.x*3) Intercept = true; else if(deltaX < -(collider->extension.x*3)) Intercept = true; } } distance = sqrtf((deltaY*deltaY)/3+deltaX*deltaX) * 3; //Set the direction if(deltaX > 0) direction.x = -1; else if(deltaX < 0) direction.x = 1; if(deltaY > 0) direction.y = -1; else if(deltaY < 0) direction.y = 1; velocity=Vector2((deltaX/distance)*-(speed * 3 * Deltatime), (deltaY/distance)*-((speed*4) * Deltatime)); if(distance <= collider->extension.x) { //HIT velocity =Vector2(0, 0); if(direction.x == -1 && current_animations_name != IDLELEFT) { SetCurrentAnimation(IDLELEFT); direction.y = 0; } else if(direction.x == 1 && current_animations_name != IDLERIGHT) { SetCurrentAnimation(IDLERIGHT); direction.y = 0; } } else if(direction.x == -1 && current_animations_name != WALKLEFT) { SetCurrentAnimation(WALKLEFT); } else if(direction.x == 1 && current_animations_name != WALKRIGHT) SetCurrentAnimation(WALKRIGHT); position += velocity; }
void PlayerObject::Update(float deltatime) { changed_element = false; destroy_fire = 0; destroy_water = 0; destroy_wood = 0; add_fire = 0; add_water = 0; add_wood = 0; add_element = false; current_animation->Update(deltatime); add_element = false; if(!dead) { //collision timer if(!can_collide) { collision_refresh_timer += deltatime; position.x += deltatime * knockback_speed * collision_direction.x; position.y += deltatime * knockback_speed * collision_direction.y; //can collide again if(collision_refresh_timer > knockback_time) { can_collide = true; collision_refresh_timer = 0.0f; } } else { //Shooting if(created_projectile) { shooting_delay += deltatime; } if(shooting_delay == 0.001f || shooting_delay > 0.2) { //If you are not shooting, then you are able to move Movement(deltatime); //If you are not shooting, then you are able to use souls Souls(); //Elemental swap if(sf::Keyboard::isKeyPressed(sf::Keyboard::R) && !element_changed && CanChangeElement()) { element_changed = true; NextElement(); } if(element_changed) { element_changed_delay += deltatime; if(element_changed_delay > 0.5f) { element_changed = false; element_changed_delay = 0.0f; } } } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && !created_projectile) { create_projectile = true; created_projectile = true; if(direction.x == 1) { SetCurrentAnimation(ATTACKRIGHT); } else { SetCurrentAnimation(ATTACKLEFT); } } else { create_projectile = false; } if(shooting_delay > delay) { shooting_delay = 0.001f; created_projectile = false; } //end of shooting //timern till lost souls if(used_lost_souls) { lost_souls_counter += deltatime; if(lost_souls_counter > 0.3) { used_lost_souls = false; lost_souls_counter = 0.0f; } } } } //death else { if(collider != nullptr) { delete collider; collider = nullptr; } death_animation_time += deltatime; if(death_animation_time > current_animation->GetNumberOfFrames() * current_animation->GetFrameDuration()) { flagged_for_death = true; } } //Lastly update the collider and the sprites position if(hasCollider()) { collider->position.x = position.x + entity_offset_x; collider->position.y = position.y + entity_offset_y; hitbox.setPosition(sf::Vector2f(collider->position.x, collider->position.y)); } current_animation->getSprite()->setPosition(position.x, position.y); }
void PlayerObject::Movement(float deltatime) { //Vertical movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { //if(!created_projectile) //{ if(direction.x == 1) { SetCurrentAnimation(WALKRIGHT); } else { SetCurrentAnimation(WALKLEFT); } //} position.y -= speed*deltatime; direction.y = -1; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { //if(!created_projectile) //{ if(direction.x == 1) { SetCurrentAnimation(WALKRIGHT); } else { SetCurrentAnimation(WALKLEFT); } //} position.y += speed*deltatime; direction.y = 1; } //horizontal movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { //if(!created_projectile) //{ SetCurrentAnimation(WALKLEFT); //} position.x -= speed*deltatime; direction.x = -1; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { //if(!created_projectile) //{ SetCurrentAnimation(WALKRIGHT); //} position.x += speed*deltatime; direction.x = 1; } else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::W) && !sf::Keyboard::isKeyPressed(sf::Keyboard::S) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { if(direction.x == 1) { SetCurrentAnimation(IDLERIGHT); direction.y = 0; } else { SetCurrentAnimation(IDLELEFT); direction.y = 0; } } }
void PlayerObject::NextElement() { changed_element = true; switch(type) { case FIRE: if(water_elements > 0) { type = WATER; arrow = WATER; } else if(wood_elements > 0) { type = WOOD; arrow = WOOD; } else { dead = true; if(direction.x == 1) { SetCurrentAnimation(DEATHRIGHT); } else { SetCurrentAnimation(DEATHLEFT); } } break; case WATER: if(wood_elements > 0) { type = WOOD; arrow = WOOD; } else if(fire_elements > 0) { type = FIRE; arrow = FIRE; } else { dead = true; if(direction.x == 1) { SetCurrentAnimation(DEATHRIGHT); } else { SetCurrentAnimation(DEATHLEFT); } } break; case WOOD: if(fire_elements > 0) { type = FIRE; arrow = FIRE; } else if(water_elements > 0) { type = WATER; arrow = WATER; } else { dead = true; if(direction.x == 1) { SetCurrentAnimation(DEATHRIGHT); } else { SetCurrentAnimation(DEATHLEFT); } } break; } }