Code Monkey home page Code Monkey logo

abstracted-weapon-interface's Introduction

Abstracted-Weapon-Interface

PoC on how one can abstract implementation details of weapons/attacking using OOP design patterns in C# (Unity Engine)

This code uses a factory to abstract "low-level" details such as attacking with a weapon. In this repo it's demonstrated with attack visuals.

Usage:

Simply add your own logic to WeaponBase.cs, and attach Weapon.cs to your weapon prefab!

How it works:

WeaponBase.cs defines some variables and methods for the weapon and exposes them to WeaponFactory.cs.

These implementation details are then exposed (via inheritance) to Weapon.cs as an ActiveWeaponHandle

Not only does this provide much cleaner code, it is also faster as it removes the need for any conditional statements to check which type of weapon code/logic is required:

The "Regular" way:

void Attack()
{
    // Note that this process must often be repeated for different functions thus violating the DRY principal.
    if (IsMelee)
    {
        // Melee weapon code
    }
    else
    {
        // Non-melee weapon code
    }
}

The factory way (i.e. this repository):

void Attack()
{
    /*
        Under the hood `ActiveWeaponHandle` is just a getter:
            protected OpaqueWeapon ActiveWeaponHandle { get => m_Factories[(byte)m_CurrentMapping].Handle; }
    */
    ActiveWeaponHandle.AttackVisuals(); // Particle effects, sounds, etc
    ActiveWeaponHandle.AttackLogic();   // Raycasts, hitbox detection, etc
}

abstracted-weapon-interface's People

Contributors

v01d-null avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.