Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Mr BigSock
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gard Aleksander Furre
Mr BigSock
Commits
f328c63c
Commit
f328c63c
authored
2 years ago
by
Robin Halseth Sandvik
Browse files
Options
Downloads
Patches
Plain Diff
Added AbilityService.
- Handles Abilities and controlling them.
parent
92bc6971
No related branches found
Branches containing commit
No related tags found
1 merge request
!25
Ability rework.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
MrBigsock/Assets/Code/Services/AbilityService.cs
+85
-0
85 additions, 0 deletions
MrBigsock/Assets/Code/Services/AbilityService.cs
MrBigsock/Assets/Code/Services/AbilityService.cs.meta
+11
-0
11 additions, 0 deletions
MrBigsock/Assets/Code/Services/AbilityService.cs.meta
with
96 additions
and
0 deletions
MrBigsock/Assets/Code/Services/AbilityService.cs
0 → 100644
+
85
−
0
View file @
f328c63c
using
System.Collections
;
using
System
;
using
System.Linq
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
UnityEngine
;
using
UnityEngine.InputSystem
;
using
BigSock.Item
;
namespace
BigSock.Service
{
/*
Service for handling abilities.
*/
public
partial
class
AbilityService
{
/*
The instance to use.
*/
public
static
readonly
AbilityService
SINGLETON
=
new
AbilityService
();
/*
Get an instance of the ability of the given id.
*/
public
IAbility
Get
(
ulong
id
)
{
if
(
_abilities
.
TryGetValue
(
id
,
out
var
res
))
return
_new
(
res
);
return
null
;
}
/*
Get a random ability from the ability pool.
*/
public
IAbility
GetRandom
()
{
var
num
=
_rnd
.
Next
(
_abilityList
.
Count
);
return
_new
(
_abilityList
[
num
]);
}
}
public
partial
class
AbilityService
{
private
Dictionary
<
ulong
,
IAbility
>
_abilities
=
new
Dictionary
<
ulong
,
IAbility
>();
private
List
<
IAbility
>
_abilityList
=
new
List
<
IAbility
>();
private
System
.
Random
_rnd
=
new
System
.
Random
();
private
AbilityService
()
{
_loadItems
();
}
/*
Load the abilities into the dictionary.
Reflection code: https://stackoverflow.com/a/6944605
*/
private
void
_loadItems
()
{
// Get the classs that inherit the item base class.
var
types
=
Assembly
.
GetAssembly
(
typeof
(
BaseAbility
))
.
GetTypes
()
.
Where
(
myType
=>
myType
.
IsClass
&&
!
myType
.
IsAbstract
&&
myType
.
IsSubclassOf
(
typeof
(
BaseAbility
)));
// Create list of instances.
_abilityList
=
types
.
Select
(
t
=>
(
IAbility
)
Activator
.
CreateInstance
(
t
,
new
object
[
0
]))
.
ToList
();
// Map to a dictionary by their ids.
_abilities
=
_abilityList
.
ToDictionary
(
t
=>
t
.
Id
);
foreach
(
var
a
in
_abilityList
)
{
Debug
.
Log
(
$"[AbilityService._loadItems()]
{
a
.
Id
}
"
);
}
}
/*
Creates a new instance of the object.
*/
private
IAbility
_new
(
IAbility
obj
)
=>
(
IAbility
)
Activator
.
CreateInstance
(
obj
.
GetType
(),
new
object
[
0
]);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
MrBigsock/Assets/Code/Services/AbilityService.cs.meta
0 → 100644
+
11
−
0
View file @
f328c63c
fileFormatVersion: 2
guid: accabe59812fa39448d6bd558041692d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment