/******************************************************************************/
#include "stdafx.h"
#include "../../../../../data/enum/_enums.h"
/******************************************************************************/
Camera desired_camera[2]; // create a helper desired camera
/******************************************************************************/
STRUCT(Player , Game::Chr)
//{
virtual Bool update();
};
/******************************************************************************/
class ViewPort:public GuiObjs
{
static void Exit (ViewPort &v) {v.hide();}
public:
Viewport v;
MeshPtr mesh,armor,pants,boots,gauntlets;
CSkeleton cskel; // controlled skeleton
ViewPort(){}
static void Render();
static void ViewportDraw(Viewport &viewport);
void create();
void update(int i=0);
void draw();
}viewPort;
/******************************************************************************/
void ViewPort::Render()
{
switch(Renderer())
{
case RM_PREPARE:
{
viewPort.mesh->draw(viewPort.cskel);
viewPort.armor->draw(viewPort.cskel);
viewPort.pants->draw(viewPort.cskel);
viewPort.boots->draw(viewPort.cskel);
viewPort.gauntlets->draw(viewPort.cskel);
}break;
}
}
void ViewPort::ViewportDraw(Viewport &viewport) // function wich will be called when drawing the Gui Viewport element
{
Int camera_index=Mid((UIntPtr)viewport.user,0,Elms(desired_camera)-1);
desired_camera[camera_index].set();
Renderer(Render);
SetMatrix(MatrixIdentity);
}
void ViewPort::create()
{
load("gui/obj/viewPort.gobj");
Gui+=T;
Window &window=getWindow("w").asWindow();
window+=v.create((&getViewport("v"))->rect,ViewportDraw,(Ptr)1);
mesh="obj/chr/warrior/body.mesh";
armor="obj/chr/warrior/armor.mesh";
pants="obj/chr/warrior/pants.mesh";
boots="obj/chr/warrior/boot.mesh";
gauntlets="obj/chr/warrior/gauntlet.mesh";
cskel.create(Skeletons("obj/chr/warrior/body.skel"));
}
void ViewPort::update(int i)
{
cskel.clear().animate(L"../data/anim/walk.anim",Time.time()).updateMatrix(MatrixIdentity).updateVelocities();
Camera *cam=NULL; // start with none
if(i) cam=&desired_camera[1];
if(cam) // if found a camera
{
if(Ms.b(0)) // only when mouse button pressed
{
cam->yaw-=Ms.dc().x;
cam->pitch+=Ms.dc().y;
}
if(Ms.wheel()<0)cam->dist*=1.2f;
if(Ms.wheel()>0)cam->dist/=1.2f;
cam->setSpherical(); // apply changes by calling 'setSpherical' which sets camera matrix from current values
}
desired_camera[1].updateVelocities();
}
/******************************************************************************/
Game::ObjMemx<Game::Static> Statics;
Game::ObjMemx<Player> Players;
/******************************************************************************/
Bool Player::update()
{
if(action)
{
if(Kb.b(KB_W) || Kb.b(KB_S) || Kb.b(KB_A) || Kb.b(KB_D) || Kb.b(KB_Q) || Kb.b(KB_E))actionBreak();
}
if(!action)
{
input.turn.x=Kb.b(KB_Q)-Kb.b(KB_E);
input.turn.y=Kb.b(KB_T)-Kb.b(KB_G);
input.move.x=Kb.b(KB_D)-Kb.b(KB_A);
input.move.z=Kb.b(KB_W)-Kb.b(KB_S);
input.move.y=Kb.b(KB_SPACE)-Kb.b(KB_LSHIFT);
input.dodge = Kb.bd(KB_D)-Kb.bd(KB_A);
input.crouch= Kb.b (KB_LSHIFT);
input.walk = Kb.b (KB_LCTRL );
input.jump =(Kb.bp(KB_SPACE ) ? 3.5f : 0);
}
return super::update();
}
/******************************************************************************/
void InitPre()
{
App.name("Camera Collisions");
App.flag=APP_FULL_TOGGLE;
DataPath("../data");
Paks.add("engine.pak");
D.full(false).sync(true).ambPower(0.3f).hpRt(true);
}
/******************************************************************************/
Bool Init()
{
Physics.create(CSS_NONE,true,"../Installation/PhysX");
Sky.atmospheric();
Sun.image=Images("gfx/sky/sun.gfx"); Sun.light_color=1-D.ambColor();
Game::World.init()
.setObjType(Statics,OBJ_STATIC)
.setObjType(Players,OBJ_PLAYER)
.New("world/apple.world");
viewPort.create();
return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
Bool Update()
{
if(Kb.bp(KB_ESC))return false;
if(Gui.msLit()==&viewPort.v)
viewPort.update(1);
else{
Game::World.update(desired_camera[0].at);
desired_camera[0].yaw-=Ms.d().x; // update camera yaw angle according to mouse delta x
desired_camera[0].pitch+=Ms.d().y; // update camera pitch angle according to mouse delta y
Clamp(desired_camera[0].pitch,-PI_2,PI_4); // clamp to possible camera pitch angle
desired_camera[0].dist=Max(1.0f,desired_camera[0].dist*ScaleFactor(Ms.wheel()*-0.2f)); // update camera distance according to mouse wheel
desired_camera[0].at=Players[0].pos();
desired_camera[0].setSpherical(); // set as spherical from current values, this will set the camera's matrix (desired_camera.matrix)
Ball ball(0.1f, desired_camera[0].at); // we place it at starting point (where the camera is looking at)
Physics.move(ball, desired_camera[0].matrix.pos-ball.pos); // use physics movement to move the ball as far as it can go without any collisions
Cam.setPosDir(ball.pos, desired_camera[0].matrix.z, desired_camera[0].matrix.y); // we'll use 'desired_camera.matrix' directions which were set in 'setSpherical' camera method
Cam.updateVelocities().set(); // update camera velocities and activate it
}
Gui.update();
viewPort.update();
return true;
}
/******************************************************************************/
void Render()
{
Game::World.draw();
}
void Draw()
{
Renderer(Render);
Gui.draw();
if(Gui.msLit()==&viewPort.v)
D.text(0,0.9f,S+"Camera "+1);
else
D.text(0,0.9f,S+"Camera "+0);
}
/******************************************************************************/