void Entity::Cleanup() { if(hasCollider()) { delete collider; collider = nullptr; } }
void GameObject::setPosition(sf::Vector2f& _pos) { m_pos = _pos; if (hasSprite()) m_sprite->setPosition(m_pos); if ( hasCollider()) m_collider->m_pos = m_pos; }
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); } }
GameObject::~GameObject() { if (m_collider != nullptr) { if (hasCollider()) { delete m_collider; m_collider = nullptr; } } if (m_sprite != nullptr) { if (hasSprite()) { delete m_sprite; m_sprite = nullptr; } } //std::cout << " ~GameObject\n"; }
void WoodEnemyObject::Update(float deltatime) { //Firstly update the animation current_animation->Update(deltatime); //Secondly update the sprites position if there is a collider if(hasCollider()) { current_animation->getSprite()->setPosition(position.x, position.y); collider->position.x = position.x + entity_offset_x; collider->position.y = position.y + entity_offset_y; } if(!dead) { if(!can_collide) { collision_refresh_timer += deltatime; //can collide again if(collision_refresh_timer > knockback_time) { can_collide = true; collision_refresh_timer = 0.0f; } } else { //Attacking if(created_projectile) { shooting_delay += deltatime; if(current_animation->GetCurrentFrame() == 4 && direction.x == 1) { float extension = 0.0f; extension = collider->extension.x; collider->position.x = (position.x + extension) + extension; } else if(current_animation->GetCurrentFrame() == 4 && direction.x == -1) { if(direction.x == -1) collider->position.x = collider->position.x - collider->extension.x; else if(direction.x == 1) collider->position.x = collider->position.x + collider->extension.x; } } Attack(); if(shooting_delay > current_animation->GetNumberOfFrames() * current_animation->GetFrameDuration()) { shooting_delay = 0.001f; created_projectile = false; } //end of attacking //Movement if(!created_projectile) Movement(deltatime); } } else { death_animation_time += deltatime; if(death_animation_time > current_animation->GetNumberOfFrames() * current_animation->GetFrameDuration()) { flagged_for_death = true; } //drop lost soul delay if(dropped_lostsoul == true) { lostsouldrop_delay += deltatime; } if(lostsouldrop_delay == 0.001f && !dropped_lostsoul && random_number<=2) { drop_lostsoul = true; dropped_lostsoul = true; } else { drop_lostsoul = false; } if(lostsouldrop_delay > lostsoulgoahead_delay) { lostsouldrop_delay = 0.001f; dropped_lostsoul = false; } } if(hasCollider()) { //Change the position of the hitbox hitbox.setPosition(collider->position.x, collider->position.y); } }
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); }