Alright, so I decided to use Visual Studio. Now, my problem is that my framerate goes down a lot when I start firing bullets using the code below. I don’t know what I’m doing wrong. Any help please? There are some functions from other classes I made…
#include "LevelOneState.h"
#include "../core/DMAGame.h"
#include "../core/DBGR.h"
#define _USE_MATH_DEFINES
// Panda3D stuff.
#include "auto_bind.h"
#include "audioManager.h"
#include "executionEnvironment.h"
#include "filename.h"
#include "AnimControlCollection.h"
#include "collisionHandlerPusher.h"
#include "collisionTraverser.h"
#include "collisionNode.h"
#include "collisionSphere.h"
#include "collisionInvSphere.h"
#include "pandaFramework.h"
#include "clockObject.h"
// C++ stuff.
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <list>
#include <stdio.h>
#include <time.h>
LevelOneState::LevelOneState() {
}
LevelOneState::~LevelOneState() {
}
void LevelOneState::init (PlayerStats *ps) {
counter = new TextNP ("counter", intToString(moneyBallHealth));
counter->set_x (-1.3);
counter->set_z (-0.95);
counter->set_scale (0.2);
moneyBallHealth=10;
bulletsC=0;
easyBotsC=0;
// Get the location of the executable file I'm running.
mydir = ExecutionEnvironment::get_binary_name();
mydir = mydir.get_dirname();
//window=DMAGame::getWindow();
// Load the environment model and display it.
NodePath environ = LoadModelAndDisplay (mydir+"/models/Ground2/Ground2");
environ.set_scale (1, 1, 1);
// The environment's 3D model center is not in the middle of the grassfield, so we move the
// model so that the grassfield is approximately at (0,0,0) of render's coord system.
environ.set_pos (0, 0, 0);
moneyBall = LoadModelAndDisplay(mydir+"/models/alice-shapes--sphere/sphere");
moneyBall.set_color(0.5, 0, 0, 0);
moneyBall.set_scale(10);
moneyBall.set_pos(0,0,20);
collision.addCollisionSphere(moneyBall, "moneyBall", false, Point(0, 0, 0), 4);
player = LoadModelAndDisplay ("panda-model");
player.set_scale (0.03); // player model is large, so have to scale it down quite a bit
player.set_pos (100, 100, 0);
player.set_hpr (90, 0, 0);
player.reparent_to (RENDER);
cNode=new CollisionNode("player");
cNode->add_solid(new CollisionSphere(0,0,0,600));
playerC=player.attach_new_node(cNode);
sky = LoadModelAndDisplay(mydir + "/models/happysky/happysky");
sky.set_scale(0.78, 0.78, 2);
sky.set_pos(0,0,0);
/*cNode = new CollisionNode("sky");
cNode->add_solid(new CollisionInvSphere(0,0,0,725));
skyC=sky.attach_new_node(cNode);*/
//bullets = LoadModelAndDisplay(mydir + "/../models/fire (1)/fire");
//bullets.set_scale(0.03);
// Load our player (main actor, currently a panda), centering it in the environment.
//collision.addCollisionSphere(player, "player", true, Point(0, 0, 0), 600);
collision.addCollisionInvSphere(sky, "sky", false, Point(0,0,0), 725);
CollisionHandlerPusher* wall = new CollisionHandlerPusher;
traverser=new CollisionTraverser();
wall->add_collider(playerC, player);
traverser->add_collider(playerC, wall);
camera = WINDOW->get_camera_group();
camera.reparent_to (RENDER);
camera.set_pos (0, 400/0.03, 200/0.03);
//camera.set_pos(0, 0, 2000);
camera.look_at (player.get_pos());
easyBot();
fire();
}
void LevelOneState::deinit() {
}
void LevelOneState::fire()
{
bulletsc=LoadModelAndDisplay(mydir+"/models/fire (1)/fire");
bulletsc.set_hpr(player.get_h()+90, 0, 90);
bulletsc.set_scale(0.03);
bulletsc.set_pos(player, Point(0, -1000, 100));
//bullets[bulletsC].reparent_to (RENDER);
//bulletsX[bulletsC]=player.get_x();
//bulletsY[bulletsC]=player.get_y();
collision.addCollisionSphere(bulletsc, "bullets", false, Point(0,0,0), 100);
//bulletsX.push_back(player.get_x());
//bulletsY.push_back(player.get_y());
bullets.push_back(bulletsc);
DUMP("FIRED");
}
void LevelOneState::easyBot()
{
lastTime=ClockObject::get_global_clock()->get_real_time();
srand (time (NULL));
int maxNum = 1450;
double randomX = rand() % 725;
if(rand()%2==0)
{
randomX=-randomX;
}
double randomY = sqrt(725*725-randomX*randomX);
if(rand()%2==0)
{
randomY = -randomY;
}
easyBotsc=LoadModelAndDisplay(mydir+"/models/bvw-f2004--evilaibo/evilaibodog");
//easyBots[easyBotsC].look_at(moneyBall);
//easyBots[easyBotsC].set_pos(randomX, randomY, 0);
//easyBots[easyBotsC].set_pos(100, 725, 0);
easyBotsc.set_pos(randomX, randomY, 0);
int angle=90-(atan(abs(easyBotsc.get_x()/easyBotsc.get_y()))*180/M_PI);
if((easyBotsc.get_x()>=0&&easyBotsc.get_y()<0))
{
angle=-angle;
}
if(easyBotsc.get_x()<0&&easyBotsc.get_y()>0)
{
angle=180-angle;
}
if(easyBotsc.get_x()<0&&easyBotsc.get_y()<=0)
{
angle=180+angle;
}
easyBotsc.set_h(angle);
easyBotsc.set_scale(10);
collision.addCollisionSphere(easyBotsc, "easyBots", false, Point(0,0,0), 2);
//easyBotHealth[easyBotsC]=10;
//easyBots[easyBotsC].reparent_to (RENDER);
easyBots.push_back(easyBotsc);
DUMP("Dog LOADED");
}
void LevelOneState::logic(void) {
if(KPM->isKeyPressed("q"))
{
FRAMEWORK->close_framework();
}
if(moneyBallHealth>0)
{
if(KPM->isKeyPressed("arrow_up"))
{
player.set_y(player, -20);
/*if (KPM->timeSinceKeyPress ("arrow_up") > 0.5) {
fire();
KPM->resetKeyPressTimer("arrow_up");
}*/
}
else if(KPM->isKeyPressed("arrow_down"))
{
player.set_y(player, 20);
}
if(KPM->isKeyPressed("arrow_left"))
{
player.set_h(player.get_h() + 2);
}
else if(KPM->isKeyPressed("arrow_right"))
{
player.set_h(player.get_h() - 2);
}
if(KPM->isKeyPressed("a"))
{
if (KPM->timeSinceKeyPress ("a") > 0.2) {
fire();
KPM->resetKeyPressTimer("a");
}
}
double currTime = ClockObject::get_global_clock()->get_real_time();
if((currTime-lastTime)>2)
{
easyBot();
}
camera.set_pos (player, Point(0, 400/0.03, 200/0.03));
camera.look_at(player.get_pos());
//camera.set_pos (player, Point(0, 150/0.03, 1000));
if(KPM->isKeyPressed("space"))
{
if(!KPM->isKeyProcessed("space"))
{
if(jumping==false)
{
zspeed=5;
jumping=true;
}
KPM->setKeyProcessed("space");
}
}
if(jumping==true)
{
player.set_z(player.get_z()+zspeed);
zspeed=zspeed-0.1;
}
if((player.get_z()<=0)&&(jumping=true))
{
zspeed=0;
player.set_z(0);
jumping=false;
}
traverser->traverse(RENDER);
/*list<NodePath>::iterator it;
for(it = bullets.begin(); it != bullets.end(); it++)
{
it->set_pos(*it, Point(0, 0, -200));
if(collision.collide(*it, sky)){
NodePath destructor = *it;
destructor.remove_node();
}
}*/
list<NodePath>::iterator its;
list<NodePath>::iterator itc;
for(its = easyBots.begin(); its != easyBots.end(); its++)
{
its->set_pos(*its, Point(-0.1, 0, 0));
if(collision.collide(*its, moneyBall)){
NodePath destructor = *its;
destructor.remove_node();
//moneyBallHealth--;
}
for(itc = bullets.begin(); itc != bullets.end(); itc++)
{
itc->set_pos(*itc, Point(0,0,-20));
if(collision.collide(*its, *itc)){
NodePath destructor = *itc;
destructor.remove_node();
NodePath cdestructor = *its;
cdestructor.remove_node();
}
if(collision.collide(*itc, sky)){
NodePath destructor = *itc;
destructor.remove_node();
}
if(collision.collide(*itc, moneyBall)){
NodePath destructor = *itc;
destructor.remove_node();
}
}
}
its = easyBots.begin();
while( its != easyBots.end())
{
if(its->is_empty())
{
easyBots.remove(*its);
easyBots.erase(its);
}
its++;
}
its = bullets.begin();
while( its != bullets.end())
{
if(its->is_empty())
{
bullets.remove(*its);
bullets.erase(its);
}
its++;
}
counter->SetText(intToString(moneyBallHealth));
counter->set_x (-1.3);
counter->set_z (-0.95);
counter->set_scale (0.2);
}
else
{
TextNP* loser = new TextNP ("loser", "You Lose!!!");
loser->set_x (-1.5);
loser->set_scale(0.5);
}
}