Skip to content

Commit ed64dc4

Browse files
committed
Rewrite functions that add monsters to players
1 parent 4d77d32 commit ed64dc4

File tree

7 files changed

+259
-807
lines changed

7 files changed

+259
-807
lines changed

Assets/_Project/Scenes/tally-viz-master.unity

Lines changed: 84 additions & 714 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_Project/Scripts/Players/MonsterAddSpritesFrameAnim.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,33 @@ private void Awake ()
2323
frameAnimation = GetComponent<FrameAnimation> ();
2424
spriteRenderer = GetComponent<SpriteRenderer> ();
2525

26-
// test
27-
spriteRenderer.sprite = MonsterManager.Instance.monstersFromSheets [0];
28-
Debug.Log (spriteRenderer.sprite);
29-
30-
// TEMP
31-
32-
frameAnimation.sprites = new Sprite [3];
33-
// pick random value
34-
totalSprites = MonsterManager.Instance.monsters.Length;
35-
// make sure its divisible by 4
36-
spriteIndex = (int)Random.Range (0, totalSprites);
37-
while (spriteIndex % 4 != 0) {
38-
spriteIndex = (int)Random.Range (0, totalSprites);
39-
}
40-
41-
// set the next three as the sprites for this animation
42-
frameAnimation.sprites [0] = MonsterManager.Instance.monstersFromSheets [spriteIndex + 1];
43-
frameAnimation.sprites [1] = MonsterManager.Instance.monstersFromSheets [spriteIndex + 2];
44-
frameAnimation.sprites [2] = MonsterManager.Instance.monstersFromSheets [spriteIndex + 3];
4526

27+
//SetRandomSpritesFromArray ();
4628
}
4729

30+
// old test version, before all monster sprites were combined into three large sheets
31+
//void SetRandomSpritesFromArray ()
32+
33+
// // test
34+
// spriteRenderer.sprite = MonsterIndex.Instance.monsters [0];
35+
// Debug.Log(spriteRenderer.sprite);
36+
37+
// frameAnimation.sprites = new Sprite [3];
38+
// // pick random value
39+
// totalSprites = MonsterIndex.Instance.monsters.Length;
40+
// // make sure its divisible by 4
41+
// spriteIndex = (int) Random.Range (0, totalSprites);
42+
// while (spriteIndex % 4 != 0) {
43+
// spriteIndex = (int) Random.Range (0, totalSprites);
44+
// }
45+
46+
// // set the next three as the sprites for this animation
47+
// frameAnimation.sprites [0] = MonsterIndex.Instance.monsters [spriteIndex + 1];
48+
// frameAnimation.sprites [1] = MonsterIndex.Instance.monsters [spriteIndex + 2];
49+
// frameAnimation.sprites [2] = MonsterIndex.Instance.monsters [spriteIndex + 3];
50+
51+
//}
52+
4853

4954

5055

Assets/_Project/Scripts/Players/MonsterAddSpritesParticleAnim.cs

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,98 @@
44

55

66
/**
7-
* This (somewhat temp) version adds sprites for PARTICLE animation
7+
* Adds sprites for PARTICLE animation
88
*/
99

