Tycho
Tycho
CC#
Created by Tycho on 12/18/2023 in #help
MonoGame - inheritance implementation.
Hello, all! I've been assigned a MonoGame assignment for school and wanted to challenge myself a bit more with some good inheritance implementation. Now, I'm not sure if I'm even using it correctly, which is why I wanted to ask you. Have I implemented it well, are there any upgrades to be made? There are two additional classes under the Obstacle class which I weren't able to add in a new 'file'. Thanks!
19 replies
CC#
Created by Tycho on 9/11/2023 in #help
❔ Trying to shorten this code.
The code works well, but it's a bit dual coded so I've been trying to shorten it. I can't find a good fix, though.
void EnemyRaycastCheck() {
streakScriptReference.HandleStreakBar();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();
hitEnemy2D = hit.transform.GetComponent<Enemy2D>();
hitEnemy2D.ReduceHealth();
if (hitEnemy2D.GetHealth() == 0) {
Destroy(hitEnemy2D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
streakScriptReference.ResetStreak();
}
}
if (Input.GetMouseButtonDown(1)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();

hitEnemy3D = hit.transform.GetComponent<Enemy3D>();
hitEnemy3D.ReduceHealth();
if (hitEnemy3D.GetHealth() == 0) {
Destroy(hitEnemy3D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
}
}
}
void EnemyRaycastCheck() {
streakScriptReference.HandleStreakBar();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();
hitEnemy2D = hit.transform.GetComponent<Enemy2D>();
hitEnemy2D.ReduceHealth();
if (hitEnemy2D.GetHealth() == 0) {
Destroy(hitEnemy2D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
streakScriptReference.ResetStreak();
}
}
if (Input.GetMouseButtonDown(1)) {
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, enemyLayerMask3D)) {
adrenaline++;
adrenalineScriptReference.SetAdrenaline(adrenaline);
adrenalineScriptReference.ImproveAdrenalineBar();

hitEnemy3D = hit.transform.GetComponent<Enemy3D>();
hitEnemy3D.ReduceHealth();
if (hitEnemy3D.GetHealth() == 0) {
Destroy(hitEnemy3D.gameObject);
}
streakScriptReference.ScorePlus();
} else if (Physics.Raycast(ray, out hit, Mathf.Infinity, enemyLayerMask2D)) {
adrenalineScriptReference.DeclineAdrenalineBar();
}
}
}
28 replies