1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
Player
======
The Player object is used to control a specific player/character. Player objects are tagged *player*. That might be useful when dealing with collisions.
**Note:** in Open Surge, companion objects can be used to give new abilities to players. Companion objects must be indicated in the character definition files (.chr) and are implemented as regular scripts. The engine will automatically spawn the user-defined companion objects as children of the correct Player objects.
*Example*
```
using SurgeEngine.Audio.Sound;
//
// This is a dash move that should be configured as a
// companion object in a character definition file (.chr)
//
// When you are stopped, hold up and press jump to charge.
// Release up after 0.5 second and you'll gain a nice boost!
//
object "My Peel Out" is "companion"
{
charge = Sound("samples/charge.wav");
release = Sound("samples/release.wav");
player = parent; // since this object is configured as a
// companion, parent is the reference
// to the correct Player object
speed = 720; // dash speed, in pixels/second
// capture the event
state "main"
{
if(player.lookingUp) {
if(player.input.buttonPressed("fire1")) {
charge.play();
state = "charging";
}
}
}
// charging the dash
state "charging"
{
player.anim = 2; // running animation
player.animation.speedFactor = 1.85;
player.frozen = true; // disable physics (temporarily)
// ready to go?
if(player.input.buttonReleased("up")) {
if(timeout(0.5)) {
player.gsp = speed * player.direction; // dash!!!
release.play();
}
player.frozen = false; // enable physics
state = "main";
}
else if(player.input.buttonPressed("fire1"))
charge.play();
}
}
```
Factory
-------
#### Player
`Player(playerName | playerId)`
Gets the Player object associated with a certain character.
*Arguments*
* `playerName`: string. The name of the character (defined in the *characters/* folder).
* `playerId`: number. The ID of the character (as defined in the *.lev* file), an integer between `0` and `Player.count - 1`, inclusive.
*Returns*
A Player object.
*Example*
```
using SurgeEngine.Player;
// Gives Surge 1 collectible each second
// Just place it in your level
object "Collectible Giver" is "entity", "awake"
{
player = Player("Surge");
state "main"
{
if(timeout(1))
state = "give collectible";
}
state "give collectible"
{
player.collectibles = player.collectibles + 1;
state = "main";
}
}
```
#### Player.active
`Player.active`
The active player, i.e., the one currently in focus.
*Returns*
A Player object.
*Example*
```
using SurgeEngine.Player;
// Tells the name of the active player
// Just place it in the level
object "Who am I" is "entity", "awake"
{
state "main"
{
Console.print("I am " + Player.active.name);
destroy();
}
}
```
#### Player.count
`Player.count`
Player count.
*Returns*
The number of players in the level.
#### Player.initialLives
`Player.initialLives`
The initial number of lives set by the engine.
*Returns*
The initial number of lives.
Properties
----------
#### name
`name`: string, read-only.
The name of the character.
#### input
`input`: [Input](/engine/input) object, read-only.
Reference to the Input object of the Player.
*Example*
```
using SurgeEngine.Player;
object "Application"
{
state "main"
{
if(Player.active.input.buttonDown("right"))
Console.print("Player is holding right");
}
}
```
#### anim
`anim`: number.
A shortcut to `animation.id`: an integer corresponding to the animation number.
#### animation
`animation`: [Animation](/engine/animation) object, read-only.
Reference to the Animation object of the Player.
#### attacking
`attacking`: boolean, read-only.
Is the player attacking? (jumping, rolling, and so on)
#### midair
`midair`: boolean, read-only.
Is the player midair?
#### stopped
`stopped`: boolean, read-only.
Is the player stopped?
#### walking
`walking`: boolean, read-only.
Is the player walking?
#### running
`running`: boolean, read-only.
Is the player running?
#### jumping
`jumping`: boolean, read-only.
Is the player jumping?
#### springing
`springing`: boolean, read-only.
Is the player playing the "springing" animation, displayed just after hitting a spring?
#### braking
`braking`: boolean, read-only.
Is the player braking?
#### balancing
`balancing`: boolean, read-only.
Is the player balancing on a ledge?
#### waiting
`waiting`: boolean, read-only.
Is the player waiting? (happens after remaining stopped for a few seconds)
#### rolling
`rolling`: boolean, read-only.
Is the player rolling?
#### charging
`charging`: boolean, read-only.
Is the player charging a rolling movement?
#### pushing
`pushing`: boolean, read-only.
Is the player pushing a wall?
#### hit
`hit`: boolean, read-only.
Is the player getting hit (i.e., losing collectibles or a shield)?
#### blinking
`blinking`: boolean, read-only.
Is the player blinking? (happens just after getting hit)
#### dying
`dying`: boolean, read-only.
Is the player dying or drowning?
#### drowning
`drowning`: boolean, read-only.
Is the player drowning (i.e., dying underwater)?
#### breathing
`breathing`: boolean, read-only.
Is the player breathing an air bubble underwater?
#### crouchingDown
`crouchingDown`: boolean, read-only.
Is the player crouching down?
#### lookingUp
`lookingUp`: boolean, read-only.
Is the player looking up?
#### winning
`winning`: boolean, read-only.
Is the player displaying the "winning" animation (after clearing a level)?
#### secondsToDrown
`secondsToDrown`: number, read-only.
The number of seconds to drown, if underwater.
#### topspeed
`topspeed`: number, read-only.
The maximum speed the player can reach without items (i.e., running naturally), in pixels per second.
#### direction
`direction`: number, read-only.
Direction will be +1 if the player is facing right, -1 if facing left.
#### transform
`transform`: [Transform](/engine/transform) object, read-only.
The transform of the Player.
#### collider
`collider`: [Collider](/engine/collider) object, read-only.
A collider associated with this Player.
#### shield
`shield`: string | `null`.
One of the following: *"shield"*, *"fire"*, *"thunder"*, *"water"*, *"acid"*, *"wind"* or `null` (if there is no shield).
#### invincible
`invincible`: boolean.
Used to make the player invincible. Defaults to `false`.
#### turbo
`turbo`: boolean.
Turbo mode (increases player speed). Defaults to `false`.
#### frozen
`frozen`: boolean.
Disable/enable player movement. Defaults to `false`.
#### underwater
`underwater`: boolean.
Get underwater / out of water. Defaults to `false`.
#### aggressive
`aggressive`: boolean.
If set to `true`, the [attacking](#attacking) flag will be `true` and the player will be able to hit the baddies just by touching them, regardless if he/she is jumping or not. Defaults to `false`.
*Available since:* Open Surge 0.5.1
#### breathTime
`breathTime`: number.
The maximum number of seconds the player can remain underwater without breathing.
#### layer
`layer`: string.
The current layer of the player. One of the following: *"green"*, *"yellow"*, *"default"*. This property tells you which bricks will be sensed, depending on their layer.
#### visible
`visible`: boolean.
Should the player sprite be rendered? Defaults to `true`.
#### collectibles
`collectibles`: number.
The number of collectibles, an integer shared between all player objects.
#### lives
`lives`: number.
The number of lives, an integer shared between all player objects.
#### score
`score`: number.
The score of the player, an integer value shared between all player objects.
#### speed
`speed`: number.
The speed of the player, in pixels per second. This will be mapped automatically to [gsp](#gsp) or [xsp](#xsp), depending on whether the player is touching the ground or not.
#### gsp
`gsp`: number.
Ground speed, in pixels per second. Prefer using [speed](#speed) instead, since `gsp` is only meaningful if the player is touching the ground.
#### xsp
`xsp`: number.
Horizontal speed, in pixels per second (useful while midair). Prefer using [speed](#speed) instead.
#### ysp
`ysp`: number.
Vertical speed, in pixels per second.
#### angle
`angle`: number.
The angle of the player, in degrees. The same as `transform.angle`.
#### width
`width`: number, read-only.
The width of the player sprite, in pixels.
#### height
`height`: number, read-only.
The height of the player sprite, in pixels.
Functions
---------
#### bounce
`bounce(hazard | null)`
Makes the player bounce after smashing a hazard. The trajectory of the movement will be computed according to the position of the hazard. If no hazard is present, you may pass `null` as the argument.
*Arguments*
* `hazard`: [Actor](/engine/actor) object. The hazard.
*Example*
```
using SurgeEngine.Player;
using SurgeEngine.Actor;
using SurgeEngine.Collisions.CollisionBox;
object "Shield Box" is "entity"
{
actor = Actor("Shield Box");
shieldCollider = CollisionBox(32,32);
state "main"
{
player = Player.active;
if(player.attacking) {
if(player.collider.collidesWith(shieldCollider)) {
if(player.midair)
player.bounce(actor);
player.shield = "fire";
destroy();
}
}
}
}
```
#### bounceBack
`bounceBack(hazard)`
Makes the player bounce after smashing a hazard. If the player comes at the hazard from below, the player is thrown downwards. If not, the player is thrown upwards. The `hazard` parameter is mandatory.
*Arguments*
* `hazard`: [Actor](/engine/actor) object. The hazard.
#### getHit
`getHit(hazard | null)`
Makes the player get hit. Call it whenever the player gets hit by a hazard. The hit movement will be calculated according to the position of the hazard. If no hazard is present, you may pass `null` as the argument.
*Arguments*
* `hazard`: [Actor](/engine/actor) object. The hazard.
#### kill
`kill()`
Kills the player.
#### breathe
`breathe()`
Makes the player breathe (underwater only).
#### springify
`springify()`
Activates the "springing" activity.
#### roll
`roll()`
Makes the player roll.
#### hlock
`hlock(seconds)`
Locks the horizontal controls of the player for a few `seconds` (left and right input will be ignored during that time).
*Arguments*
* `seconds`: number. Specify how long the horizontal controls of the player will stay locked.
#### focus
`focus()`
Focuses on the player. The focused player is controlled by the user. Only one player can have focus at any given time.
#### hasFocus
`hasFocus()`
Does the player have focus?
*Returns*
Returns `true` if the player has focus, `false` otherwise.
|