-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add quarantine state #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ | |
] | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ import { | |
BALL_RADIUS, | ||
COLORS, | ||
MORTALITY_PERCENTATGE, | ||
SYMPTOMATIC_PERCENTAGE, | ||
TICKS_TO_RECOVER, | ||
RUN, | ||
SPEED, | ||
MAXSPEED, | ||
STATES | ||
} from './options.js' | ||
import { checkCollision, calculateChangeDirection } from './collisions.js' | ||
|
@@ -25,16 +27,19 @@ export class Ball { | |
this.hasCollision = true | ||
this.survivor = false | ||
this.hasAppInstalled = hasAppInstalled | ||
this.contacts = [] | ||
this.feelSick = null | ||
} | ||
|
||
checkState () { | ||
if (this.state === STATES.infected) { | ||
if (this.state === STATES.infected || this.state === STATES.quarantine) { | ||
if (RUN.filters.death && !this.survivor && this.timeInfected >= TICKS_TO_RECOVER / 2) { | ||
this.survivor = this.sketch.random(100) >= MORTALITY_PERCENTATGE | ||
if (!this.survivor) { | ||
this.hasMovement = false | ||
const oldState = this.state | ||
this.state = STATES.death | ||
RUN.results[STATES.infected]-- | ||
RUN.results[oldState]-- | ||
RUN.results[STATES.death]++ | ||
return | ||
} | ||
|
@@ -44,14 +49,44 @@ export class Ball { | |
} | ||
|
||
if (this.timeInfected >= TICKS_TO_RECOVER) { | ||
const oldState = this.state | ||
this.state = STATES.recovered | ||
RUN.results[STATES.infected]-- | ||
RUN.results[oldState]-- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Puo' essere un valore diverso da infected? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
RUN.results[STATES.recovered]++ | ||
this.hasMovement = true | ||
} else { | ||
this.timeInfected++ | ||
} | ||
} | ||
|
||
// after a while it may feel sick | ||
if (this.state === STATES.infected) { | ||
if (this.feelSick === null && this.timeInfected >= TICKS_TO_RECOVER / 3) { | ||
this.feelSick = SYMPTOMATIC_PERCENTAGE >= this.sketch.random(100) | ||
if (this.feelSick) { | ||
this.state = STATES.quarantine | ||
RUN.results[STATES.infected]-- | ||
RUN.results[STATES.quarantine]++ | ||
return | ||
} | ||
} | ||
} | ||
|
||
// notify quarantine state to all contacts having app | ||
if (this.state === STATES.quarantine) { | ||
if (this.hasAppInstalled) { | ||
for (let i = 0; i < this.contacts.length; i++) { | ||
const otherBall = this.contacts[i] | ||
if (otherBall.hasAppInstalled && otherBall.state === STATES.infected) { | ||
otherBall.contacts.splice(otherBall.contacts.indexOf(this), 1) | ||
otherBall.state = STATES.quarantine | ||
RUN.results[STATES.infected]-- | ||
RUN.results[STATES.quarantine]++ | ||
} | ||
} | ||
this.contacts = [] | ||
} | ||
} | ||
} | ||
|
||
checkCollisions ({ others }) { | ||
|
@@ -73,13 +108,25 @@ export class Ball { | |
otherBall.vx = ax | ||
otherBall.vy = ay | ||
|
||
// limit vx and vy to MAXSPEED | ||
this.vx = Math.max(Math.min(this.vx, MAXSPEED), -MAXSPEED) | ||
this.vy = Math.max(Math.min(this.vy, MAXSPEED), -MAXSPEED) | ||
otherBall.vx = Math.max(Math.min(otherBall.vx, MAXSPEED), -MAXSPEED) | ||
otherBall.vy = Math.max(Math.min(otherBall.vy, MAXSPEED), -MAXSPEED) | ||
|
||
// both has same state, so nothing to do | ||
if (this.state === state) return | ||
// if any is recovered, then nothing happens | ||
if (this.state === STATES.recovered || state === STATES.recovered) return | ||
// if any is in quarantine, then nothing happens | ||
if (this.state === STATES.quarantine || state === STATES.quarantine) return | ||
// then, if some is infected, then we make both infected | ||
if (this.state === STATES.infected || state === STATES.infected) { | ||
if (this.hasAppInstalled && otherBall.hasAppInstalled) return | ||
// this and otherBall came in touch, update their contact list | ||
if (this.contacts.indexOf(otherBall) === -1) { | ||
this.contacts.push(otherBall) | ||
otherBall.contacts.push(this) | ||
} | ||
this.state = otherBall.state = STATES.infected | ||
RUN.results[STATES.infected]++ | ||
RUN.results[STATES.well]-- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potresti auitarmi a capire contacts? Da quello che visto dal codice sarebbe un peopleEnteredInContact, corretto?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes,
contact list
likecontact-tracing
. It's the list of other balls this ball came in touch while it's sick.