Need help with using grid

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Hi,

    ok well i am using the grid 64*64 and i have run into a problem:

    i have an object that has the turret function and drag and drop.

    so i know that the image origin point is where the object rotates on , whilst using the turret behaviour

    and that if the image origin is in the middle the object sits right in the middle of the 4 surrounding squares

    - this however i don't want : i want the object to sit in the cell itself

    but like i said if i do that the object will rotate around the image origin so what do i do?

    this is the code i use to insure the object aligns with the grid:

    + Turret1: On DragDrop drop + Turret1: [X] Is overlapping UI + Turret1: Is overlapping Wall -> Turret1: Set position to (round(Turret1.X÷64)×64, round(Turret1.Y÷64)×64) -> Turret1: Set DragDrop Disabled

    also another question:

    I have 5 different turrets that fire 5 different bullets at 5 different enemies

    the enemies have different health

    the bullets have different damage

    so how do i code this?

    itll take a lot of (Bullet1 on collisions with Enemy1 - 1 from health) so thatll be 25 events

    so is there a much simpler way i am overlooking?

    thanks for your help in advance

    Radkampfwagen

  • For the grid you can set the position to int(x/64)*64+32, int(y/64)*64+32 to center it in the grid. Notice the +32 which is half the grid size. I used int instead of round because it works better for this case, but you can try both to see what i mean.

    For the rest of your question I'd recommend utilizing families and\or representing things as instances of fewer types. Both would require less events.

    Consider a simple example with similarities with your setup. Each object has it's own object type: Bullet1, bullet2, enemy1 and enemy2. And both enemies have health variables. Events would look like:

    keyboard: on z key pressed
    -- create bullet1
    
    keyboard: on x key pressed
    -- create bullet2
    
    bullet1: collides with enemy1
    -- enemy1: subtract 1 from health
    -- bullet1: destroy
    
    bullet1: collides with enemy2
    -- enemy2: subtract 1 from health
    -- bullet1: destroy
    
    bullet2: collides with enemy1
    -- enemy1: subtract 2 from health
    -- bullet2: destroy
    
    bullet2: collides with enemy2
    -- enemy2: subtract 2 from health
    -- bullet2: destroy

    A quick fix would be to instead use families. Remove the health instance variables from enemy1 and enemy2, and then add enemy1 and enemy2 to a family. Call the family enemyFamily and give it a health instance variable. Similarly add bullet1 and bullet2 to a family and call it bulletFamily. Then add a variable called damage to that family. Your events then could reduce down to:

    keyboard: on z key pressed
    -- create bullet1
    -- bullet1: set damage to 1
    
    keyboard: on x key pressed
    -- create bullet2
    -- bullet2: set damage to 2
    
    bulletFamily: collides with enemyFamily
    -- enemyFamily: subtract bulletFamily.damage from health
    -- bulletFamily: destroy

    You can do something similar by just having different enemies be different animations on the same object type. But it just depends what you want to do.

  • Thanks so much, your method works perfectly.

    Thanks,

    Radkampfwagen

  • Ok I have just 2 questions:

    1)

    How do i make the turrets(the ones that have the dragndrop behaviour) like go to the closest free grid square.

    ok i mean like for it to sort of suck in, like when mouse released the turret will go to the closest available grid (available meaning that it cant land on some grid squares)

    2)

    seems like a simple question but:

    how do i make some turret have dragndrop enabled only when player has enough score (100)

    but i have different turret animations

    each animation has a different cost

    so how do i make turret.animation1 have dragndrop enabled when score is equal to or more than 100?

    thanks alot

    Radkampfwagen

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 1) One way could be to create an invisible sprite at every grid location that isn't occupied and pick the closest one. Likely you could do that with collision detection and a double loop to loop over all the squares. It would give you an opportunity to understand some extra nuances to picking since you'd need wait or something to let all the newly created objects be pickable before picking the closest. Or just keep an invisible sprite at all the grid positions and not have to worry about it.

    Or if you prefer traditional programming logic you'd just loop over the center positions of all the empty squares, and calculate the distance. While doing it you'd keep track of the position with the closest distance, and update that if another is closer.

    2)

    It's mostly a matter of adding a condition i imagine. This is the power of events. If you want to do something conditionally you can add another condition.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)