//attacker is an array 5 pointers //a pointer to the attacker of each side //so attacker[Left] is the attacker on the left void Player_TakeDamage(Entity* p, Entity** attacker) { //push player in the opposite direction of the attacker if(attacker[Left]) { p->hp -= attacker[Left]->damage; Player_Move(p, 20, 0); } if(attacker[Right]) { p->hp -= attacker[Right]->damage; Player_Move(p, -20, 0); } if(attacker[Top]) { p->hp -= attacker[Top]->damage; Player_Move(p, 0, 20); } if(attacker[Bottom]) { p->hp -= attacker[Bottom]->damage; Player_Move(p, 0, -20); } p->playerC->invulnerability_timer = 2000; }
void Player_Move(ULONG Direction, float Distance, bool Velocity) { D3DXFROMWINEVECTOR3 vecShift = D3DXFROMWINEVECTOR3(0, 0, 0); // Which direction are we moving? if ( Direction & DIR_RIGHT ) vecShift += g_vecPlayer_Right * Distance; if ( Direction & DIR_LEFT ) vecShift -= g_vecPlayer_Right * Distance; if ( Direction ) Player_Move ( vecShift, Velocity ); }
//------------Player_Update------------ // Update player by calling other functions accordingly // Input: none // Output: none void Player_Update(void){ // Re-locate player Player_Move(); Player_UpdateVelocity(); if(Button0_Pressed() && player.power > 0 && player.powerTick==0) { Powerups[player.power].function(&player); // call powerup function which will intialize powerTick } else if(player.powerTick>0) { Powerups[player.power].function(&player); } }
void Player_Update( float fElapsedTime ) { // Clamp the velocity to our max velocity vector if ( fabs(g_vecPlayer_Velocity.x) > g_fMaxVelocityX ) { if (g_vecPlayer_Velocity.x > 0) g_vecPlayer_Velocity.x = g_fMaxVelocityX; else g_vecPlayer_Velocity.x = -g_fMaxVelocityX; } // Move the player Player_Move (g_vecPlayer_Velocity * fElapsedTime, false ); // Apply collision detection. Player_CollisionDetection(); //-------------- // Deceleration! //-------------- // Calculate the reverse of the velocity direction D3DXFROMWINEVECTOR3 vecDec = -g_vecPlayer_Velocity; D3DXFROMWINEVec3Normalize( &vecDec, &vecDec ); // Retrieve the actual velocity length float Length = D3DXFROMWINEVec3Length( &g_vecPlayer_Velocity ); // Calculate total deceleration based on friction values float Dec = (g_fFriction * fElapsedTime); if ( Dec > Length ) Dec = Length; // Apply the friction force g_vecPlayer_Velocity += vecDec * Dec; }
virtual int render() { STX_TRY; mI->Input->Update (); // ----- Delta Time ----- fElapsedTime = mI->Render->GetFrameTime() / 1000.0f; // ----- Text ----- stx_strlcpy (szText, "Move the mouse left and right and watch the tank try to follow!\n", 2048); stx_strlcat (szText, "Press Left Mouse button to fire!\n", 2048); stx_strlcat (szText, "Press 'f' to toggle freezing the tank movement (debug)\n", 2048); stx_snprintf(szBuf, 2048, "%d, %d\n", mI->Input->GetMouseX(), mI->Input->GetMouseY() ); stx_strlcat (szText, szBuf, 2048); stx_snprintf(szBuf, 2048, "%.3f, %.3f\n", g_vecPlayer_Pos.x, g_vecPlayer_Pos.y); stx_strlcat (szText, szBuf, 2048); stx_snprintf(szBuf, 2048, "Muzzle angle %.3f\n", g_fGunAngle); stx_strlcat (szText, szBuf, 2048); stx_snprintf(szBuf, 2048, "Tank velocity %.3f\n", g_vecPlayer_Velocity.x); stx_strlcat (szText, szBuf, 2048); mTextSmallWhite.SetText (szText); // Position the mouse cursor cursor.SetPosition((float)mI->Input->GetMouseX(), (float)mI->Input->GetMouseY(), 1); // "Freeze" tank except allow gun to rotate if (mI->Input->OnKeyPress(KEY_f)) g_bToggleFreezeTank = !g_bToggleFreezeTank; ULONG Direction = 0; float deltaX = g_vecPlayer_Pos.x - mI->Input->GetMouseX(); if (!g_bToggleFreezeTank && fabs(deltaX) > g_Epsilon) { if (deltaX < 0) { Direction |= DIR_RIGHT; tank.SetSequence(1); } else if (deltaX > 0) { Direction |= DIR_LEFT; tank.SetSequence(2); } } else { // Stop the animation if not moving tank.SetSequence(0); } // Any movement? if ( Direction && !g_bToggleFreezeTank ) { Player_Move( Direction, g_fSpeed * fElapsedTime, true ); } //--------------------------- // Update the player //--------------------------- Player_Update( fElapsedTime ); //--------------------------- // Move and rotate the barrel // Note: there would normally be a class representing the whole tank //--------------------------- float fMouse2TankX = tank.GetPosX() - mI->Input->GetMouseX(); float fMouse2TankY = tank.GetPosY() - mI->Input->GetMouseY(); // y position going down // Clamp. Mouse is below tank if (fMouse2TankY < 0.0f) fMouse2TankY = 0.0f; g_fGunAngle = atan2(fMouse2TankY, fMouse2TankX) * 180.0f / fPI; // in degrees g_fGunAngle -= 90.0f; // barrel is initially pointing up (in image). atan2() w.r.t x-axis (pointing right) barrel.SetAngleXYZ(0.0f, 0.0f, g_fGunAngle); // Set position of barrel (child) on the tank (parent) g_vecBarrel.x = tank.GetPosX(); g_vecBarrel.y = tank.GetPosY(); barrel.SetPosition(g_vecBarrel.x, g_vecBarrel.y, 0); // Move the tank (player) tank.SetPosition(g_vecPlayer_Pos.x, g_vecPlayer_Pos.y, 1); tankShadow.SetPosition(tank.GetPosX(), tank.GetPosY() + 50, 0); // location is tank HotSpot plus an offset of 50 //--------------------------- // Fire weapon!! //--------------------------- if (5 && mI->Input->OnMouseButtonPress(MBUTTON_LEFT)) { // Initial bullet position (following code is rather verbose and could be combined into less lines // but did it that way to explain the math. float angleDegrees = barrel.GetAngleZ() + 90.0f; float angleRadians = angleDegrees * fPI / 180.0f; float fX = barrel.GetPosX() - cosf(angleRadians) * BARREL_LENGTH; float fY = barrel.GetPosY() - sinf(angleRadians) * BARREL_LENGTH; bullet.SetAngleXYZ(0, 0, angleDegrees - 90.0f); g_vecBullet_Pos = D3DXFROMWINEVECTOR3(fX, fY, 0.0f); bullet.SetPosition(g_vecBullet_Pos.x, g_vecBullet_Pos.y, 0); // Initial bullet velocity D3DXFROMWINEMATRIX mtxRotate; D3DXFROMWINEVECTOR3 vecVelDir = D3DXFROMWINEVECTOR3(-1.0f, 0.0f, 0.0f); D3DXFROMWINEMatrixRotationZ( &mtxRotate, angleRadians); D3DXFROMWINEVec3TransformNormal( &vecVelDir, &vecVelDir, &mtxRotate ); g_vecBullet_Velocity = vecVelDir * BULLET_SPEED; // Show blast! g_fMuzzleFlashTimer = 0.05f; muzzleFlash.SetPosition(g_vecBullet_Pos.x, g_vecBullet_Pos.y, 0); g_bBulletDead = false; // Make sound! (no sound lib hooked up but this is where you would do it. STX_Service::GetAudioInstance()->Play("tankfire1"); } // Update the single bullet if (!g_bBulletDead) { bullet.SetShow(true); Bullet_Update( fElapsedTime, &bullet ); bullet.SetPosition(g_vecBullet_Pos.x, g_vecBullet_Pos.y, 0); } else { bullet.SetShow(false); } //--------------------------- // Render muzzle flash effect //--------------------------- g_fMuzzleFlashTimer -= fElapsedTime; if (g_fMuzzleFlashTimer > 0.0f) { muzzleFlash.SetShow(true); } else { muzzleFlash.SetShow(false); g_fMuzzleFlashTimer = 0.0f; } //----------------- // Render the scene //----------------- mI->Render->BeginScene (); mI->Render->ClearViewPort (160, 160, 160); mI->Render->SetViewPort2d (0, 0, mI->Window->GetWidth(), mI->Window->GetHeight()); mI->Entity2dManager->RenderEntities2d ( ); // Render GUI layer mI->Entity2dManager->RenderEntities2d (1); //???mI->Render->ShowFpsInWindowTitle(); // MUST call this to update FMOD STX_Service::GetAudioInstance()->Update(); mI->Render->EndScene (); STX_CATCH; return 0; }
void Player_Update(World* world) { Entity* p = &world->player; if(SDL_GetTicks() - p->weaponsC->current_weapon->last_shot > 250) { p->weaponsC->recoil -= 0.5f * delta_g; } if(p->weaponsC->recoil < 0) { p->weaponsC->recoil = 0; } if(bullet_time_g) { p->playerC->time_stop -= 0.01 * delta_g; if(p->playerC->time_stop <= 0) { bullet_time_g = false; } } if(p->playerC->running) { Player_Run(p); } else { Player_Walk(p); } p->playerC->vision_timer += delta_g; if(game_state_g != GameState_Editing_Map && p->playerC->vision_timer > 15) { Player_FieldOfView(p, world); p->playerC->vision_timer = 0; } if(game_state_g == GameState_Editing_Map) { p->movementC->speed = LEVEL_EDITOR_SPEED; } if(game_state_g != GameState_Editing_Map) { p->playerC->invulnerability_timer -= delta_g; if(p->weaponsC->reloading) { WeaponsComponent_Reload(p->weaponsC); } Entity_CollisionWithStuff(p, world); Player_CheckBonusCollision(p, &world->bonus_vector); } Player_Move(p, p->movementC->dx, p->movementC->dy); }
void Physics_Frame(struct server *server) { int i; int x; struct client *client; struct edict *e; vec3_t forward, right, up, end; struct trace *trace; for (i=0; i<32; i++) { if (server->clients[i].inuse == false) continue; client = &server->clients[i]; Player_Move(server, &server->clients[i], &server->edicts[i+1], &client->ucmd_new); /* client = &server->clients[i]; Vector_Angles(server->edicts[i+1].state.angles, forward, right, up); Vector_Scale(up, server->frametime * client->ucmd_new.upmove, up); Vector_Scale(forward, server->frametime * client->ucmd_new.forwardmove, forward); Vector_Scale(right, server->frametime * client->ucmd_new.sidemove, right); e = &server->edicts[i+1]; Vector_Add(end, e->state.origin, up); Vector_Add(end, end, forward); Vector_Add(end, end, right); trace = Trace_Trace(server, NULL, e->state.mins, e->state.maxs, e->state.origin, end, 0, e); Vector_Copy(e->state.origin, trace->endpos); */ /* printf("mins: "); PRINT_VEC(e->state.mins); printf("maxs: "); PRINT_VEC(e->state.maxs); */ /* Vector_Add(server->edicts[i+1].state.origin, server->edicts[i+1].state.origin, up); Vector_Add(server->edicts[i+1].state.origin, server->edicts[i+1].state.origin, forward); Vector_Add(server->edicts[i+1].state.origin, server->edicts[i+1].state.origin, right); */ for (x=0; x<3; x++) { server->edicts[i+1].state.angles[x] = client->ucmd_new.angles[x]; } } for (i=MAX_CLIENTS+1; i<server->edicts_count; i++) { e = &server->edicts[i]; if (e->inuse == false) continue; if (e->nexttime > server->realtime) continue; LUA_CallFunctionArguments(server, &server->mod, "handle_entity", 0, false, "u", e); } }
//------------Player_Update------------ // Update player by calling other functions accordingly // Input: none // Output: none void Player_Update(void){ // Re-locate player Player_Move(); }