1010
public class MonsterAddSpritesParticleAnim : MonoBehaviour {
1111

12-
// base class for monster
1312

13+
// temp sprites for assigning monsters
14+
// - when you drag in all the files they will include the texture parent of the sprites so you have to handle this in the picker
15+
//public Sprite [] monsters;
16+
17+
18+
[Space (10)]
19+
[Header ("DETAILS")]
20+
21+
// the mid of this current monster
1422
public int mid;
15-
//public SpriteRenderer spriteRenderer;
16-
//FrameAnimation frameAnimation;
23+
// the index of this mid in the midsInGame
24+
public int gameMidsIndex;
25+
// the index of this monster in the sprite
26+
public int spriteIndex;
1727

1828
public int totalSprites;
19-
public int spriteIndex;
2029

2130
private void Awake ()
2231
{
2332

24-
//frameAnimation = GetComponent<FrameAnimation> ();
25-
//spriteRenderer = GetComponent<SpriteRenderer> ();
26-
27-
// test
28-
//spriteRenderer.sprite = MonsterManager.Instance.monsters [0];
29-
//Debug.Log (spriteRenderer.sprite);
30-
31-
33+
}
3234

3335

36+
public void Init (int _mid)
37+
{
38+
// save the mid and it's position in the index
39+
mid = _mid;
40+
gameMidsIndex = MonsterIndex.Instance.GetGameMidIndex (mid);
3441

42+
// set random sprite
43+
SetSpriteInParticleSystem (mid, gameMidsIndex);
3544

45+
}
3646

37-
// sprites to choose from
38-
totalSprites = MonsterManager.Instance.monsters.Length;
3947

48+
// random
49+
void SetSpriteInParticleSystem (int _mid = -1, int _gameMidsIndex = -1)
50+
{
51+
Debug.Log ("SetRandomSpriteInParticleSystem() mid =" + mid);
4052

41-
// TEMP
53+
// choose random
54+
if (_mid < 1) {
55+
// sprites to choose from
56+
totalSprites = MonsterIndex.Instance.monstersFromSheetsDistinct.Length;
4257

43-
//frameAnimation.sprites = new Sprite [3];
44-
// make sure its divisible by 4
45-
spriteIndex = (int)Random.Range (0, totalSprites);
46-
while (spriteIndex % 4 != 0) {
58+
// make sure its divisible by 3
4759
spriteIndex = (int)Random.Range (0, totalSprites);
60+
while (spriteIndex % 3 != 0) {
61+
spriteIndex = (int)Random.Range (0, totalSprites);
62+
}
63+
} else {
64+
spriteIndex = _gameMidsIndex * 3;
4865
}
4966

50-
// set the next three as the sprites for this animation
51-
//frameAnimation.sprites [0] = MonsterManager.Instance.monsters [spriteIndex + 1];
52-
//frameAnimation.sprites [1] = MonsterManager.Instance.monsters [spriteIndex + 2];
53-
//frameAnimation.sprites [2] = MonsterManager.Instance.monsters [spriteIndex + 3];
54-
55-
5667

68+
// access particle system's TextureSheetAnimationModule
5769
ParticleSystem ps = GetComponent<ParticleSystem> ();
5870
ParticleSystem.TextureSheetAnimationModule tsam = ps.textureSheetAnimation;
59-
tsam.SetSprite (0, MonsterManager.Instance.monsters [spriteIndex + 1]);
60-
tsam.SetSprite (1, MonsterManager.Instance.monsters [spriteIndex + 2]);
61-
tsam.SetSprite (2, MonsterManager.Instance.monsters [spriteIndex + 3]);
71+
// SetSprite on the three
72+
tsam.SetSprite (0, MonsterIndex.Instance.monstersFromSheetsDistinct [spriteIndex + 0]);
73+
tsam.SetSprite (1, MonsterIndex.Instance.monstersFromSheetsDistinct [spriteIndex + 1]);
74+
tsam.SetSprite (2, MonsterIndex.Instance.monstersFromSheetsDistinct [spriteIndex + 2]);
75+
}
6276

6377

6478

65-
}
79+
//// old test version, before all monster sprites were combined into three large sheets
80+
//void SetRandomSpritesFromArray ()
81+
//{
82+
// // sprites to choose from
83+
// totalSprites = MonsterIndex.Instance.monsters.Length;
84+
85+
// // make sure its divisible by 4 (to account for the texture that showed up in the list)
86+
// spriteIndex = (int)Random.Range (0, totalSprites);
87+
// while (spriteIndex % 4 != 0) {
88+
// spriteIndex = (int)Random.Range (0, totalSprites);
89+
// }
90+
// // access particle system's TextureSheetAnimationModule
91+
// ParticleSystem ps = GetComponent<ParticleSystem> ();
92+
// ParticleSystem.TextureSheetAnimationModule tsam = ps.textureSheetAnimation;
93+
// // SetSprite on the three
94+
// tsam.SetSprite (0, MonsterIndex.Instance.monsters [spriteIndex + 1]);
95+
// tsam.SetSprite (1, MonsterIndex.Instance.monsters [spriteIndex + 2]);
96+
// tsam.SetSprite (2, MonsterIndex.Instance.monsters [spriteIndex + 3]);
97+
//}
98+
6699

67100

68101

Assets/_Project/Scripts/Players/MonsterController.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,60 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5+
/**
6+
* Attached to Player GameObject - Adds / removes monsters from the player
7+
*/
58
public class MonsterController : MonoBehaviour {
69

10+
11+
12+
713
public Transform leader; // The character the monsters will follow
814
private List<Transform> monsters = new List<Transform> (); // List of monsters following the leader
9-
public Transform[] spawnPoints; // Array of spawn points for monsters
15+
public Transform [] spawnPoints; // Array of spawn points for monsters
1016
public GameObject monsterPrefab; // Prefab of monsters being instantiated
1117

18+
19+
1220
TallyInputSystem inputs;
1321

1422
// Start is called before the first frame update
1523
void Start ()
1624
{
17-
inputs = new TallyInputSystem();
18-
inputs.Debug.MonsterAdd.performed += ctx => AddMonster();
19-
inputs.Debug.MonsterAdd.Enable();
20-
}
25+
//Debug.Log ("MonsterController " + gameObject.name);
2126

22-
// FixedUpdate is called at a regular interval
23-
void FixedUpdate ()
24-
{
25-
27+
inputs = new TallyInputSystem ();
28+
inputs.Debug.MonsterAdd.performed += ctx => AddMonster ();
29+
inputs.Debug.MonsterAdd.Enable ();
2630
}
2731

32+
33+
2834
// Adds a monster to the circle
2935
public void AddMonster ()
3036
{
31-
// If not at max monsters
32-
if (monsters.Count < 8)
33-
{
34-
// Create new monster and make it a child of the next available spawn point
35-
GameObject monsterObj;
36-
monsterObj = Instantiate(monsterPrefab, spawnPoints[monsters.Count]);
37-
monsters.Add(monsterObj.transform);
38-
}
37+
// if at max monsters
38+
if (monsters.Count >= 8) return;
39+
40+
// get a random mid from those in the game
41+
int mid = MonsterIndex.Instance.gameMids [(int)Random.Range (0, MonsterIndex.Instance.gameMids.Length)];
42+
43+
// Create new monster and make it a child of the next available spawn point
44+
GameObject monsterObj;
45+
monsterObj = Instantiate (monsterPrefab, spawnPoints [monsters.Count]);
46+
47+
// call Init() on monster to get data / display animation
48+
monsterObj.GetComponent<MonsterAddSpritesParticleAnim> ().Init (mid);
49+
50+
// add to list
51+
monsters.Add (monsterObj.transform);
3952
}
53+
54+
// Removes a monster from the circle
55+
public void RemoveMonster (int mid = -1)
56+
{
57+
if (mid < 1) return;
58+
}
59+
60+
4061
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using UnityEngine;
6+
7+
/**
8+
* Index of all the monster sprites, mids, etc.
9+
*/
10+
public class MonsterIndex : Singleton<MonsterIndex> {
11+
// singleton
12+
protected MonsterIndex () { }
13+
//public static new MonsterIndex Instance;
14+
15+
16+
17+
18+
// sprites for assigning monsters
19+
public Sprite [] monstersFromSheets;
20+
// copy of above, with duplicates removed
21+
public Sprite [] monstersFromSheetsDistinct;
22+
// mid index
23+
public int [] gameMids = {
24+
6,15,48,63,82,86,87,89,91,92,96,100,102,110,111,118,121,122,132,135,137,151,154,155,158,161,165,169,170,171,172,174,176,177,178,179,181,184,202,204,205,207,209,210,211,212,215,216,217,218,220,221,222,224,229,231,241,242,243,244,269,271,272,274,277,278,279,281,282,283,300,301,310,315,316,324,325,326,327,329,330,331,332,333,334,335,336,343,346,347,348,350,357,359,360,362,363,370,371,376,381,383,384,385,386,410,418,420,425,443,450,456,466,469,470,472,477,482,492,499,521,545,563,582,584,589,594,596,599,600,601,614,618,620,625,628,630,632,633,635,637,638,640,653,655,673,677,681,683,684
25+
};
26+
27+
28+
private void Awake ()
29+
{
30+
// remove duplicates from the spritesheet array
31+
monstersFromSheetsDistinct = monstersFromSheets.Distinct ().ToArray ();
32+
}
33+
34+
35+
/**
36+
* Return the index of the mid within the midsInGame array
37+
*/
38+
public int GetGameMidIndex (int _mid)
39+
{
40+
Debug.Log ("GetMonsterSpriteIndex() _mid = " + _mid);
41+
//int midSpriteIndex = Array.FindIndex (midsInGame, element => element == _mid);
42+
int midSpriteIndex = Array.IndexOf (gameMids, _mid);
43+
Debug.Log ("GetMonsterSpriteIndex() midSpriteIndex = " + midSpriteIndex);
44+
return midSpriteIndex;
45+
}
46+
47+
48+
49+
50+
}
File renamed without changes.

Assets/_Project/Scripts/Players/MonsterManager.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)