Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void update(float elapsed, bool isFirstPerson)
- {
- if (elapsed > 1.0f / 60.0f)
- elapsed = 1.0f / 60.0f;
- if (Pick)
- {
- float t = 0.0f;
- int xp = 0;
- int yp = 0;
- int zp = 0;
- // Update picking target
- target = updatePickingTarget();
- if (MMy_Physics::GetNearestPickableCube(Position, target, World, t, xp, yp, zp))
- {
- World->deleteCube(xp, yp, zp);
- }
- Pick = false;
- }
- //Par defaut, on applique la gravité (-100 sur Z), la moitie si dans l'eau
- YVec3f force = YVec3f(0, 0, -1) * 9.81f * gravityMultiplier;
- if (InWater)
- force = YVec3f(0, 0, -1) * 0.5f;
- float lastheight = CurrentHeight;
- CurrentHeight = Height;
- if (Crouch)
- CurrentHeight = Height / 2;
- //Pour ne pas s'enfoncer dans le sol en une frame quand on se releve
- if (CurrentHeight > lastheight)
- Position.Z += Height / 4;
- ////Si l'avatar n'est pas au sol, alors il ne peut pas sauter
- //if (!Standing && !InWater) //On jump tout le temps
- // Jump = false;
- float accel = 40;
- if (Crouch)
- accel = 20;
- if (!Standing)
- accel = 5;
- if (Run)
- accel = 80;
- YVec3f forward(Cam->Direction.X, Cam->Direction.Y, 0);
- forward.normalize();
- YVec3f right(Cam->RightVec.X, Cam->RightVec.Y, 0);
- right.normalize();
- //On applique les controles en fonction de l'accélération
- if (avance)
- force += forward * accel;
- if (recule)
- force += forward * -accel;
- if (gauche)
- force += right * -accel;
- if (droite)
- force += right * accel;
- //On applique le jump
- if (Jump)
- {
- force += Cam->UpRef * 15.0f / elapsed; //(impulsion, pas fonction du temps)
- Jump = false;
- }
- //On applique les forces en fonction du temps écoulé
- Speed += force * elapsed;
- //On met une limite a sa vitesse horizontale
- float speedmax = 70;
- if (Crouch)
- speedmax = 45;
- if (!Standing)
- speedmax = 70;
- if (Run)
- speedmax = 140;
- YVec3f horSpeed = Speed;
- horSpeed.Z = 0;
- if (horSpeed.getSize() > speedmax)
- {
- horSpeed.normalize();
- horSpeed *= speedmax;
- Speed.X = horSpeed.X;
- Speed.Y = horSpeed.Y;
- }
- //On le déplace, en sauvegardant son ancienne position
- YVec3f oldPosition = Position;
- Position += (Speed * elapsed);
- //YLog::log(YLog::ENGINE_INFO, ("zS " + toString(Speed.Z)).c_str());
- if (_TimerStanding.getElapsedSeconds() > 0.01)
- Standing = false;
- for (int pass = 0; pass < 2; pass++)
- {
- for (int i = 0; i < 6; i++)
- {
- float valueColMin = 0;
- MWorld::MAxis axis = World->getMinCol(Position, Speed, Width, CurrentHeight, valueColMin, i < 3);
- //YLog::log(YLog::ENGINE_INFO,"check");
- if (axis != 0)
- {
- //valueColMin = nymax(nyabs(valueColMin), 0.0001f) * (valueColMin > 0 ? 1.0f : -1.0f);
- if (axis & MWorld::AXIS_X)
- {
- //YLog::log(YLog::ENGINE_INFO,("x " + toString(valueColMin)).c_str());
- Position.X += valueColMin + 0.001*sign(valueColMin);
- Speed.X = 0;
- }
- if (axis & MWorld::AXIS_Y)
- {
- //YLog::log(YLog::ENGINE_INFO, ("y " + toString(valueColMin)).c_str());
- Position.Y += valueColMin + 0.001*sign(valueColMin);
- Speed.Y = 0;
- }
- if (axis & MWorld::AXIS_Z)
- {
- //YLog::log(YLog::ENGINE_INFO, ("z " + toString(valueColMin)).c_str());
- Speed.Z = 0;
- Position.Z += valueColMin + 0.001*sign(valueColMin);
- Standing = true;
- _TimerStanding.start();
- }
- }
- }
- }
- int x = (int)(Position.X / MCube::CUBE_SIZE);
- int y = (int)(Position.Y / MCube::CUBE_SIZE);
- int z = (int)(Position.Z / MCube::CUBE_SIZE);
- //Escaliers
- float floatheight = 1.0f;
- float zpied = Position.Z - (Height / 2.0f);
- float zfloatpied = zpied - floatheight;
- int izCubeDessousFloat = (int)((zfloatpied) / MCube::CUBE_SIZE);
- float zCubeDessous2Float = zfloatpied - MCube::CUBE_SIZE;
- int izCubeDessous2Float = (int)(zCubeDessous2Float / MCube::CUBE_SIZE);
- // If we are in water
- InWater = false;
- if (World->getCube(x, y, z)->getType() == MCube::CUBE_EAU)
- InWater = true;
- if (InWater)
- {
- //Standing = true;
- Speed *= pow(0.2f, elapsed);
- }
- if (Standing)
- Speed *= pow(0.01f, elapsed);
- // Update camera last, once all movement have been made (smoother)
- if (isFirstPerson) // FP camera
- {
- Cam->moveTo(Position);
- }
- else // TP camera
- {
- Cam->setPosition(Position - (Cam->Direction * 5.0f));
- Cam->setLookAt(Position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement