void Ship :: update (WorldInterface& r_world) { if (isUnitAiSet()) { if (!desired_velocity.isZero()) { double theta = max_rotation_rate * TimeSystem::getFrameDuration(); if(theta > 0.0) { rotateTowards(desired_velocity.getNormalized(), theta); } double delta = max_acceleration * TimeSystem::getFrameDuration(); if(delta > 0.0) { double currentSpeed = getSpeed(); double desiredSpeed = desired_velocity.getNorm(); if (currentSpeed < desiredSpeed) { currentSpeed += delta; if (currentSpeed > desiredSpeed) currentSpeed = desiredSpeed; setSpeed(currentSpeed); } else if (currentSpeed > desiredSpeed) { currentSpeed -= delta; if (currentSpeed < desiredSpeed) currentSpeed = desiredSpeed; setSpeed(currentSpeed); } } } if (wantsToFire && isReloaded()) { r_world.addBullet(getPosition(), getForward(), getId()); wantsToFire = false; markReloading(); } } if(isAlive() && isDying()) { r_world.addExplosion(getPositionPrevious(), getRadius() * EXPLOSION_SIZE_FACTOR, 0); m_is_dead = true; // no further updates assert(invariant()); return; } if(isAlive()) { updateBasic(); // moves the Ship m_reload_timer += TimeSystem::getFrameDuration(); } assert(invariant()); }
void LineItem::creationPolygonChanged(View::CreationEvent event) { if (event == View::MousePress) { const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MousePress)); setPos(poly.first().x(), poly.first().y()); setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height())); parentView()->scene()->addItem(this); //setZValue(1); return; } if (event == View::MouseMove) { const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseMove)); if (!rect().isEmpty()) { rotateTowards(line().p2(), poly.last()); } QRectF r = rect(); r.setSize(QSizeF(QLineF(line().p1(), poly.last()).length(), r.height())); setViewRect(r); return; } if (event == View::MouseRelease) { const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseRelease)); parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself parentView()->disconnect(this, SLOT(creationPolygonChanged(View::CreationEvent))); parentView()->setMouseMode(View::Default); maybeReparent(); emit creationComplete(); return; } }
void LineItem::setLine(const QLineF &line_) { setPos(line_.p1()); setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height())); if (!rect().isEmpty()) { rotateTowards(line().p2(), line_.p2()); } QRectF r = rect(); r.setSize(QSizeF(QLineF(line().p1(), line_.p2()).length(), r.height())); setViewRect(r); }
void Ship::update(InputButtons::Bitset& input, GameState& game_state) { static const float TURNING_SPEED = radiansFromDegrees(5.0f); static const vec2 turning_vel = complex_from_angle(TURNING_SPEED); anim_flags.reset(); if (input.test(InputButtons::LEFT)) { rb.angular_vel = complex_conjugate(turning_vel); anim_flags.set(AnimationFlags::THRUST_CCW); } if (input.test(InputButtons::RIGHT)) { rb.angular_vel = turning_vel; anim_flags.set(AnimationFlags::THRUST_CW); } if (input.test(InputButtons::LEFT) == input.test(InputButtons::RIGHT)) { rb.angular_vel = complex_1; Position mouse_pos = game_state.camera.inverse_transform( mvec2(static_cast<float>(game_state.mouse_x), static_cast<float>(game_state.mouse_y))); vec2 mouse_orientation = normalized(mouse_pos - rb.pos); rb.orientation = rotateTowards(rb.orientation, mouse_orientation, TURNING_SPEED); } if (input.test(InputButtons::BRAKE)) { rb.vel = 0.96f * rb.vel; anim_flags.set(AnimationFlags::INERTIAL_BRAKE); } else if (input.test(InputButtons::THRUST)) { vec2 accel = 0.05f * rb.orientation; rb.vel = rb.vel + accel; anim_flags.set(AnimationFlags::THRUST_FORWARD); } if (input.test(InputButtons::SHOOT) && shoot_cooldown == 0) { Bullet bullet; bullet.physp.pos = rb.pos; bullet.orientation = rb.orientation; bullet.physp.vel = rb.vel + 4.0f * rb.orientation; bullet.img = img_bullet; game_state.bullets.push_back(bullet); shoot_cooldown = 5; } if (shoot_cooldown > 0) { --shoot_cooldown; } shield.update(); rb.update(); }