Duck Game

Duck Game

评价数不足
Grenade modding tutorial
由 Killer-Fackur 制作
in this tutorial i'll teach you how to make a grenade with custom texture and projectiles.
   
奖励
收藏
已收藏
取消收藏
creating the texture
grenades will have to have one texture when its pinned and unpinned.
And to make it the easiest way we'll need to create a spritemap. Spritemaps are used to have multiple frames or pictures in one image.

To begin we will create a template for the frames we'll need.
an easy way of doing this is drawing a line in the centre.
now that we got the template done we'll start to draw it.
(sorry for the simple design im not very good at drawing)
when you're done with that you can remove the line and push them together in as small size as possible.
the left one is frame 0 (pin) and the right one is frame 1 (no pin).
setting up the grenade class
Now that were done with the texture we can start coding, but first we'll need to create the grenade class.

To do so we need to open up out project, go to the right panel and rightclick on your project and select new -> class.

now this window will come up and you'll get to choose what name you'll use.
when you've clicked Add your class will be created for you and the first thing you'll do is to add
"using DuckGame;" at the top of your class.












now add the following code to turn it into a grenade.
Making it your grenade
This is where we start to do some of our own customisation, to start off we'll make it appear in the editor list.
To do so we'll just add a Attribute to our class called editorgroup.
just place the code "[editorgroup("MyMod")]"





When thats done you'll add your name and texture.
The spriteMap is a extension of the normal sprite that will cut your image into smaller pieces and then you can select what part you want to show (frame).


the 8,8 is the height and width of each frame and in this case its in a 7 by 7 (fixed it in the next picture).




when thats done you need to give it a collision size and offset.
the collision offset is how much you want to move the collision starting from the bottom right corner.
i recommend putting the offset to - size divided by 2.




the _timer is the amout of frames it takes before it explodes (def: 1.2)
Debugging the collisionsize
Now we can start up duckgame, but when you try to place your item you'll notice that its not placed correctly in the editor






This will look really bad when you throw it as it spins around the center.
But to fix this all we need to to is add a center value.






the center value is supposed to placed in the center of the collisionbox for it to rotate nicely.
i recommend making it half the collisionsize.

as you can see the grenade is centered in the collisionbox.
making it change sprite
Now to change the sprite we need to find some way to check if the grenade has been activated yet,
luckely theres already a value for it. its the bool _pin.
First of all we need to create the update function just like this:
public override void Update(){
base.Update();}

the Update function is called every frame the grenade is in a level.

And now we have to change our sprites frame to 1 if the pin is gone.
This can be done in multiple ways, one is testing if the pin is false and then changing
if(!_pin){
sprite.frame = 1;}


or the more advanced way (but it takes less code):
sprite.frame = _pin ? 0 : 1;

Your update function should look something like this now:






if you test it out now it should change color.

making it explode when it touches a duck
Now were going to make the grenade somthing more than a regular grenade, we'll make it explode when it hits a duck.

To do so we're going to use the OnSoftImpact() function and then check if it hits a duck.

first we need to add the function, its an override just like the Update function and it should looke something like this:
public override void OnSoftImpact(MaterialThing with, ImpactedFrom from)
{
base.OnSoftImpact(with, from);
}

as you can see theres 2 variables in this the MaterialThing with (the object it hits) and ImpactedFrom from (what side it hit the object on).
first we'll check if the grenade doesnt have a pin with this if statement:

if (!_pin){ }

then we need to check if the thing it hit is a duck, this can be done using the as operator that converts it into another type and then we can check if its null.

if(with as Duck != null){ }

now all we have to do is set the _timer value to 0.

now your onsoftImpact should look like this:

public override void OnSoftImpact(MaterialThing with, ImpactedFrom from)
{
if (!_pin)
{
if(with as Duck != null)
{
_timer = 0;
}
}
base.OnSoftImpact(with, from);
}


You should increase the origional _timer value in the begining now so that it takes longer time for it to explode on its own.

Another useful function is OnRemoved() {} which is called when the grenade gets removed from the level or when it explodes.
full code
[EditorGroup("myMod")]
class tutorialGrenade : Grenade
{
public SpriteMap sprite;
public tutorialGrenade(float x,float y) : base(x,y)
{
sprite = new SpriteMap(GetPath("grenade"),7,7);
graphic = sprite;
_editorName = "testGrenade";

collisionSize = new Vec2(7, 7);
collisionOffset = new Vec2(-3.5f, -3.5f);

_timer = 5f;

center = new Vec2(3.5f, 3.5f);

}

public override void Update()
{
sprite.frame = _pin ? 0 : 1;
base.Update();
}

public override void OnSoftImpact(MaterialThing with, ImpactedFrom from)
{
if (!_pin)
{
if(with as Duck != null)
{
_timer = 0;
}
}
base.OnSoftImpact(with, from);
}
}
5 条留言
Lucky 2022 年 7 月 17 日 下午 2:16 
i figured it out
Lucky 2019 年 2 月 23 日 下午 2:56 
How to make it shot custom bullets instead of normal bullet
|PURPLE|carrick 2018 年 4 月 1 日 上午 3:53 
same
⚡ It's an Oreo!™ ⚡ 2017 年 9 月 11 日 上午 12:24 
Same Sandvich lmao

I was really curious til I saw it and I just n o p e
key lemon spy 2017 年 7 月 13 日 下午 2:26 
i read till the coding part my brain went from :senpai: to :frogzone: