"Discovery Dust" item locating function, how could I improve???

So I'm making my very first game, I've done decent architecture designing so far or at least I hope. I made an Item class with derived specific objects and I really wanted to embrace OOP so I made each item take in arguments for whatever it is they will alter or access, this specific item "discovery dust" takes in a Game(custom object) reference and a Character(custom object) reference.

It uses the Game reference to use a function to return an array of coordinates to all the Item objects on the board. To overcomplicate things I of course wanted just one array so I have it twice the size of the number of Item objects I have on the board, and have the X coordinates be the first half, and the Y coordinate be the second. Hence the "CurrClosest[0] = TempList[i]; CurrClosest[1] = TempList[ListSize+i];" line and other similar lines

The function basically tells the rough distance of the closest item to the Character using it and the direction (north, northeast, southwest, etc.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

edit: actually im not even sure it works for anything but south southwest and southeast but considering south is handled basically the same as north it shouldn't an issue, however I haven't coded in if the item is a straight shot north, east, west or south yet but that's an easy write in, keep in mind though
void Use( Character& AlterChar, GameBase& RefPass )
    {
        int CurrClosest[2], TempVector[2], CurrShortest, ListSize;
        bool IsRight = false, IsUp = false, IsNoS = false; //the only necessary bool is IsNoS(is north or south) the 
        vector<int> TempCoords, TempList; //               \\others are an attempt to improve readability.
        TempCoords = AlterChar.getCoords(); //get a size 2 vector with first coord as X of character using item and
        TempList = RefPass.return_coord_list(); //get vector of Item coordinates.                 \\second as Y coord.
        ListSize = (int)TempList.size() / 2; //remember the list is twice as long as the coordinate amount
        for ( int i = 0; i < ListSize; i++ ) //uses hypot to find distance between each coordinate pair and if shorter 
        { //                                                                                         \\changes CurrShortest
            if ( abs( hypot(TempCoords[0]-TempList[i], TempCoords[1]-TempList[ListSize+i] ) ) < CurrShortest )
                {
                    CurrShortest = abs( hypot( TempList[i] - TempCoords[0], TempList[ListSize+i] - TempCoords[1] ) );
                    CurrClosest[0] = TempList[i]; //sets the CurrClosest element 0 to the X coordinate and
                    CurrClosest[1] = TempList[ListSize+i]; //sets the CurrClosest element 1 to the Y coordinate
                }
        }
        TempVector[0] = TempCoords[0] - CurrClosest[0]; //sets TempVector elem 0 to the closest Item's X subtracted from Char's X
        TempVector[1] = TempCoords[1] - CurrClosest[1]; //sets TempVector elem 1 to the closest Item's Y subtracted from Char's Y
        if ( TempCoords[1] > CurrClosest[1] ) { IsUp = true; } //finds if Item is higher or lower, to improve readability
        if ( TempCoords[0] > CurrClosest[0] ) { IsRight = true; } //finds if Item is right or left, again readability 
        cout << "The Dust indicates that the closest item is about " << (int)CurrShortest << " steps due ";
        if ( IsUp == true && ( ( TempVector[0] / TempVector[1] ) < 4 || ( TempVector[0] / TempVector[1] ) > -4 ) )
            { cout << "North"; IsNoS = true; }
        else if ( ( TempVector[0] / TempVector[1] ) < 4 || ( TempVector[0] / TempVector[1] ) > -4 )
            { cout << "South"; IsNoS = true; }
        if ( IsNoS && IsRight == true && ( TempVector[1] / TempVector[0] ) > - 4 )
            { cout << "-west."; }
        else if ( IsNoS && ( TempVector[1] / TempVector[0] ) > -4 )
            { cout << "-east."; }
        if ( ( !IsNoS && TempVector[0] / TempVector[1] ) > 4 )
            { cout << "East."; }
        else if ( !IsNoS && ( TempVector[0] / TempVector[1] ) < -4 )
        { cout << "West."; }
    } 


didn't even try to comment in explanations for my final portion but it uses some linear algebra to figure out if the coord pair is in the right range for the direction (north, south, etc.)

Basically I could have gone through each direction like a clock, check if in north range, check if in north-east range, check if in east range, check if in south-east range, etc. but I really wanted to shrink the code and hit the patterns, at the expense of readability I'm fearing

I'm not going to school or anything so any advice on how to make it more elegant would be awesome, I'm a compaction whore and I like as little unnecessary code as possible but I worry it ends up hurting me
Last edited on
Topic archived. No new replies allowed.