Grenades and Explosions


  • Forum Admin

    It's now possible on Testbed to fire projectiles which add explosions or any kind of NPC on impact.

    1. In projectiles.json add a projectile type with an impact.spawn definition. In this case it will spawn "gasgrenade". In your weapon use this projectile ("projectile": "grenade").
    ...
    {
       "type":"grenade", 
       "image":"snowball_idle.bani",
        "arrow":"bbuilder_grenade.png",
        "zangle":10,
        "speed":11,
        "gravity":0.9,
        "hittype":"grenade",
        "impact": {
            "damage": [12, 18],
            "radius": 2,
            "spawn": "gasgrenade"
        }
    }
    ...
    
    1. In spawns.json define what should be spawned for gasgrenade: in this case we add item greengas.
    ...
    {
        "type": "gasgrenade",
        "items": [
           {"itemid":"greengas", "chance":100, "atcenter":true}
        ]
    }
    ...
    
    1. Add an item greengas to items/spawn/greengas.json which uses script class spawn_greengas:
    {
        "itemid": "greengas",
        "name": "Green Gas",
        "icon": "bbuilder_stone1.png",
        "ani": "particle_grenade",
        "price": 0,
        "itemtype": "spawn",
        "scriptclasses": [ "spawn_greengas" ]
    }
    
    1. In scriptclasses/spawn_greengas.js you can define some script behaviour, in this case we auto-destroy after one second:
    function onCreated() {
        this.name = "";
        this.ani = "particle_grenade"; // can also be configured in items/spawn/greengas.json
        this.scheduleevent(1,"despawn");
    }
    function onDespawn() {
        this.destroy();
    }
    

    These are quite a lot of steps and you will also need to create an animation particle_grenade.bani with particle emitters, but it lets you easily re-use existing spawn definitions for other projectiles. In your script you can use the function this.map.addnpc() to add more effects.


  • Corleone Staff

    Very cool! Can't wait for the grenade launcher.



  • Interesting! keep up the good works stef


  • Forum Admin

    Simplified running of scripts on projectile impact (and fixed collision with players on server-side):

    1. In projectiles.json add a projectile type with impact.scriptclasses

    {
        "type":"ak47", 
        "image":"ak47_idle.bani",
        "arrow":"corleone_bullet1.png",
        "zangle":0,
        "speed":11,
        "gravity":0,
        "hittype":"bullet",
        "damage":7,
        "collidetiles":true,
        "impact": {
            "scriptclasses": ["effect_fire"]
        }
    }
    

    2. In scriptclasses/effect_fire.js search for near players and call seteffect

    function onCreated() {
        let players = Server.searchplayers({
            map:this.map,
            area:{x:this.x-2, y:this.y-2, w:4,h:4}
        });
        players.forEach(function(player) {
            player.seteffect(10, "hurt", {hp:1});
            player.aniarg1 = "effect_fire.bani";
        });
        this.destroy();
    }
    


  • Wow stefan nice script 🙂


Log in to reply