phetsims / vegas

Reusable game components for PhET simulations.
MIT License
1 stars 4 forks source link

Factor out game enum/states/logic? #74

Open samreid opened 6 years ago

samreid commented 6 years ago

From https://github.com/phetsims/area-model-common/issues/99

// Copyright 2017-2018, University of Colorado Boulder

/**
 * Enumeration for all states that the game can be in.
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 * REVIEW: These states and corresponding logic are duplicated across many games.  Do we have the potential to factor something out?
 * REVIEW*: Probably not easily, as showing a "correct" solution, giving two attempts, and having a "level complete"
 * REVIEW*: state are not-at-all universal for game sims.
 */
define( function( require ) {
  'use strict';

  // modules
  var areaModelCommon = require( 'AREA_MODEL_COMMON/areaModelCommon' );

  var GameState = {
    // "check" button, editable
    FIRST_ATTEMPT: 'FIRST_ATTEMPT',
    SECOND_ATTEMPT: 'SECOND_ATTEMPT',

    // "next" button, happy face with +1 or +2 depending on score. NOT editable
    CORRECT_ANSWER: 'CORRECT_ANSWER',

    // "try again" button, sad face (editable?) - triggers next state on edit?
    WRONG_FIRST_ANSWER: 'WRONG_FIRST_ANSWER',

    // "show solution" button, sad face (editable?) - no trigger on edit?
    WRONG_SECOND_ANSWER: 'WRONG_SECOND_ANSWER',

    // "next" button, NOT editable, replaced with a solution
    SHOW_SOLUTION: 'SHOW_SOLUTION',

    LEVEL_COMPLETE: 'LEVEL_COMPLETE'
  };

  areaModelCommon.register( 'GameState', GameState );

  // @public {Array.<GameState>} - All values the enumeration can take.
  GameState.VALUES = [
    GameState.FIRST_ATTEMPT,
    GameState.SECOND_ATTEMPT,
    GameState.CORRECT_ANSWER,
    GameState.WRONG_FIRST_ANSWER,
    GameState.WRONG_SECOND_ANSWER,
    GameState.SHOW_SOLUTION,
    GameState.LEVEL_COMPLETE
  ];

  // verify that enum is immutable, without the runtime penalty in production code
  if ( assert ) { Object.freeze( GameState ); }

  return GameState;
} );

I've seen several games that use these states, should this or corresponding logic be factored out? I'll run this past @jbphet first, since he has familiarity with vegas and some games.

jbphet commented 6 years ago

I had this same thought a while back and tried it in Area Builder, please see the file QuizGameModel.js. It was an attempt to do - I think - exactly what you're describing here, i.e. extract the logic for the basic flow of the game into a reusable object or set of objects. In the end, I found what I'd implemented a little awkward because there was some sim-specific logic that needed to be hooked up somehow, and I did that, and it worked, but I didn't feel like it worked well enough that I wanted to move it into the vegas repo. Please take a look and, if you think it would be worth the effort, we could potentially use this as a starting point and go from there. It does seem like it would be a cost savings if there was a grab-and-go object for this that wasn't too hard to use.

jonathanolson commented 6 years ago

@samreid is there something I should do here? It's the last blocking common non-a11y issue for area-model.

samreid commented 6 years ago

At some point we should move code like this and supporting logic into vegas. That will help us develop new games, factor out code from other games and will simplify PhET-iO development and API. It's really up to Ariel when we do that.

ariel-phet commented 6 years ago

@samreid I agree we should do this at some point. @jbphet next sim after EFAC is quite likely to be a new math sim....and math sims often seem to have games. I am going to mark this as deferred and perhaps we can consider this improvement with our next sim with a game.

ariel-phet commented 6 years ago

Marked as deferred but assigned to @jbphet to be revisited when we look at next math sim.