From e43c75060f39036ce916defa2191c38f2cac77a9 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sun, 12 Nov 2017 20:19:10 +0000 Subject: [PATCH] [Change] Improved mouse control. [Add] Added a few shiptypes. Don't yell at me for pathetic names. --- src/main.cpp | 4 ++-- src/player.cpp | 23 ++++++++++++++++------- src/player.h | 3 ++- src/ship.cpp | 7 ++++--- src/ship.h | 2 +- src/ship_type.cpp | 30 +++++++++++++++++++++++++++++- src/ship_type.h | 6 ++++-- 7 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f58d745..2ed9c84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -174,14 +174,14 @@ void L3D::MainLoop(void) { earth_frame->SetPosition(vector3d(149598000000.0, 0, 0)); earth_frame->SetRadius(2*380000000); /* 2 moon orbital radii. */ - player = new Player(); + player = new Player(ShipType::SLEEK); player->SetLabel("Me"); player->SetFrame(earth_frame); player->SetPosition(vector3d(100, 0, 0)); Space::AddBody(player); for(int i = 0; i < 4; i++) { - Ship* body = new Ship(); + Ship* body = new Ship(ShipType::LADYBIRD); char buf[64]; snprintf(buf, sizeof(buf), "X%c-0%02d", "A"+i, i); body->SetLabel(buf); diff --git a/src/player.cpp b/src/player.cpp index 502826c..e123cab 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -8,9 +8,10 @@ #define DEG_2_RAD 0.0174532925 -Player::Player(void) : Ship() { +Player::Player(ShipType::Type shipType) : Ship(shipType) { m_external_view_rotx = m_external_view_roty = 0; m_external_view_dist = 200; + m_mouseCMov[0] = m_mouseCMov[1] = 0; } void Player::Render(const Frame* camFrame) { @@ -49,7 +50,8 @@ void Player::ApplyExternalViewRotation(void) { glRotatef(-m_external_view_roty, 0, 1, 0); } -#define MOUSE_ACCEL 400 +#define MOUSE_CTRL_AREA 10.0f +#define MOUSE_RESTITUTION 0.9f void Player::AITurn(void) { int mouseMotion[2]; @@ -63,10 +65,18 @@ void Player::AITurn(void) { if(time_step == 0) return; if(GetDockedWith()) return; - L3D::GetMouseMotion(mouseMotion); float mx, my; - mx = -mouseMotion[0]*time_step*MOUSE_ACCEL; - my = mouseMotion[1]*time_step*MOUSE_ACCEL; + + { + float restitution = powf(MOUSE_RESTITUTION, time_step); + L3D::GetMouseMotion(mouseMotion); + m_mouseCMov[0] += mouseMotion[0]; + m_mouseCMov[1] += mouseMotion[1]; + m_mouseCMov[0] = CLAMP(m_mouseCMov[0]*restitution, -MOUSE_CTRL_AREA, MOUSE_CTRL_AREA); + m_mouseCMov[1] = CLAMP(m_mouseCMov[1]*restitution, -MOUSE_CTRL_AREA, MOUSE_CTRL_AREA); + mx = -m_mouseCMov[0] / MOUSE_CTRL_AREA; + my = m_mouseCMov[1] / MOUSE_CTRL_AREA; + } ClearThrusterState(); if(L3D::KeyState(SDLK_w)) SetThrusterState(ShipType::THRUSTER_REAR, 1.0f); @@ -87,11 +97,10 @@ void Player::AITurn(void) { */ mx /= ts2; my /= ts2; - if(L3D::MouseButtonState(3) && (mouseMotion[0] || mouseMotion[1])) { + if(L3D::MouseButtonState(3)) { SetAngThrusterState(1, mx); SetAngThrusterState(0, my); } else if(L3D::GetCamType() != L3D::CAM_EXTERNAL) { - float tq = 100/ts2; float ax = 0; float ay = 0; if(L3D::KeyState(SDLK_LEFT)) ay += 1; diff --git a/src/player.h b/src/player.h index fd8ab38..e546c5b 100644 --- a/src/player.h +++ b/src/player.h @@ -4,7 +4,7 @@ class Player : public Ship { public: - Player(void); + Player(ShipType::Type shipType); virtual void AITurn(); virtual void Render(const Frame* camFrame); void DrawHUD(const Frame* cam_frame); @@ -12,6 +12,7 @@ public: vector3d GetExternalViewTranslation(void); void ApplyExternalViewRotation(void); private: + float m_mouseCMov[2]; float m_external_view_rotx, m_external_view_roty; float m_external_view_dist; }; diff --git a/src/ship.cpp b/src/ship.cpp index 27aec9f..0fcef19 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -6,10 +6,10 @@ #include "sbre/sbre.h" #include "space.h" -Ship::Ship(void) : RigidBody() { +Ship::Ship(ShipType::Type shipType) : RigidBody() { m_dockedWith = 0; m_mesh = 0; - m_shipType = ShipType::COBRA3; + m_shipType = shipType; m_angThrusters[0] = m_angThrusters[1] = m_angThrusters[2] = 0; m_laserCollisionObj.owner = this; for(int i = 0; i < ShipType::GUNMOUNT_MAX; i++) { @@ -127,6 +127,7 @@ static ObjParams params = { }; void Ship::Render(const Frame* camFrame) { + const ShipType& stype = GetShipType(); params.angthrust[0] = m_angThrusters[0]; params.angthrust[1] = m_angThrusters[1]; params.angthrust[2] = m_angThrusters[2]; @@ -135,6 +136,6 @@ void Ship::Render(const Frame* camFrame) { params.linthrust[2] = m_thrusters[ShipType::THRUSTER_REAR] - m_thrusters[ShipType::THRUSTER_FRONT]; strncpy(params.pText[0], GetLabel().c_str(), sizeof(params.pText)); - RenderSbreModel(camFrame, 10, ¶ms); + RenderSbreModel(camFrame, stype.sbreModel, ¶ms); } diff --git a/src/ship.h b/src/ship.h index a399978..bb1a581 100644 --- a/src/ship.h +++ b/src/ship.h @@ -7,7 +7,7 @@ class SpaceStation; class Ship : public RigidBody { public: - Ship(void); + Ship(ShipType::Type shipType); virtual void AITurn(void); virtual Object::Type GetType(void) { return Object::SHIP; } virtual void SetDockedWith(SpaceStation*); diff --git a/src/ship_type.cpp b/src/ship_type.cpp index d316162..ffa757a 100644 --- a/src/ship_type.cpp +++ b/src/ship_type.cpp @@ -1,7 +1,35 @@ #include "ship_type.h" -const ShipType ShipType::types[1] = { +const ShipType ShipType::types[] = { { + /* + * Besides running a wicked corporatist regime in the sirius system, + * Sirius corporation make a range of lovely ships! + */ + "Sirius Interdictor", 10, + { 250, -250, 50, -50, -50, 50 }, + 700.0, + { + { vector3f(0, -0.5, 0), vector3f(0, 0, -1) }, + { vector3f(0, 0, 0), vector3f(0, 0, 1) } + } + }, + { + /* + * I should outsource name choosing, or this happens.. + */ + "Ladybird Starfighter", + 13, + { 250, -250, 50, -50, -50, 50 }, + 500.0, + { + { vector3f(0, -0.5, 0), vector3f(0, 0, -1) }, + { vector3f(0, 0, 0), vector3f(0, 0, 1) } + } + }, + { + "Flowerfairy Heavy Trader", + 14, { 250, -250, 50, -50, -50, 50 }, 500.0, { diff --git a/src/ship_type.h b/src/ship_type.h index 73a40de..b62b447 100644 --- a/src/ship_type.h +++ b/src/ship_type.h @@ -6,10 +6,12 @@ struct ShipType { public: enum Thruster { THRUSTER_FRONT, THRUSTER_REAR, THRUSTER_TOP, THRUSTER_BOTTOM, THRUSTER_LEFT, THRUSTER_RIGHT, THRUSTER_MAX }; - enum Type { COBRA3 }; + enum Type { SLEEK, LADYBIRD, FLOWERFAIRY }; enum { GUNMOUNT_MAX = 2 }; /*******************************/ + const char* name; + int sbreModel; float linThrust[THRUSTER_MAX]; float angThrust; struct GunMount { @@ -18,6 +20,6 @@ public: } gunMount[GUNMOUNT_MAX]; /*******************************/ - static const ShipType types[1]; + static const ShipType types[]; };