Skip to content

Instantly share code, notes, and snippets.

Created June 4, 2017 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/47f35abe717ce37aed28ae952983b3c1 to your computer and use it in GitHub Desktop.
Save anonymous/47f35abe717ce37aed28ae952983b3c1 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RimWorld;
using Verse;
namespace Plague
{
class Projectile_PlagueBullet : Bullet
{
#region Properties
//
public ThingDef Def {
get {
return this.def as ThingDef_PlagueBullet;
}
}
#endregion Properties
#region Overrides
protected override void Impact(Thing hitThing)
{
base.Impact(hitThing);
/*
* Null checking is very important in RimWorld.
* 99% of errors reported are from NullReferenceExceptions (NREs).
* Make sure your code checks if things actually exist, before they
* try to use the code that belongs to said things.
*/
if (Def != null && hitThing != null && hitThing is Pawn hitPawn) //Fancy way to declare a variable inside an if statement. - Thanks Erdelf.
{
var rand = Rand.Value; // This is a random percentage between 0% and 100%
if (rand <= Def.AddHediffChance) // If the percentage falls under the chance, success!
{
/*
* Messages.Message flashes a message on the top of the screen.
* You may be familiar with this one when a colonist dies, because
* it makes a negative sound and mentioneds "So and so has died of _____".
*
* Here, we're using the "Translate" function. More on that later in
* the localization section.
*/
Messages.Message("TST_PlagueBullet_SuccessMessage".Translate(new object[] {
this.launcher.Label, hitPawn.Label
}), MessageSound.Standard);
//This checks to see if the character has a heal differential, or hediff on them already.
var plagueOnPawn = hitPawn?.health?.hediffSet?.GetFirstHediffOfDef(Def.HediffToAdd);
var randomSeverity = Rand.Range(0.15f, 0.30f);
if (plagueOnPawn != null)
{
//If they already have plague, add a random range to its severity.
//If severity reaches 1.0f, or 100%, plague kills the target.
plagueOnPawn.Severity += randomSeverity;
}
else
{
//These three lines create a new health differential or Hediff,
//put them on the character, and increase its severity by a random amount.
Hediff hediff = HediffMaker.MakeHediff(Def.HediffToAdd, hitPawn, null);
hediff.Severity = randomSeverity;
hitPawn.health.AddHediff(hediff, null, null);
}
}
else //failure!
{
/*
* Motes handle all the smaller visual effects in RimWorld.
* Dust plumes, symbol bubbles, and text messages floating next to characters.
* This mote makes a small text message next to the character.
*/
MoteMaker.ThrowText(hitThing.PositionHeld.ToVector3(), hitThing.MapHeld, "TST_PlagueBullet_FailureMote".Translate(Def.AddHediffChance), 12f);
}
}
}
#endregion Overrides
}
}
<?xml version="1.0" encoding="utf-8"?>
<ThingDefs>
<ThingDef Class="Plague.ThingDef_PlagueBullet" ParentName="BaseBullet">
<defName>TST_Bullet_PlagueGun</defName>
<label>plague bullet</label>
<graphicData>
<texPath>Things/Projectile/Bullet_Small</texPath>
<graphicClass>Graphic_Single</graphicClass>
</graphicData>
<projectile>
<flyOverhead>false</flyOverhead>
<damageDef>Bullet</damageDef>
<DamageAmountBase>9</DamageAmountBase>
<Speed>55</Speed>
</projectile>
<AddHediffChance>0.05</AddHediffChance>
<HediffToAdd>Plague</HediffToAdd>
<thingClass>Plague.Projectile_PlagueBullet</thingClass>
</ThingDef>
<ThingDef ParentName="BaseHumanMakeableGun">
<defName>TST_Gun_PlagueGun</defName>
<label>plague gun</label>
<description>A curious weapon notable for its horrible health effects.</description>
<graphicData>
<texPath>Things/Item/Equipment/WeaponRanged/Pistol</texPath>
<graphicClass>Graphic_Single</graphicClass>
</graphicData>
<soundInteract>InteractPistol</soundInteract>
<statBases>
<WorkToMake>15000</WorkToMake>
<Mass>1.2</Mass>
<AccuracyTouch>0.91</AccuracyTouch>
<AccuracyShort>0.71</AccuracyShort>
<AccuracyMedium>0.50</AccuracyMedium>
<AccuracyLong>0.32</AccuracyLong>
<RangedWeapon_Cooldown>1.26</RangedWeapon_Cooldown>
</statBases>
<costList>
<Steel>30</Steel>
<Component>2</Component>
</costList>
<verbs>
<li>
<verbClass>Verb_Shoot</verbClass>
<hasStandardCommand>true</hasStandardCommand>
<projectileDef>TST_Bullet_PlagueGun</projectileDef>
<warmupTime>0.3</warmupTime>
<range>24</range>
<soundCast>ShotPistol</soundCast>
<soundCastTail>GunTail_Light</soundCastTail>
<muzzleFlashScale>9</muzzleFlashScale>
</li>
</verbs>
</ThingDef>
<!-- ====================== BASES ===================== -->
<ThingDef Name="BaseGun" Abstract="True" ParentName="BaseWeapon">
<techLevel>Industrial</techLevel>
<smeltable>true</smeltable>
<thingCategories>
<li>WeaponsRanged</li>
</thingCategories>
<inspectorTabs>
<li>ITab_Art</li>
</inspectorTabs>
<comps>
<li>
<compClass>CompQuality</compClass>
</li>
<li Class="CompProperties_Art">
<nameMaker>ArtName_WeaponGun</nameMaker>
<descriptionMaker>ArtDescription_WeaponGun</descriptionMaker>
<minQualityForArtistic>Excellent</minQualityForArtistic>
</li>
</comps>
</ThingDef>
<ThingDef Name="BaseMakeableGun" ParentName="BaseGun" Abstract="True">
<recipeMaker>
<workSpeedStat>SmithingSpeed</workSpeedStat>
<workSkill>Crafting</workSkill>
<effectWorking>Smith</effectWorking>
<soundWorking>Recipe_Smith</soundWorking>
<recipeUsers>
<li>TableMachining</li>
</recipeUsers>
<unfinishedThingDef>UnfinishedGun</unfinishedThingDef>
</recipeMaker>
</ThingDef>
<ThingDef Name="BaseHumanGun" ParentName="BaseGun" Abstract="True">
<weaponTags>
<li>Gun</li>
</weaponTags>
</ThingDef>
<ThingDef Name="BaseHumanMakeableGun" ParentName="BaseMakeableGun" Abstract="True">
<weaponTags>
<li>Gun</li>
</weaponTags>
</ThingDef>
<ThingDef Name="BaseBullet" Abstract="True">
<category>Projectile</category>
<tickerType>Normal</tickerType>
<altitudeLayer>Projectile</altitudeLayer>
<thingClass>Bullet</thingClass>
<label>bullet</label>
<useHitPoints>False</useHitPoints>
<neverMultiSelect>True</neverMultiSelect>
<graphicData>
<shaderType>Transparent</shaderType>
</graphicData>
</ThingDef>
</ThingDefs>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RimWorld;
using Verse;
namespace Plague
{
public class ThingDef_PlagueBullet : ThingDef
{
public float AddHediffChance = 0.05f; //The default chance of adding a hediff.
public HediffDef HediffToAdd = HediffDefOf.Plague;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment