Comments (8)
I took a look at this issue, but I imagine that if an effect that damages the unit is created, it would call the unit.takeDamage which already verifies if the unit died. Am I missing something here? Or maybe I misunderstood the issue.
from warriorjs.
I think this will be clearer if I explain how the turns are played by the units in the floor:
- In the order the units were added to the floor, their
prepareTurn
method is executed. This is when theplayTurn
method of each unit is called, executing the senses immediately but deferring the action for later. Nothing changes in the game at this point; units just analyze their environment to decide what the best course of action is. - In that same order, the
performTurn
method of each unit is executed. This is when the actions that were deferred for later are executed, having impact in the game.
Example:
╔════════╗
║ @s >║
╚════════╝
We have the Warrior there, next to a Sludge. The warrior is full health, but the sludge is about to die (one hit from the Warrior and it's dead). During (1), both the Warrior and the Sludge decide to attack. Later in (2), the Warrior attacks first, killing the Sludge. The Sludge had already decided that it was going to attack, but it wouldn't be ok if he was able to do so because he's now dead. That's why this check exists.
Now, what this issue is about is putting a similar check in place for the effects. The effects are executed here, during performTurn
, and can have an impact on the game (e.g. kill the affected unit).
Example:
Our Warrior is about to die (1 HP) and he's under the "poisoned" effect, that subtracts 2 HP each turn. The poison takes effect here, killing the Warrior. It wouldn't be ok if the Warrior was able to the action after, because he's now dead. So another
isAlive()
check is needed.
Open questions:
-
Where do we put the
isAlive()
check?
a. Once after executing thepassTurn
of each of the effects
b. Once after executing thepassTurn
of all the effects -
Do we want to execute the effects before the action (as it's currently being done) or after?
from warriorjs.
Thanks for that response, it really cleared things up. The isAlive()
check should occur after the execution of each effect, checking if the current health of the player is at or greater than 0. Unless I am wrong, the performTurn method should be written like this for proper implementation.
performTurn() {
if (this.isAlive()) {
this.effects.forEach(effect => effect.passTurn());
if(this.isAlive()){
if (this.turn.action && !this.isBound()) {
const [name, args] = this.turn.action;
this.abilities.get(name).perform(...args);
}
}
else {
this.log(`You have $(this.health) out of $(this.maxHealth). You are dead.`);
}
}
}
from warriorjs.
Hey @PB020, I encourage you to open a PR if you want to try a fix for this! We can discuss the code there with better instruments.
Here are some comments regarding what you already posted though:
this.effects.forEach(effect => effect.passTurn());
if (this.isAlive()) {
There you're only checking once, after all effects have been executed. In order to check after each effect, the check needs to be done inside the forEach
. But to be able to return early, the forEach
needs to be replaced with a for..of
loop.
else {
this.log(`You have $(this.health) out of $(this.maxHealth). You are dead.`);
}
This code is redundant and should be removed. If an effect kills the unit, this will be logged by the effect already.
from warriorjs.
You can use some
if you need to exit early rather than a for..of
loop.
from warriorjs.
@olistic , @PB020 , can i work on this?
from warriorjs.
@icedune Sure! Let me know if you have any questions.
from warriorjs.
It's been a while since the last comment on this, so I opened #250 - hope it works as expected. If there is any case I missed let me know.
from warriorjs.
Related Issues (20)
- Any plans on i18n? HOT 1
- Documentation links are broken in many places HOT 1
- access to newer abilities in previous levels HOT 3
- Connect WarriorJS CLI with warriorjs.com HOT 2
- Add ability to get warrior's max health HOT 3
- brief description on each release HOT 3
- @warriorjs/tower-tick-tick-boom warrior haven't got the "pivot" ability and bashing 'backward' forcedly
- Should the warrior receive more damage when attacked from behind? HOT 1
- Add retro sounds HOT 2
- translate Korean HOT 2
- Korean Translation HOT 3
- Can't access functions from a Unit object
- Watch for Player.js changes and run
- I cannot sign in or create an account for the online platform HOT 2
- When I 'Run' the game after defeated, the state isn't refreshed unless I reload the page for several times HOT 1
- rescue('backward') is undefined HOT 3
- This code is wierdly running HOT 4
- where did constructor() come from and what is it? How does it work and what does it mean? I'm not able to learn anything here HOT 1
- Error: No files matching the pattern "packages/**/src" were found
- Please help me with the algorithm of moves leading to victory in this 9th level on the map "Tick, Tick... Boom!"!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from warriorjs.