void display (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer glLoadIdentity(); //gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); gluLookAt (xpos, ypos, xpos, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //camera position, x,y,z, looking at x,y,z, Up Positions of the camera camera(); fps(); player_position(); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, quad); glColorPointer(3, GL_FLOAT, 0, colors); glPushMatrix(); glTranslatef(0.0f, 0.0f, -5.0f); glDrawArrays(GL_QUADS, 0, 4); glPopMatrix(); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glFlush(); glutSwapBuffers(); //swap the buffers angle++; //increase the angle }
void BattleCommanderStateDirection::CheckPointingActor(void){ Cursor3D* cursor3d(_accessor->GetCursor3D()); if(!cursor3d->GetVisible()){ _pointing_actor = nullptr; return; } D3DXVECTOR3 cursor_position(cursor3d->GetPosition()); HitSphere cursor_hit(nullptr, cursor_position, 5.0f); //敵と味方を全部辿って、当たっている奴の中で一番近いやつを指す _pointing_actor = nullptr; float min_length(1000.0f); int player_count(_accessor->GetPlayerCount()); for(int i=0; i<player_count; i++){ BattleActor* player(_accessor->GetPlayer(i)); Hit* player_hit(player->GetCursorHit()); bool hit(player_hit->HitCheck(&cursor_hit)); if(hit){ D3DXVECTOR3 player_position(player->GetPosition()); D3DXVECTOR3 v(player_position - cursor_position); float length(D3DXVec3Length(&v)); if(min_length > length){ min_length = length; _pointing_actor = player; } } } int enemy_count(_accessor->GetEnemyCount()); for(int i=0; i<enemy_count; i++){ BattleActor* enemy(_accessor->GetEnemy(i)); Hit* enemy_hit(enemy->GetCursorHit()); bool hit(enemy_hit->HitCheck(&cursor_hit)); if(hit){ D3DXVECTOR3 enemy_position(enemy->GetPosition()); D3DXVECTOR3 v(enemy_position - cursor_position); float length(D3DXVec3Length(&v)); if(min_length > length){ min_length = length; _pointing_actor = enemy; } } } }
void EnemyAIAttackNeighbor::Update(void){ if(_accessor == nullptr){return;} if(_owner == nullptr){return;} //一番近いターゲットの索敵 float min_length(1000000.0f); BattleActor* target_player(nullptr); for(int i = 0; i<PLAYER_MAX; i++){ BattleActor* player(_accessor->GetPlayer(i)); if(player == nullptr){continue;} D3DXVECTOR3 player_position(player->GetPosition()); D3DXVECTOR3 enemy_position(_owner->GetPosition()); D3DXVECTOR3 enemy_to_player(player_position - enemy_position); D3DXVECTOR3 h_enemy_to_player(enemy_to_player); h_enemy_to_player.y = 0.0f; float h_sq_length(D3DXVec3LengthSq(&h_enemy_to_player)); if(h_sq_length < min_length){ min_length = h_sq_length; target_player = player; } } min_length = sqrt(min_length); const float SEARCH_RANGE(50.0f); const float ATTACK_RANGE(10.0f); if(target_player == nullptr || min_length > SEARCH_RANGE){return;} //一定以上なら向かう if(min_length > ATTACK_RANGE){ _owner->WalkTo(target_player->GetPosition()); } //十分近かったらそいつの方向いて攻撃 else{ //向きチェック if(_owner->GetSkill() == nullptr){ _owner->SetSkill(new BattleSkillAttack(_owner, _renderer, _accessor)); _owner->SetAction(new BattleActionSkill(_owner)); } } }
void BattleCommanderStateDirection::SetGhostAsMovement(void){ int player_count(_accessor->GetPlayerCount()); for(int i=0; i<player_count; i++){ ActorCursor* cursor(_accessor->GetPlayerCursor(i)); BattleActorGhost* ghost(_accessor->GetPlayerGhost(i)); BattleActor* player(_accessor->GetPlayer(i)); if(ghost == nullptr || player == nullptr){continue;} if(cursor == nullptr || !cursor->IsActive()){ ghost->SetVisible(false); continue; } //update position D3DXVECTOR3 position(0.0f, 0.0f, 0.0f); Cursor3D* cursor3d(_accessor->GetCursor3D()); if(cursor3d != nullptr){ position = cursor3d->GetPosition(); } ghost->SetPosition(position); //update rotation D3DXVECTOR3 player_position(0.0f, 0.0f, 0.0f); if(player != nullptr){ player_position = player->GetPosition(); } D3DXVECTOR3 direction(position - player_position); if(D3DXVec3Dot(&direction, &direction) > 0.0f){ D3DXVec3Normalize(&direction, &direction); } D3DXVECTOR3 rotation(0.0f, 0.0f, 0.0f); rotation.y = atan2(direction.x, direction.z); ghost->SetRotation(rotation); //play animation ghost->SetVisible(true); } }
void tileMap(){ sf::RenderWindow window(sf::VideoMode(512, 256), "Tilemap"); sf::View view(sf::Vector2f(0, 0), sf::Vector2f(window.getSize())); sf::View minimap(view.getCenter(), view.getSize()*2.f); minimap.setViewport(sf::FloatRect(0.75f, 0, 0.25f, 0.25f)); sf::Texture tileset; tileset.loadFromFile("terrain-6.png"); sf::Vector2f player_position(90, 90); sf::Vector2f zone_center(player_position); sf::Vector2u tim(300, 300); //Tiles in Map sf::Vector2u tiz(25, 25); // Tiles in Zone sf::Vector2u tile_size(32, 32); sf::Vector2u tex_tile_size(tileset.getSize()/16u); sf::Vector2f zone_size(tiz.x*tile_size.x, tiz.y*tile_size.y); std::vector<uint8_t> tiles(tiz.x*tiz.y); sf::VertexArray vertices(sf::Quads, tiz.x*tiz.y*4); std::ifstream map_file; map_file.open("map.data", std::ifstream::binary); //map_file.read(reinterpret_cast<char*> (&tiles[0]), tiles.size() * sizeof(tiles[0])); while (window.isOpen()){ sf::Event event; while (window.pollEvent(event)){ if(event.type == sf::Event::Closed) window.close(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){ player_position.x-=2; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){ player_position.x+=2; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){ player_position.y-=1; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){ player_position.y+=1; } if (std::abs(player_position.x - zone_center.x) > zone_size.x || std::abs(player_position.y - zone_center.y) > zone_size.y){ for (int y = (player_position.y - ((tiz.y-1)/2)); y <= player_position.y + ((tiz.y-1)/2); y++){ int x = (player_position.x - ((tiz.x-1)/2)); int n = y*tiz.x+x; map_file.read(reinterpret_cast<char*> (&tiles[n]), tim.x * sizeof(tiles[0])); } for (size_t y=0; y<tiz.y; y++){ for (size_t x=0; x<tiz.x; x++){ sf::Vertex* quad = &vertices[(y*tiz.x+x)*4]; quad[0].position = sf::Vector2f(y*tile_size.x, x*tile_size.y); quad[1].position = sf::Vector2f((y+1)*tile_size.x, x*tile_size.y); quad[2].position = sf::Vector2f((y+1)*tile_size.x, (x+1)*tile_size.y); quad[3].position = sf::Vector2f(y*tile_size.x, (x+1)*tile_size.y); const int tile_id = tiles[y*tiz.x+x]; const int tx = tile_id % (tileset.getSize().x / tex_tile_size.x); const int ty = tile_id / (tileset.getSize().x / tex_tile_size.x); quad[0].texCoords = sf::Vector2f(tx*tex_tile_size.x, ty*tex_tile_size.y); quad[1].texCoords = sf::Vector2f((tx+1)*tex_tile_size.x, ty*tex_tile_size.y); quad[2].texCoords = sf::Vector2f((tx+1)*tex_tile_size.x, (ty+1)*tex_tile_size.y); quad[3].texCoords = sf::Vector2f(tx*tex_tile_size.x, (ty+1)*tex_tile_size.y); } } } window.clear(sf::Color::White); window.setView(view); view.setCenter(player_position); window.draw(vertices, &tileset); minimap.setCenter(view.getCenter()); window.setView(minimap); window.draw(vertices, &tileset); window.display(); sf::sleep(sf::milliseconds(10)); } }