void EraserToolInputState::DestroyUnitAtScreenCoords( const Vec2f& screenCoords ) { EditorState* owner = GetOwnerDerived(); MapView* mapView = owner->GetMapView(); Map* map = owner->GetMap(); // Get the UnitSprite under the pointer (if any). UnitSprite* unitSprite = mapView->GetUnitSpriteAtScreenCoords( screenCoords ); if( unitSprite ) { // If there is a Unit under the pointer, destroy it. Unit* unit = unitSprite->GetUnit(); DebugPrintf( "Erasing UnitSprite at Tile (%d,%d)!", unit->GetTileX(), unit->GetTileY() ); map->DestroyUnit( unit ); } }
Unit* PlaceToolInputState::CreateUnitAtScreenCoords( const Vec2f& screenCoords ) { Unit* unit = nullptr; if( mSelectedUnitType ) { if( mSelectedFaction ) { EditorState* owner = GetOwnerDerived(); MapView* mapView = owner->GetMapView(); Map* map = owner->GetMap(); // Get the Tile at the screen coords. Vec2f worldPos = mapView->ScreenToWorldCoords( screenCoords ); Vec2s tilePos = mapView->WorldToTileCoords( worldPos ); Map::Iterator tile = map->GetTile( tilePos ); if( tile.IsValid() && tile->IsEmpty() && mSelectedUnitType->CanMoveAcrossTerrain( tile->GetTerrainType() ) ) { // If the selected UnitType can be placed into the Tile, create a new Unit. DebugPrintf( "Placing Unit at tile (%d,%d)!", tilePos.x, tilePos.y ); unit = map->CreateUnit( mSelectedUnitType, mSelectedFaction, tilePos ); } else { WarnFail( "Cannot place Unit into tile (%d,%d)!", tilePos.x, tilePos.y ); } } else { WarnFail( "Cannot create Unit because no Faction was selected!" ); } } else { WarnFail( "Cannot create Unit because no UnitType was selected!" ); } return unit; }