I was transfering an objected oriented code which was written previously on JAVA to dart to test it visually on my device through flutter framework.
I have a class called Person that has two child classes which are SuperPerson (That has two child classes which are SuperHero and Villian and has an Attack Method) and Civil . and i made a method for the SuperHero class which is called protect that is targeted to protect objects from the Civil class.
Right now, i'm trying to make that when the Villian call the Attack method several times on a SuperHero 's object to the point where that object's health get to Zero, it makes the SuperHero object no longer protecting the Civil . and I'm not really sure on how to access Civil 's object so that i can make them unprotected by the SuperHero object after death.
Person Class
class Person {
//civil side
String protector = '';
bool protection = false;
//hero side
String protectedTargetName = '';
bool protecting = false;
//setters
//Civil side
String setProtector(String protector) {
return this.protector = protector;
}
bool setCivilProtection(bool protection) {
return this.protection = protection;
}
//Hero Side
String setProtectedTargetName(String protectedTargetName) {
return this.protectedTargetName = protectedTargetName;
}
bool setHeroProtection(bool protecting) {
return this.protecting = protecting;
}
//getters
//civil side
String getProtector() {
return protector;
}
bool isProtected() {
return protection;
}
//hero side
String getProtectedTargetName() {
return protectedTargetName;
}
bool isProtecting() {
return protecting;
}
}
SuperHero Class
class SuperHero extends SuperPerson
{
SuperHero(String n, int a, String s, String p) : super(n, a, s, p) {
setProtectedTargetName('none');
setHeroProtection(false);
}
void toProtect(Person target, BuildContext context) {
String tName = target.getName();
String pName = getName();
setProtectedTargetName(target.getName());//Hero's side
setHeroProtection(true);//Hero's side
target.setCivilProtection(true);//Civil's side
target.setProtector(getName());//Civil's side
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content: Text('$tName is under $pName\'s protection.'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
void toUnProtect(Person target, BuildContext context) {
String tName = target.getName();
String pName = getName();
setProtectedTargetName('none');//Hero's side
setHeroProtection(false);//Hero's side
target.setCivilProtection(false);//Civil's side
target.setProtector('none');//Civil's side
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content: Text('$tName is no longer under $pName\'s protection'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
Civil Class
class Civil extends Person {
//Constructor
Civil(String n, int a, String s) : super(n, a, s) {
setProtector('None');
setCivilProtection(false);
}
}
Villian Class : Here it only remove the protection from the SuperHero 's side but there still protection on the Civil 's side which i don't know how to access it from here.
void attack(Person target, BuildContext context) {
String tName = target.getName();
String tPronouns = target.pronouns();
String tProtector = target.getProtectorName();
if (!(target.isProtected())) {
super.attack(target, context);
if (target.isProtecting()) {
if (target.getHealth() == 0) {
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content:
Text('$tName is no longer under $tProtector\'s protection'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
//Hero side
target.setProtectedName('none');
target.setHeroProtection(false);
//Supposed to be Civil side but idk how to do it properly
setCivilProtection(false);
setProtectorName('none');
}
}
} else {
final snackBar = SnackBar(
duration: const Duration(milliseconds: 500),
content: Text(
'You can\'t attack $tName, $tPronouns is already under $tProtector\'s protection'),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
Can a SuperHero protect multiple Civils? If so, store a list of Civils that the SuperHero is currently protecting ( List<Civil>
) as an attribute. Whenever a SuperHero dies, remove the protector of all of the members of the list of the corresponding SuperHero.
List<Civil> protectedCivils;
// Whenever you want to remove all of the SuperHero's (target) protection of its Civils.
target.removeProtectedCivils();
// What the method would look like
void removeProtectedCivils() {
for(Civil civil : protectedCivils) civil.removeProtection();
}
Otherwise, if the SuperHero can only protect one Civil, store the currently protected Civil as an attribute in SuperHero and then you can access it whenever you want to remove the protection relationship. Overall, your code looks could be improved by just referencing the objects in question instead of using Strings and Booleans.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.