Skip to content

Commit 9df00be

Browse files
committed
adding interfaces to make it more extendable
1 parent a7ab1a7 commit 9df00be

11 files changed

Lines changed: 136 additions & 49 deletions

File tree

DataStructureEdGame/Assets/Scripts/GameController.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55
using UnityEngine.UI;
6+
using Assets.Scripts.GameObject.Interfaces;
67

78
public class GameController : MonoBehaviour {
89

@@ -165,7 +166,7 @@ void Update()
165166
positionMcPosition.z = 0;
166167
toBeAdded.transform.position = positionMcPosition;
167168
toBeAdded.gameObject.SetActive(true);
168-
selectedLink.setConnectingPlatform(toBeAdded); // connect the selected link to the new platform
169+
selectedLink.setConnectingEntity(toBeAdded); // connect the selected link to the new platform
169170
// end adding platform
170171
addingPlatforms = false;
171172
// append code and log
@@ -293,7 +294,7 @@ void Update()
293294
//Debug.Log("millisecond diff: " + (DateTime.Now.Millisecond - lastTimeClickedMillis.Millisecond));
294295
if ((DateTime.Now.Millisecond - lastTimeClickedMillis.Millisecond) < doubleClickDelay) {
295296
codePanelBehavior.appendCodeText(selectedLink.getCodeVariableString() + " = null;"); // still counds as setting it as null if it is already null;
296-
if (hoverLinkRef.connectingPlatform != null) {
297+
if (hoverLinkRef.connectingEntity != null) {
297298
hoverLinkRef.removeLinkConnection();
298299

299300
string actMsg = "the link block " + selectedLink.logId + " that was double clicked had an existing link so now it is deleted";
@@ -329,9 +330,9 @@ void Update()
329330
{
330331
selectedLink.removeLinkConnection();
331332
}
332-
if (hoverLinkRef.connectingPlatform != null && hoverLinkRef.connectingPlatform != selectedLink.containerPlatform)
333+
if (hoverLinkRef.connectingEntity != null && (PlatformBehavior)hoverLinkRef.connectingEntity != selectedLink.containerPlatform)
333334
{
334-
selectedLink.setConnectingPlatform(hoverLinkRef.connectingPlatform);
335+
selectedLink.setConnectingEntity(hoverLinkRef.connectingEntity);
335336
}
336337
removeHoverArrow();
337338
removeHoverLink();
@@ -390,19 +391,20 @@ bool isWinConditonSatisfied()
390391
}
391392
//Debug.Log(debugFrameCount + " | WIN CONDITION: Number of platforms in the level: " + numberOfTotalPlatformsInLevel);
392393

393-
if (startingLink.connectingPlatform == null)
394+
if (startingLink.connectingEntity == null)
394395
{
395396
return (numberOfTotalPlatformsInLevel == 0); // if there are no platforms in the level
396397
}
397-
else if (startingLink.connectingPlatform.childLink.GetComponent<LinkBlockBehavior>().connectingPlatform == null)
398+
else if (startingLink.connectingEntity is PlatformBehavior &&
399+
((PlatformBehavior)startingLink.connectingEntity).childLink.GetComponent<LinkBlockBehavior>().connectingEntity == null)
398400
{
399401
return (numberOfTotalPlatformsInLevel == 1); // if there is only one platform in the level
400402
}
401403
sizeOfList = 1;
402-
PlatformBehavior temp = startingLink.connectingPlatform.GetComponent<PlatformBehavior>();
404+
ValueEntity temp = ((MonoBehaviour)startingLink.connectingEntity).GetComponent<PlatformBehavior>();
403405
while (temp != null)
404406
{
405-
PlatformBehavior next = temp.childLink.GetComponent<LinkBlockBehavior>().connectingPlatform;
407+
ValueEntity next = (ValueEntity)((PlatformBehavior)temp).childLink.GetComponent<LinkBlockBehavior>().connectingEntity;
406408
if (next == null)
407409
{
408410
break;
@@ -438,33 +440,34 @@ bool isWinConditonSatisfied()
438440
}
439441
}
440442

441-
if (startingLink.connectingPlatform == null)
443+
if (startingLink.connectingEntity == null)
442444
{
443445
return (listSize == 0); // if there are no platforms in the level
444446
}
445-
else if (startingLink.connectingPlatform.childLink.GetComponent<LinkBlockBehavior>().connectingPlatform == null)
447+
else if (startingLink.connectingEntity is PlatformBehavior &&
448+
((PlatformBehavior)startingLink.connectingEntity).childLink.GetComponent<LinkBlockBehavior>().connectingEntity == null)
446449
{
447450
return (listSize == 1); // if there is only one platform in the level
448451
}
449452
//start traversing the list and comparing the current and next platform's values
450-
PlatformBehavior tempDupl = startingLink.connectingPlatform.GetComponent<PlatformBehavior>();
453+
ValueEntity tempDupl = ((MonoBehaviour)startingLink.connectingEntity).GetComponent<PlatformBehavior>();
451454
//check student's list to make a dictionary...
452455
Dictionary<int, int> PlayersList = new Dictionary<int, int>();
453456
while (tempDupl != null)
454457
{
455-
if (PlayersList.ContainsKey(tempDupl.getValue()))
458+
if (PlayersList.ContainsKey((int)tempDupl.getValue()))
456459
{
457460
return false;
458461
}
459-
else if (!PlayersList.ContainsKey(tempDupl.getValue()) && PlayersList.Count < listSize)
462+
else if (!PlayersList.ContainsKey((int)tempDupl.getValue()) && PlayersList.Count < listSize)
460463
{
461-
PlayersList.Add(tempDupl.getValue(), 1);
464+
PlayersList.Add((int)tempDupl.getValue(), 1);
462465
if ((PlayersList.Count == unique.Count) && PlayersList.Count == listSize)
463466
{
464467
return true;
465468
}
466469
}
467-
PlatformBehavior next = tempDupl.childLink.GetComponent<LinkBlockBehavior>().connectingPlatform;
470+
ValueEntity next = (ValueEntity)((PlatformBehavior)tempDupl).childLink.GetComponent<LinkBlockBehavior>().connectingEntity;
468471
if(next != null)
469472
{
470473
if(next.getValue() < tempDupl.getValue())
@@ -527,7 +530,7 @@ public void removeHoverLink()
527530
}
528531
// remove the "bridge" from the link we are no longer setting as the hover link.
529532
// so that its collision is now normal.
530-
if (hoverLinkRef.connectingPlatform != null)
533+
if (hoverLinkRef.connectingEntity != null)
531534
{
532535
if (enableLinkChaining)
533536
{
@@ -547,8 +550,8 @@ public void removeHoverLink()
547550
{
548551
previousNotNullHoverLinkRef.containerPlatform.updatePlatformValuesAndSprite();
549552
}
550-
if (previousNotNullHoverLinkRef.connectingPlatform != null) {
551-
previousNotNullHoverLinkRef.connectingPlatform.updatePlatformValuesAndSprite();
553+
if (previousNotNullHoverLinkRef.connectingEntity != null && previousNotNullHoverLinkRef.connectingEntity is PlatformBehavior) {
554+
((PlatformBehavior)previousNotNullHoverLinkRef.connectingEntity).updatePlatformValuesAndSprite();
552555
}
553556
}
554557
}
@@ -570,7 +573,7 @@ public void setHoverLink(ref LinkBlockBehavior lb)
570573

571574
// Make a red X appear over the arrow of the selected link connection if the selected link connection will be removed
572575
// if the connection establishing action is complete.
573-
if (selectedLink != null && selectedLink.connectingPlatform != null && (hoverLinkRef.connectingPlatform == null || selectedLink.connectingPlatform != hoverLinkRef.connectingPlatform))
576+
if (selectedLink != null && selectedLink.connectingEntity != null && (hoverLinkRef.connectingEntity == null || selectedLink.connectingEntity != hoverLinkRef.connectingEntity))
574577
{
575578
if (deleteSpriteRef != null)
576579
{
@@ -582,23 +585,23 @@ public void setHoverLink(ref LinkBlockBehavior lb)
582585
}
583586

584587
// conditions for "bridging": there is a select link and a hover link, and they are not equal
585-
if (selectedLink != null && hoverLinkRef != null && selectedLink != hoverLinkRef && hoverLinkRef.connectingPlatform != null)
588+
if (selectedLink != null && hoverLinkRef != null && selectedLink != hoverLinkRef && hoverLinkRef.connectingEntity != null)
586589
{
587590
hoverLinkRef.setDisplayMarker(true);
588591
// draw the faded arrow
589592
Color c = Color.gray;
590593
c.a = 0.3f;
591-
Transform[] hoverArrowParts = createArrowInstanceBetweenLinkPlatform(selectedLink, hoverLinkRef.connectingPlatform, c);
594+
Transform[] hoverArrowParts = createArrowInstanceBetweenLinkPlatform(selectedLink, hoverLinkRef.connectingEntity, c);
592595
hoverArrowLine = hoverArrowParts[0];
593596
hoverArrowHead = hoverArrowParts[1];
594597

595598
// This is when the mouse is hovering over a link for establishing a link.
596599
// update and create a "bridge" from this link to the next for next->next->.. option.
597-
if (hoverLinkRef.connectingPlatform != null)
600+
if (hoverLinkRef.connectingEntity != null && hoverLinkRef.connectingEntity is PlatformBehavior)
598601
{
599602
if (enableLinkChaining) {
600603
// Create a "bridge" from this link to the next link
601-
Bounds otherBounds = hoverLinkRef.connectingPlatform.childLink.GetComponent<SpriteRenderer>().bounds;
604+
Bounds otherBounds = ((PlatformBehavior)hoverLinkRef.connectingEntity).childLink.GetComponent<SpriteRenderer>().bounds;
602605
Bounds hoverBounds = hoverLinkRef.GetComponent<SpriteRenderer>().bounds;
603606
Vector3 worldDiffNorm = (otherBounds.center - hoverBounds.center).normalized;
604607

@@ -630,7 +633,9 @@ public void setHoverLink(ref LinkBlockBehavior lb)
630633
hoverLinkRef.GetComponent<LineRenderer>().startWidth = scalePerpScale - 0.2f; // make the path look a little smaller than it really is
631634
hoverLinkRef.GetComponent<LineRenderer>().endWidth = scalePerpScale - 0.2f;
632635

633-
hoverLinkRef.connectingPlatform.updatePlatformValuesAndSprite();
636+
if (hoverLinkRef.connectingEntity is PlatformBehavior) {
637+
((PlatformBehavior)hoverLinkRef.connectingEntity).updatePlatformValuesAndSprite();
638+
}
634639
}
635640
} // end creating the "bridge"
636641
}
@@ -730,11 +735,11 @@ public void clearReferenceLists()
730735
/**
731736
* Instantiate an arrow that goes from the given link block to the given platform using the given color.
732737
*/
733-
public Transform[] createArrowInstanceBetweenLinkPlatform(LinkBlockBehavior lb, PlatformBehavior pb, Color color)
738+
public Transform[] createArrowInstanceBetweenLinkPlatform(LinkBlockBehavior lb, ConnectableEntity ce, Color color)
734739
{
735740
// determine the start and end points of the arrow.
736741
Bounds linkBounds = lb.GetComponent<SpriteRenderer>().bounds;
737-
Bounds platBounds = pb.GetComponent<SpriteRenderer>().bounds;
742+
Bounds platBounds = ((MonoBehaviour)ce).GetComponent<SpriteRenderer>().bounds;
738743

739744
// find the closest points on both bounding boxes to the center point to make the arrow.
740745
Vector3 betweenPoint = new Vector3((linkBounds.center.x + platBounds.center.x) / 2,

DataStructureEdGame/Assets/Scripts/GameObject/HelicopterRobotBehavior.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void Update() {
2525
Vector3 moveDir = (targetLocation - transform.position).normalized;
2626
// move, lerping based on the distance away
2727
transform.Translate(Vector3.Lerp(new Vector3(), (moveDir * flySpeed * Time.deltaTime), distAway / 12f));
28-
if (childLink != null && childLink.GetComponent<LinkBlockBehavior>().connectingPlatform != null)
28+
if (childLink != null && childLink.GetComponent<LinkBlockBehavior>().connectingEntity != null)
2929
{
3030
childLink.GetComponent<LinkBlockBehavior>().setRenderArrow(false);
3131
// childLink.GetComponent<LinkBlockBehavior>().UpdateLinkArrow(); // refresh its position as it moves
@@ -38,10 +38,10 @@ void Update() {
3838

3939
public void MoveAboveLinkedPlatform()
4040
{
41-
if (childLink.GetComponent<LinkBlockBehavior>().connectingPlatform != null)
41+
if (childLink.GetComponent<LinkBlockBehavior>().connectingEntity != null)
4242
{
4343
// move above the platform
44-
targetLocation = childLink.GetComponent<LinkBlockBehavior>().connectingPlatform.transform.position + (new Vector3(0, 3, 0));
44+
targetLocation = ((MonoBehaviour)childLink.GetComponent<LinkBlockBehavior>().connectingEntity).transform.position + (new Vector3(0, 3, 0));
4545
}
4646
}
4747

DataStructureEdGame/Assets/Scripts/GameObject/Interfaces.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assets.Scripts.GameObject.Interfaces
7+
{
8+
/**
9+
* A general interface that links can connect to.
10+
*/
11+
public interface ConnectableEntity
12+
{
13+
bool isPhasedOut();
14+
bool isHidden();
15+
string getLogID();
16+
void removeIncomingConnectingLink(LinkBlockBehavior lb);
17+
void addIncomingConnectingLink(LinkBlockBehavior lb);
18+
}
19+
}

DataStructureEdGame/Assets/Scripts/GameObject/Interfaces/ConnectableEntity.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assets.Scripts.GameObject.Interfaces
7+
{
8+
public interface ValueEntity
9+
{
10+
float getValue();
11+
}
12+
}

DataStructureEdGame/Assets/Scripts/GameObject/Interfaces/ValueEntity.cs.meta

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

DataStructureEdGame/Assets/Scripts/GameObject/LinkBlockBehavior.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44
using UnityEngine.UI;
5+
using Assets.Scripts.GameObject.Interfaces;
56

67
public class LinkBlockBehavior : MonoBehaviour
78
{
@@ -10,7 +11,7 @@ public class LinkBlockBehavior : MonoBehaviour
1011
public GameController gameController;
1112

1213
public PlatformBehavior containerPlatform; // if this link block is in a platform, then this is the parent
13-
public PlatformBehavior connectingPlatform; // this is the platform object this link is pointing to.
14+
public ConnectableEntity connectingEntity; // this is the platform object this link is pointing to.
1415

1516
// references to the arrow instances
1617
public Transform linkArrow; // this is the current arrow that is instantiated
@@ -54,7 +55,7 @@ public void UpdateLinkArrow()
5455
{
5556
removeArrowBetween();
5657

57-
if (connectingPlatform == null) // only update the sprite if there is no connection
58+
if (connectingEntity == null) // only update the sprite if there is no connection
5859
{
5960
if (GetComponent<SpriteRenderer>().sprite != nullLinkSprite)
6061
{
@@ -67,11 +68,11 @@ public void UpdateLinkArrow()
6768
}
6869
}
6970

70-
if (renderArrow && connectingPlatform != null)
71+
if (renderArrow && connectingEntity != null)
7172
{
7273
// set the arrow color
7374
Color color = Color.red;
74-
if ((containerPlatform != null && containerPlatform.isPhasedOut) || (isHelicopterLink && connectingPlatform.isPhasedOut))
75+
if ((containerPlatform != null && containerPlatform.isPhasedOut) || (isHelicopterLink && connectingEntity.isPhasedOut()))
7576
{
7677
color = Color.gray; // arrowPreFab = linkArrowFadedPreFab;
7778
}
@@ -80,7 +81,7 @@ public void UpdateLinkArrow()
8081
color = Color.yellow; // arrowPreFab = linkArrowHelicopterPreFab;
8182
}
8283
// create the arrow and save references to the new instances.
83-
Transform[] linkArrowParts = gameController.createArrowInstanceBetweenLinkPlatform(this, connectingPlatform, color);
84+
Transform[] linkArrowParts = gameController.createArrowInstanceBetweenLinkPlatform(this, connectingEntity, color);
8485
linkArrow = linkArrowParts[0];
8586
linkArrowHead = linkArrowParts[1];
8687
} // end render arrow
@@ -107,28 +108,28 @@ public void removeArrowBetween()
107108
*/
108109
public bool isConnectedToPlatform()
109110
{
110-
return connectingPlatform != null;
111+
return connectingEntity != null;
111112
}
112113

113114
/**
114115
* Remove the connection from this link to the current platform. Precondtion: isConnectedToPlatform() is true.
115116
*/
116117
public void removeLinkConnection()
117118
{
118-
connectingPlatform.removeIncomingConnectingLink(this); // set connect platform null to make render update correctly
119-
connectingPlatform = null;
119+
connectingEntity.removeIncomingConnectingLink(this); // set connect platform null to make render update correctly
120+
connectingEntity = null;
120121
UpdateLinkArrow();
121122
}
122123

123124
/**
124125
* Set the playform this is going to be linking to.
125126
*/
126-
public void setConnectingPlatform(PlatformBehavior platform)
127+
public void setConnectingEntity(ConnectableEntity entity)
127128
{
128-
if (platform != null)
129+
if (entity != null)
129130
{
130-
connectingPlatform = platform;
131-
connectingPlatform.addIncomingConnectingLink(this);
131+
connectingEntity = entity;
132+
connectingEntity.addIncomingConnectingLink(this);
132133
if (isHelicopterLink) // if the link belongs to the helicopter robot...
133134
{
134135
gameController.helicopterRobotRef.GetComponent<HelicopterRobotBehavior>().MoveAboveLinkedPlatform();

0 commit comments

Comments
 (0)