code refactor part 1: advance_tic is now two parts

seems to work so far
This commit is contained in:
Timothy Stiles 2020-10-14 21:24:46 +11:00
parent c8d80dfc63
commit 2e1a87199a
2 changed files with 112 additions and 86 deletions

View File

@ -187,7 +187,6 @@ export class Level {
this.time_remaining = this.stored_level.time_limit * 20;
}
this.timer_paused = false
this.waiting_for_input = false
// Note that this clock counts *up*, even on untimed levels, and is unaffected by CC2's
// clock alteration shenanigans
this.tic_counter = 0;
@ -331,15 +330,32 @@ export class Level {
}
}
player_awaiting_input() {
//todo: the tic_counter part feels kludgey. maybe there's some other way a wait/wall nudge can wait a certain amount of time per tap?
return this.player.movement_cooldown === 0 && this.player.slide_mode === null && this.tic_counter % 2 == 0
}
// Move the game state forwards by one tic
advance_tic(p1_primary_direction, p1_secondary_direction, force_wait) {
// split into two parts for turn-based mode: first part is the consequences of the previous tick, second part depends on the player's input
advance_tic(p1_primary_direction, p1_secondary_direction, pass) {
if (this.state !== 'playing') {
console.warn(`Level.advance_tic() called when state is ${this.state}`);
return;
}
try {
this._advance_tic(p1_primary_direction, p1_secondary_direction, force_wait);
if (pass == 1)
{
this._advance_tic_part1(p1_primary_direction, p1_secondary_direction);
}
else if (pass == 2)
{
this._advance_tic_part2(p1_primary_direction, p1_secondary_direction);
}
else
{
console.warn(`What pass is this?`);
}
}
catch (e) {
if (e instanceof GameEnded) {
@ -350,26 +366,13 @@ export class Level {
}
}
// Commit the undo state at the end of each tic
if (!this.waiting_for_input) {
// Commit the undo state at the end of each tic (pass 2)
if (pass == 2) {
this.commit();
}
}
_advance_tic(p1_primary_direction, p1_secondary_direction, force_wait) {
var skip_to_third_pass = false;
//if we're waiting for input, then we want to skip straight to phase 3 with a player decision filled out when they have one ready
if (this.waiting_for_input) {
this.actor_decision(this.player, p1_primary_direction);
if (this.player.decision != null || force_wait) {
skip_to_third_pass = true;
}
else {
return;
}
}
_advance_tic_part1(p1_primary_direction, p1_secondary_direction) {
// Player's secondary direction is set immediately; it applies on arrival to cells even if
// it wasn't held the last time the player started moving
this._set_prop(this.player, 'secondary_direction', p1_secondary_direction);
@ -387,7 +390,6 @@ export class Level {
// arrival as its own mini pass, for one reason: if the player dies (which will end the game
// immediately), we still want every time's animation to finish, or it'll look like some
// objects move backwards when the death screen appears!
if (!skip_to_third_pass) {
let cell_steppers = [];
for (let actor of this.actors) {
// Actors with no cell were destroyed
@ -441,16 +443,10 @@ export class Level {
}
}
//in Turn-Based mode, wait for input if the player can voluntarily move on tic_counter % 4 == 0 and isn't
if (this.turn_based && this.player.movement_cooldown == 0 && this.player.decision == null && (this.tic_counter % 4 == 0) && !force_wait)
{
this.waiting_for_input = true;
return;
}
else
{
this.waiting_for_input = false;
}
_advance_tic_part2(p1_primary_direction, p1_secondary_direction) {
//player now makes a decision based on input
this.actor_decision(this.player, p1_primary_direction);
// Third pass: everyone actually moves
for (let actor of this.actors) {
@ -981,8 +977,6 @@ export class Level {
}
undo() {
//ok, actually we're not doing an in-progress move after all.
this.waiting_for_input = false;
this.pending_undo = [];
this.aid = Math.max(1, this.aid);

View File

@ -703,6 +703,8 @@ class Player extends PrimaryView {
}
}
waiting_for_input = false;
advance_by(tics) {
for (let i = 0; i < tics; i++) {
let input = this.get_input();
@ -765,11 +767,41 @@ class Player extends PrimaryView {
this.previous_input = input;
this.sfx_player.advance_tic();
var primary_dir = this.primary_action ? ACTION_DIRECTIONS[this.primary_action] : null;
var secondary_dir = this.secondary_action ? ACTION_DIRECTIONS[this.secondary_action] : null;
//turn based logic
//first, handle a part 2 we just got input for
if (this.waiting_for_input)
{
if (!this.turn_based || primary_dir != null || input.has('wait'))
{
this.level.advance_tic(
this.primary_action ? ACTION_DIRECTIONS[this.primary_action] : null,
this.secondary_action ? ACTION_DIRECTIONS[this.secondary_action] : null,
input.has('wait')
);
primary_dir,
secondary_dir,
2);
}
}
else //TODO: or `if (!this.waiting_for_input)` to be snappier
{
this.level.advance_tic(
primary_dir,
secondary_dir,
1);
//then if we should wait for input, the player needs input and we don't have input, we set waiting_for_input, else we run part 2
if (this.turn_based && this.level.player_awaiting_input() && !(primary_dir != null || input.has('wait')))
{
this.waiting_for_input = true;
}
else
{
this.level.advance_tic(
primary_dir,
secondary_dir,
2);
}
}
if (this.level.state !== 'playing') {
// We either won or lost!