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
6bfe0aa8
Commit
6bfe0aa8
authored
2 years ago
by
Robin Halseth Sandvik
Browse files
Options
Downloads
Patches
Plain Diff
Abstracted EnemyController to work as a root class of all enemies.
parent
9a035b8a
No related branches found
No related tags found
1 merge request
!20
New items system.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
MrBigsock/Assets/Code/Slime/SlimeController.cs
+56
-76
56 additions, 76 deletions
MrBigsock/Assets/Code/Slime/SlimeController.cs
MrBigsock/Assets/Code/orc/EnemyController.cs
+33
-71
33 additions, 71 deletions
MrBigsock/Assets/Code/orc/EnemyController.cs
with
89 additions
and
147 deletions
MrBigsock/Assets/Code/Slime/SlimeController.cs
+
56
−
76
View file @
6bfe0aa8
...
@@ -7,21 +7,21 @@ using System;
...
@@ -7,21 +7,21 @@ using System;
namespace
BigSock
{
namespace
BigSock
{
public
partial
class
SlimeController
:
Charact
er
{
public
partial
class
SlimeController
:
EnemyControll
er
{
public
float
collisionOffset
=
0.05f
;
//
public float collisionOffset = 0.05f;
public
ContactFilter2D
movementFilter
;
//
public ContactFilter2D movementFilter;
List
<
RaycastHit2D
>
castCollisions
=
new
List
<
RaycastHit2D
>();
//
List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
private
Transform
target
;
//
private Transform target;
Animator
m_Animator
;
//
Animator m_Animator;
private
float
distance
;
//
private float distance;
private
float
canAttack
;
//
private float canAttack;
private
EmptyCollider
followCollider
;
//
private EmptyCollider followCollider;
private
EmptyCollider
attackCollider
;
//
private EmptyCollider attackCollider;
private
bool
isInMelee
=
false
;
//
private bool isInMelee = false;
//Rigidbody2D rb;
//Rigidbody2D rb;
...
@@ -63,40 +63,24 @@ namespace BigSock {
...
@@ -63,40 +63,24 @@ namespace BigSock {
public
double
LeapForce
=>
MovementSpeed
*
4
;
public
double
LeapForce
=>
MovementSpeed
*
4
;
void
Start
()
{
//void Start() {
rb
=
GetComponent
<
Rigidbody2D
>();
// rb = GetComponent<Rigidbody2D>();
// m_Animator = gameObject.GetComponent<Animator>();
m_Animator
=
gameObject
.
GetComponent
<
Animator
>();
// followCollider = transform.Find("followCollider").GetComponent<EmptyCollider>();
// followCollider.OnColliderEnter2D_Action += Move_OnColliderEnter2D;
followCollider
=
transform
.
Find
(
"followCollider"
).
GetComponent
<
EmptyCollider
>();
// followCollider.OnColliderStay2D_Action += Move_OnColliderStay2D;
followCollider
.
OnColliderEnter2D_Action
+=
Move_OnColliderEnter2D
;
// followCollider.OnColliderExit2D_Action += Move_OnColliderExit2D;
followCollider
.
OnColliderStay2D_Action
+=
Move_OnColliderStay2D
;
// attackCollider = transform.Find("MeleeCollider").GetComponent<EmptyCollider>();
followCollider
.
OnColliderExit2D_Action
+=
Move_OnColliderExit2D
;
// attackCollider.OnColliderEnter2D_Action += Attack_OnColliderEnter2D;
// attackCollider.OnColliderStay2D_Action += Attack_OnColliderStay2D;
attackCollider
=
transform
.
Find
(
"MeleeCollider"
).
GetComponent
<
EmptyCollider
>();
// attackCollider.OnColliderExit2D_Action += Attack_OnColliderExit2D;
attackCollider
.
OnColliderEnter2D_Action
+=
Attack_OnColliderEnter2D
;
//}
attackCollider
.
OnColliderStay2D_Action
+=
Attack_OnColliderStay2D
;
attackCollider
.
OnColliderExit2D_Action
+=
Attack_OnColliderExit2D
;
}
protected
override
void
Update
()
{
private
void
RotateAnimation
(
Vector2
direction
)
{
if
(
direction
.
x
>
0.01f
){
gameObject
.
GetComponent
<
SpriteRenderer
>().
flipX
=
false
;
}
else
if
(
direction
.
x
<
-
0.01f
){
gameObject
.
GetComponent
<
SpriteRenderer
>().
flipX
=
true
;
}
}
private
void
Update
()
{
if
(
State
==
SlimeState
.
Idle
)
{
if
(
State
==
SlimeState
.
Idle
)
{
// If it has a target and has idled long enough.
// If it has a target and has idled long enough.
if
(
target
!=
null
&&
DateTime
.
Now
>=
NextTimeStateCanChange
)
{
if
(
target
!=
null
&&
DateTime
.
Now
>=
NextTimeStateCanChange
)
{
...
@@ -160,37 +144,36 @@ namespace BigSock {
...
@@ -160,37 +144,36 @@ namespace BigSock {
*/
*/
public
partial
class
SlimeController
{
public
partial
class
SlimeController
{
private
void
Attack_OnColliderEnter2D
(
Collider2D
other
)
{
//private void Attack_OnColliderEnter2D(Collider2D other) {
if
(
other
.
gameObject
.
tag
==
"Player"
)
// if (other.gameObject.tag == "Player")
isInMelee
=
true
;
// isInMelee = true;
}
//}
private
void
Attack_OnColliderStay2D
(
Collider2D
other
)
{
//private void Attack_OnColliderStay2D(Collider2D other) {
var
player
=
other
.
gameObject
.
GetComponent
<
PlayerController
>();
// var player = other.gameObject.GetComponent<PlayerController>();
if
(
player
!=
null
)
{
// if (player != null) {
// Create attack object.
// // Create attack object.
var
attack
=
new
AttackStats
{
// var attack = new AttackStats{
Damage
=
Damage
,
// Damage = Damage,
Knockback
=
KnockbackForce
,
// Knockback = KnockbackForce,
Range
=
0
,
// Range = 0,
AttackSpeed
=
AttackSpeed
,
// AttackSpeed = AttackSpeed,
Source
=
transform
.
position
,
// Source = transform.position,
};
// };
// // Get the player to take the damage.
// Get the player to take the damage.
// if(player.TakeDamage(attack)) {
if
(
player
.
TakeDamage
(
attack
))
{
// //knockback ?
//knockback ?
// //animer nå ?
//animer nå ?
//
// }
}
// }
}
//}
}
//private void Attack_OnColliderExit2D(Collider2D other){
private
void
Attack_OnColliderExit2D
(
Collider2D
other
){
// if (other.gameObject.tag == "Player")
if
(
other
.
gameObject
.
tag
==
"Player"
)
// isInMelee = false;
isInMelee
=
false
;
//}
}
}
}
...
@@ -199,18 +182,15 @@ namespace BigSock {
...
@@ -199,18 +182,15 @@ namespace BigSock {
*/
*/
public
partial
class
SlimeController
{
public
partial
class
SlimeController
{
pr
ivat
e
void
Move_OnColliderEnter2D
(
Collider2D
other
)
{
pr
otected
overrid
e
void
Move_OnColliderEnter2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
//m_Animator.SetTrigger("walk");
//m_Animator.SetTrigger("walk");
target
=
other
.
transform
;
target
=
other
.
transform
;
}
}
}
}
private
void
Move_OnColliderStay2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
}
}
pr
ivat
e
void
Move_OnColliderExit2D
(
Collider2D
other
)
{
pr
otected
overrid
e
void
Move_OnColliderExit2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
{
//m_Animator.SetTrigger("idle");
//m_Animator.SetTrigger("idle");
target
=
null
;
target
=
null
;
...
...
This diff is collapsed.
Click to expand it.
MrBigsock/Assets/Code/orc/EnemyController.cs
+
33
−
71
View file @
6bfe0aa8
...
@@ -10,25 +10,25 @@ namespace BigSock {
...
@@ -10,25 +10,25 @@ namespace BigSock {
public
partial
class
EnemyController
:
Character
{
public
partial
class
EnemyController
:
Character
{
public
float
collisionOffset
=
0.05f
;
public
float
collisionOffset
=
0.05f
;
public
ContactFilter2D
movementFilter
;
public
ContactFilter2D
movementFilter
;
List
<
RaycastHit2D
>
castCollisions
=
new
List
<
RaycastHit2D
>();
protected
List
<
RaycastHit2D
>
castCollisions
=
new
List
<
RaycastHit2D
>();
pr
ivate
Transform
target
;
pr
otected
Transform
target
;
Animator
m_Animator
;
protected
Animator
m_Animator
;
pr
ivate
float
distance
;
pr
otected
float
distance
;
pr
ivate
float
canAttack
;
pr
otected
float
canAttack
;
pr
ivate
EmptyCollider
followCollider
;
pr
otected
EmptyCollider
followCollider
;
pr
ivate
EmptyCollider
attackCollider
;
pr
otected
EmptyCollider
attackCollider
;
pr
ivate
bool
isInMelee
=
false
;
pr
otected
bool
isInMelee
=
false
;
//Rigidbody2D rb;
//Rigidbody2D rb;
// private Collider2D_Proxy secondCollider;
// private Collider2D_Proxy secondCollider;
void
Start
(){
protected
virtual
void
Start
(){
rb
=
GetComponent
<
Rigidbody2D
>();
rb
=
GetComponent
<
Rigidbody2D
>();
m_Animator
=
gameObject
.
GetComponent
<
Animator
>();
m_Animator
=
gameObject
.
GetComponent
<
Animator
>();
...
@@ -50,7 +50,7 @@ namespace BigSock {
...
@@ -50,7 +50,7 @@ namespace BigSock {
pr
ivate
void
RotateAnimation
(
Vector2
direction
)
{
pr
otected
virtual
void
RotateAnimation
(
Vector2
direction
)
{
if
(
direction
.
x
>
0.01f
){
if
(
direction
.
x
>
0.01f
){
gameObject
.
GetComponent
<
SpriteRenderer
>().
flipX
=
false
;
gameObject
.
GetComponent
<
SpriteRenderer
>().
flipX
=
false
;
}
}
...
@@ -59,7 +59,7 @@ namespace BigSock {
...
@@ -59,7 +59,7 @@ namespace BigSock {
}
}
}
}
pr
ivate
void
Update
(){
pr
otected
virtual
void
Update
()
{
if
(
target
!=
null
&&
!
isInMelee
){
if
(
target
!=
null
&&
!
isInMelee
){
/* //walk
/* //walk
...
@@ -78,38 +78,7 @@ namespace BigSock {
...
@@ -78,38 +78,7 @@ namespace BigSock {
}
}
}
}
private
bool
TryMove_OLD
(
Vector2
direction
)
{
if
(
direction
!=
Vector2
.
zero
)
{
// Check for potential collisions
int
count
=
rb
.
Cast
(
direction
,
// X and Y values between -1 and 1 that represent the direction from the body to look for collisions
movementFilter
,
// The settings that determine where a collision can occur on such as layers to collide with
castCollisions
,
// List of collisions to store the found collisions into after the Cast is finished
(
float
)
MovementSpeed
*
Time
.
fixedDeltaTime
+
collisionOffset
);
// The amount to cast equal to the movement plus an offset
//Debug.Log($"cast {string.Join(", ", castCollisions.Select(x => $"{x.collider.isTrigger}"))}");
//Debug.Log($"rb.position : {rb.position}");
//Debug.Log($"target.position : {target.position}");
//Debug.Log($"direction : {direction}");
if
(
count
==
0
){
rb
.
MovePosition
(
rb
.
position
+
direction
*
(
float
)
MovementSpeed
*
Time
.
fixedDeltaTime
);
//print($"rb.position {rb.position}");
//print($"direction {direction}");
RotateAnimation
(
direction
);
return
true
;
}
else
{
return
false
;
}
}
else
{
// Can't move if there's no direction to move in
return
false
;
}
}
}
}
...
@@ -118,14 +87,13 @@ namespace BigSock {
...
@@ -118,14 +87,13 @@ namespace BigSock {
*/
*/
public
partial
class
EnemyController
{
public
partial
class
EnemyController
{
private
void
Attack_OnColliderEnter2D
(
Collider2D
other
){
protected
virtual
void
Attack_OnColliderEnter2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
if
(
other
.
gameObject
.
tag
==
"Player"
)
isInMelee
=
true
;
isInMelee
=
true
;
}
}
pr
ivate
void
Attack_OnColliderStay2D
(
Collider2D
other
)
{
pr
otected
virtual
void
Attack_OnColliderStay2D
(
Collider2D
other
)
{
var
player
=
other
.
gameObject
.
GetComponent
<
PlayerController
>();
var
player
=
other
.
gameObject
.
GetComponent
<
PlayerController
>();
if
(
player
!=
null
)
{
if
(
player
!=
null
)
{
// Create attack object.
// Create attack object.
...
@@ -146,9 +114,9 @@ namespace BigSock {
...
@@ -146,9 +114,9 @@ namespace BigSock {
}
}
}
}
}
}
pr
ivate
void
Attack_OnColliderExit2D
(
Collider2D
other
){
pr
otected
virtual
void
Attack_OnColliderExit2D
(
Collider2D
other
)
{
if
(
other
.
gameObject
.
tag
==
"Player"
)
if
(
other
.
gameObject
.
tag
==
"Player"
)
isInMelee
=
false
;
isInMelee
=
false
;
}
}
}
}
...
@@ -158,32 +126,26 @@ namespace BigSock {
...
@@ -158,32 +126,26 @@ namespace BigSock {
*/
*/
public
partial
class
EnemyController
{
public
partial
class
EnemyController
{
private
void
Move_OnColliderEnter2D
(
Collider2D
other
)
protected
virtual
void
Move_OnColliderEnter2D
(
Collider2D
other
)
{
{
//Debug.Log("enter");
Debug
.
Log
(
"enter"
);
if
(
other
.
gameObject
.
tag
==
"Player"
){
if
(
other
.
gameObject
.
tag
==
"Player"
){
//Debug.Log("enter if");
Debug
.
Log
(
"enter if"
);
m_Animator
.
SetTrigger
(
"walk"
);
m_Animator
.
SetTrigger
(
"walk"
);
target
=
other
.
transform
;
target
=
other
.
transform
;
}
}
}
}
private
void
Move_OnColliderStay2D
(
Collider2D
other
)
protected
virtual
void
Move_OnColliderStay2D
(
Collider2D
other
)
{
{
if
(
other
.
gameObject
.
tag
==
"Player"
){
}
}
}
private
void
Move_OnColliderExit2D
(
Collider2D
other
)
protected
virtual
void
Move_OnColliderExit2D
(
Collider2D
other
)
{
{
if
(
other
.
gameObject
.
tag
==
"Player"
){
if
(
other
.
gameObject
.
tag
==
"Player"
){
m_Animator
.
SetTrigger
(
"idle"
);
m_Animator
.
SetTrigger
(
"idle"
);
target
=
other
.
transform
;
target
=
other
.
transform
;
target
=
null
;
target
=
null
;
}
}
}
}
}
}
...
...
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