-
entry
1 -
comment
1 -
views
1,631
About this blog
This is my first blog and I will make a First Player Minigame to have all the stuff I need for a bigger game.
I hope I can use most of them with Leadwerks5 later, but it's allways a good training.
Since 3 years I have not programmed anything with Leadwerks, that has nothing to do with Leadwerks it was a real life thing.
So I must learn the stuff from the beginning. My natural language is not English, I hope you can forgive me a mistake or two .
If you want test the code you make a new project and a map. I made some test objects to see if climbing fails or not and under which steps I can crouch
and on which step I can jump.
I make this blog for me to save the source code. Two times in my life I lost nearly all my source I programmed by HardDrive crashes.
So it is a nice thing to have that online. And for beginnes to have a start code.
In your project folder, by me in "documents/Leadwerks/Projects/.... <-- here are my Projects"
you have a subfolder also named projects:
"documents/Leadwerks/Projects/ProjectName/projects/"
by me in the folder windows is the VisualStudio 17 project: "ProjectName.sln" << Start this with Doubleclick, but first install VisualStudio.
I delete all the stuff inside main.cpp, App.cpp and app.h
and put the following code in main.cpp
change your mapname > "YouMapName.map" from your project and compile. I can not say whats the menu names in english because I have the german GUI.
For VisualStudio tutorials look in the Web or YouTube, I am sure there are dozens of them.
#include "Leadwerks.h" #include <string> using namespace Leadwerks; int screenwidth = GetSystemMetrics(0); int screenheight = GetSystemMetrics(1); Window* window; Context* context; World* world; Model* thePlayer; Camera* theCam; // declare functions that we need, they are used once so I made them inline for faster code. inline void Start(); inline void loop(); inline void DrawInfo(); //crouch, crawl, jog, run // used for mouse handling Vec3 mausdata = Vec3(0, 0, 0); float mausx = 0.0; float mausy = 0.0; // used for player movement Vec3 mdat = Vec3(0, 0, 0); Vec3 ppos = Vec3(0, 0, 0); Vec3 velo = Vec3(0, 0, 0); float walk = 0.0; float strafe = 0.0; float jump = 0.0; float crouch = 0.0; float strafespeed = 2.0; float movespeed = 2.4; float jumpspeed = 5.0; bool cdown = 0; bool sdown = 0; // used for mainloop bool mainloop = true; // mainfunction int main(int argc,const char *argv[]) { // hold the mainfuntion small and insert what we need with an inline function. Start(); // mainloop while (mainloop) { loop(); // Here is the most mailoop stuff if (window->Closed() || window->KeyDown(Key::Escape)) mainloop = false; //Quit if ESC is pressed Leadwerks::Time::Update(); world->Update(); world->Render(); // For holding the mainloop small put all drawing stuff inside an inline function. DrawInfo(); context->Sync(true); } return 0; } // Inline functions we use only once. inline void DrawInfo() { // For the first, all the drawing stuff in this funtion. context->SetBlendMode(Blend::Alpha); context->SetColor(1.0, 1.0, 1.0); context->DrawText("Camera Rotation: " + theCam->GetRotation().ToString(), 2, 2); context->DrawText("Player Position: " + thePlayer->GetPosition().ToString(), 2, 40); context->DrawText("Player Position: " + std::to_string(ppos[0]), 2, 70); context->DrawText("Player Position: " + std::to_string(ppos[1]), 2, 100); context->DrawText("Player Position: " + std::to_string(ppos[2]), 2, 130); context->DrawText("UPS: " + String(Time::UPS()), 2, 160); context->DrawText("Velocity: " + thePlayer->GetVelocity().ToString(), 2, 190); // a way to check if the player is not on ground, for possibly later use. if (thePlayer->GetAirborne()) { context->DrawText("In the air", 2, 220); } context->SetBlendMode(Blend::Solid); } inline void Start() { // Window //window = Window::Create("",0,0, 2560, 1440, Window::Fullscreen); //window = Window::Create("", 0, 0, screenwidth, screenheight, Window::Fullscreen); window = Window::Create("", 0, 0, screenwidth, screenheight); context = Context::Create(window); window->Maximize(); // The world world = World::Create(); // The player that we need for "First Player Sight" thePlayer = Model::Create(); thePlayer->SetPhysicsMode(Entity::CharacterPhysics); thePlayer->SetPosition(0, 5, 0); thePlayer->SetMass(1); // Put the camera as a child to the player, so we can move and look up and down separately theCam = Camera::Create(thePlayer); theCam->SetPosition(0,1.65,0); // Font we use for drawing operations Font* font = Font::Load("Fonts/Arial.ttf", 12); context->SetFont(font); // Map //Map::Load("Maps/TheWorld.map"); Map::Load("Maps/BetonMap.map"); // <<<<----- put here your mapname ............................................. } inline void loop() { ppos = thePlayer->GetPosition(); mausdata = window->GetMousePosition(); mausx += (mausdata[0] - window->GetWidth() / 2) * 0.1; mausy += (mausdata[1] - window->GetHeight() / 2) * 0.1; window->SetMousePosition(window->GetWidth() / 2, window->GetHeight() / 2); // not Shift: walk if (!window->KeyDown(Key::Shift) && sdown == 1) { sdown = 0; movespeed = 2.4; } //Shift: run if (window->KeyDown(Key::Shift) && sdown == 0) { sdown = 1; movespeed = 5.0; } // not CTRL: stay if (!window->KeyDown(Key::ControlKey) && cdown == 1) { cdown = 0; movespeed = 3.0; ppos = thePlayer->GetPosition(); theCam->SetPosition(0,1.65, 0); crouch = 0; } // CTRL: crouch if (window->KeyDown(Key::ControlKey) && cdown == 0) { cdown = 1; movespeed = 1.0; crouch = 1; ppos = thePlayer->GetPosition(); theCam->SetPosition(0,0.95,0); } // Walk with movespeed when press Key W walk = (window->KeyDown(Key::W) - window->KeyDown(Key::S)) * movespeed; strafe = (window->KeyDown(Key::D) - window->KeyDown(Key::A)) * strafespeed; // check if player is on ground, this means velo[1] = "y direction" has no velocity = 0 // in this way the player kann not jump if he is already in the air. velo = thePlayer->GetVelocity(); if (velo[1] == 0.0) { jump = window->KeyHit(Key::Space) * jumpspeed; } else { jump = window->KeyHit(Key::Space) * 0.0; } if (mausy > 50.0) mausy = 50.0; if (mausy < -90.0 ) mausy = -90.0; thePlayer->SetInput(mausx, walk, strafe, jump, crouch); //void SetInput(float angle, float move, float strafe = 0, float jump = 0, const bool crouch = false, const float maxaccel = 1, const float maxdecel = 0.5, const bool detailed = false, const float maxrotationspeed = 5.0) theCam->SetRotation(mausy, 0, 0); mdat = theCam->GetRotation(); }
The next step I will made is to put the code into a class.
Use the C Key for toggle crouching and use the CTRL for faster runnig, the I have walk with Key W, jogging with Shift+W and running with CTRL and W.