2013-07-08 2 views
0

먼저 내가 아직 아주 경험이 없다고 말하고 싶습니다. 지난 주에 프로그래밍을 시작했습니다.Enemy Logic | 글 머리 기호 재설정

현재 첫 번째 게임 (및 C++ 응용 프로그램)을 개발 중이며 문제가 발생하여이를 해결할 수 없어서 포기하기 시작했습니다.

적을 올바르게 스폰 할 수 있지만, 이제는 매초마다 한 번씩 씩씩씩 쏘고 싶습니다. 다음과 같이 내가 이것을 사용하고 코드는 다음과 같습니다

for (int i = 0; i < 200; i++) 
     { 
      if (enemy_basic[i].getPositionY() >= -100 && enemy_basic[i].getPositionY() <= 900) 
      { 
       if (enemyBasicLaserNumber[i] < 200 && enemyLaserTimer[i].getElapsedTime().asSeconds() > 1) 
       { 
        enemy_laser[enemyBasicLaserNumber[i]].setPosition(enemy_basic[i].getPositionX(), enemy_basic[i].getPositionY()); 
        enemyLaserTimer[i].restart(); 
        enemyBasicLaserNumber[i]++; 
        cout << enemyBasicLaserNumber[i] << endl; 
       } 

       if (enemyBasicLaserNumber[i] >= 199) enemyBasicLaserNumber[enemyBasicNumber] = 0; 
      } 
     } 

를 자, 내가 어딘가에서 뭔가 잘못 알고 총알은 유지 (즉시 화면에 여러 적이 있기 때문에 다시 적에게 다시 받고 있기 때문에 오래지 않아 완벽하게 작동하는 원수는 1 명에 불과합니다.) 그리고 나는 아직도 변화가 필요한 것 또는 완전히 다른 방식으로이 작업을 수행해야 하는지를 파악하지 못했습니다.

누군가가 올바른 방향으로 나를 가리키거나 어떤 식 으로든 나를 도울 수 있다면, 나는 매우 감사 할 것입니다!

답변

0

코드를보다 적절한 객체 지향 스타일로 구조해야합니다. 가상 함수 업데이트를 포함하는 기본 클래스 Entity를 만들 수 있습니다. 엔티티의 참조/포인터 부모도 포함해야합니다.

class SceneManager 
{ 
public: 
    SceneManager(); 
    virtual ~SceneManager(); 

    //put your own parameters 
    Bullet& createBullet(/* */); 

private: 
    //a list of pointers for polymorphism 
    boost::ptr_list<Entity> entities_; 
}; 

class Entity 
{ 
public: 
    Entity(SceneManager& smgr); 
    virtual ~Entity(); 

    virtual void update(const float timedelta) = 0; 
private: 
    const int id_; 
    static int count_ = 0; 
protected: 
    SceneManager& smgr_; 

    //local scale, rotation 
    Transform transform_; 

    //position 
    Vector3d position_; 

    //the model,sprite representation 
    char drawable_; 
}; 

class Enemy:public Entity 
{ 
public: 
    Enemy(SceneManager& smgr); 
    virtual ~Enemy(); 

    virtual void update(const float timedelta); 
    void shoot(); 
}; 

class Bullet:public Entity 
{ 
public: 
    Bullet(SceneManager& smgr); 
    virtual ~Bullet(); 

    virtual void update(const float timedelta); 
}; 

void Enemy::update(const float timedelta) 
{ 
    //shooting each second 
    static float shootDelay = 1; 

    static Timer timer; 

    //check if timer has started 
    if(!timer.started) 
     timer.start(); 

    //if the timer has reached the desired delay 
    if(timer.elapsed() > shootDelay) 
    { 
     shoot(); 
     timer.restart(); 
    } 
} 

void Enemy::shoot() 
{ 
    //create the bullet via the scenemanager 
    smgr_.createBullet(/******/); 
} 

이 코드는 상징적 인 코드 일 뿐이며, 실제로이 구조를 구현하려면 기존 라이브러리를 사용해야합니다. 이 코딩 방법은 특히 Irrlicht 및 Ogre3D 그래픽 엔진에서 사용되지만 게임 엔진의 핵심 로직에도 적용 할 수 있습니다.

+0

네, 지금 당장의 방법보다 코드를 구조화하는 더 좋은 방법이 있다는 것을 알았지 만 어떤 리소스가 학습에 도움이되는지 알지 못합니다. 책, OOP에 대한 자습서를 찾고, 아니면 오픈 소스 게임을보아야합니까? 당신이 올바른 방향으로 날 가리킬 수 있다면 멋져요, 고마워! – user2558804

관련 문제