Skip to content

Commit

Permalink
✨ ct.camera, UI and game coordinates, and nested rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoMyzrailGorynych committed Feb 16, 2020
1 parent 31c1a3d commit 902c92d
Show file tree
Hide file tree
Showing 43 changed files with 1,404 additions and 498 deletions.
5 changes: 5 additions & 0 deletions app/data/ct.libs/fittoscreen/CHANGELOGmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v3.0.0

- Support for the new `ct.camera`.
- Removed `ct.fittoscreen.manageViewport`.
- Removed "Expand, manage viewport" mode, as "Expand" replaces it.
4 changes: 0 additions & 4 deletions app/data/ct.libs/fittoscreen/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

Resizes the canvas immediately.

## `ct.fittoscreen.manageViewport();`

Shifts the viewport so the previous central point stays in the same place. You usually don't need to call it manually. Works only if "Manage the view" option is enabled.

## `ct.fittoscreen.toggleFullscreen();`

Tries to toggle the fullscreen mode. Errors, if any, will be logged to console. Also, this won't work in the internal ct.js debugger. Instead, test it in your browser.
Expand Down
4 changes: 2 additions & 2 deletions app/data/ct.libs/fittoscreen/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This module allows you to automagically fit your game to screen, either by resizing the whole drawing canvas or by simple scaling.
This module allows you to automagically fit your game to screen, either by resizing the whole drawing canvas or by simple scaling. See the settings for this module for different scaling methods.

It also gives you functions to enter a real fullscreen mode programmatically.
It also gives you functions to enter a real fullscreen mode programmatically (see the "Reference" tab).
43 changes: 6 additions & 37 deletions app/data/ct.libs/fittoscreen/index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,32 @@
(function (ct) {
var width,
height;
var oldWidth, oldHeight;
var canv = ct.pixiApp.view;
var manageViewport = function (room) {
room = room || ct.room;
room.x -= (width - oldWidth) / 2;
room.y -= (height - oldHeight) / 2;
};
var resize = function() {
const {mode} = ct.fittoscreen;
width = window.innerWidth;
height = window.innerHeight;
var kw = width / ct.roomWidth,
kh = height / ct.roomHeight,
minorWidth = kw > kh;
kh = height / ct.roomHeight;
var k = Math.min(kw, kh);
if (mode === 'fastScale') {
canv.style.transform = 'scale(' + k + ')';
canv.style.position = 'absolute';
canv.style.left = (width - ct.width) / 2 + 'px';
canv.style.top = (height - ct.height) / 2 + 'px';
} else {
var {room} = ct;
if (!room) {
return;
}
oldWidth = ct.width;
oldHeight = ct.height;
if (mode === 'expandViewport' || mode === 'expand') {
for (const bg of ct.types.list.BACKGROUND) {
bg.width = width;
bg.height = height;
}
ct.viewWidth = width;
ct.viewHeight = height;
ct.camera.width = width;
ct.camera.height = height;
}
if (mode !== 'scaleFit') {
ct.pixiApp.renderer.resize(width, height);
if (mode === 'scaleFill') {
if (minorWidth) {
ct.viewWidth = Math.ceil(width / k);
} else {
ct.viewHeight = Math.ceil(height / k);
}
for (const bg of ct.types.list.BACKGROUND) {
bg.width = ct.viewWidth;
bg.height = ct.viewHeight;
}
ct.camera.width = Math.ceil(width / k);
ct.camera.height = Math.ceil(height / k);
}
} else {
ct.pixiApp.renderer.resize(Math.floor(ct.viewWidth * k), Math.floor(ct.viewHeight * k));
ct.pixiApp.renderer.resize(Math.floor(ct.camera.width * k), Math.floor(ct.camera.height * k));
canv.style.position = 'absolute';
canv.style.left = (width - ct.width) / 2 + 'px';
canv.style.top = (height - ct.height) / 2 + 'px';
Expand All @@ -59,9 +35,6 @@
ct.pixiApp.stage.scale.x = k;
ct.pixiApp.stage.scale.y = k;
}
if (mode === 'expandViewport') {
manageViewport(room);
}
}
};
var toggleFullscreen = function () {
Expand Down Expand Up @@ -96,7 +69,6 @@
height = window.innerHeight;
window.addEventListener('resize', resize);
ct.fittoscreen = resize;
ct.fittoscreen.manageViewport = manageViewport;
ct.fittoscreen.toggleFullscreen = queueFullscreen;
var $mode = '/*%mode%*/';
Object.defineProperty(ct.fittoscreen, 'mode', {
Expand All @@ -105,11 +77,8 @@
set(value) {
if ($mode === 'fastScale' && value !== 'fastScale') {
canv.style.transform = '';
} else if (value === 'fastScale' || value === 'expand' || value === 'expandViewport') {
ct.pixiApp.stage.scale.x = ct.pixiApp.stage.scale.y = 1;
}
$mode = value;
ct.fittoscreen();
},
get() {
return $mode;
Expand Down
1 change: 0 additions & 1 deletion app/data/ct.libs/fittoscreen/injects/start.js

This file was deleted.

6 changes: 1 addition & 5 deletions app/data/ct.libs/fittoscreen/module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"main": {
"name": "Fit to Screen",
"version": "2.0.0",
"version": "3.0.0",
"authors": [{
"name": "Cosmo Myzrail Gorynych",
"mail": "admin@nersta.ru"
Expand All @@ -17,10 +17,6 @@
"value": "expand",
"name": "Expand",
"help": "Expands the viewport, remaining the scale of the copies untouched"
}, {
"value": "expandViewport",
"name": "Expand, manage viewport",
"help": "Additionally moves the viewport so that the central point always stays in the center of the drawing canvas."
}, {
"value": "scaleFit",
"name": "Scaling with letterboxing",
Expand Down
7 changes: 7 additions & 0 deletions app/data/ct.libs/mouse/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v3.0.0

* Support the new ct.camera object
* Introduce `ct.mouse.xui`, `ct.mouse.yui`, as well as `ct.mouse.xuiprev` and `ct.mouse.yuiprev`
* Remove `ct.mouse.rx` and `ct.mouse.ry
* Fix broken `Wheel` input method

v2.0.0

* Support the new Actions system
Expand Down
14 changes: 8 additions & 6 deletions app/data/ct.libs/mouse/DOCS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## `ct.mouse.x`, `ct.mouse.y`

Current cursor position at horisontal and vertical axes, in world coordinates.
Current cursor position at horisontal and vertical axes, in game coordinates.

**Example: make a copy follow the cursor**

Expand All @@ -9,11 +9,9 @@ this.x = ct.mouse.x;
this.y = ct.mouse.y;
```

# `ct.mouse.rx`, `ct.mouse.ry`
# `ct.mouse.xui`, `ct.mouse.yui`

A cursor position relative to the current view (or camera), but not relative to the room.

`ct.mouse.rx` is the same as `ct.mouse.x - ct.room.x`.
A cursor position relative to the current view (UI coordinates), but not relative to the room.

## `ct.mouse.pressed`

Expand Down Expand Up @@ -43,7 +41,11 @@ Can be either `true` or `false`. Determines whether there is a cursor inside the

## `ct.mouse.hovers(copy)`

Returns `true` if the mouse hovers over a given `copy`. This does **not** take scaling and rotation into account, as well as polygonal shapes (as they are hollow).
Returns `true` if the mouse hovers over a given `copy` in game coordinates. This does **not** take scaling and rotation into account, as well as polygonal shapes (as they are hollow).

## `ct.mouse.hoversUi(copy)`

Returns `true` if the mouse hovers over a given `copy` in UI coordinates. This does **not** take scaling and rotation into account, as well as polygonal shapes (as they are hollow).

## `ct.mouse.hide()`, `ct.mouse.show()`
Change the visibility of the mouse cursor.
Expand Down
49 changes: 30 additions & 19 deletions app/data/ct.libs/mouse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
};

ct.mouse = {
rx: 0,
ry: 0,
xprev: 0,
yprev: 0,
xuiprev: 0,
yuiprev: 0,
inside: false,
pressed: false,
down: false,
Expand All @@ -41,11 +41,20 @@
}
return false;
},
get x() {
return ct.mouse.rx + ct.rooms.current.x;
},
get y() {
return ct.mouse.ry + ct.rooms.current.y;
hoversUi(copy) {
if (!copy.shape) {
return false;
}
if (copy.shape.type === 'rect') {
return ct.u.prect(ct.mouse.xui, ct.mouse.yui, copy);
}
if (copy.shape.type === 'circle') {
return ct.u.pcircle(ct.mouse.xui, ct.mouse.yui, copy);
}
if (copy.shape.type === 'point') {
return ct.mouse.xui === copy.x && ct.mouse.yui === copy.y;
}
return false;
},
hide() {
ct.pixiApp.renderer.view.style.cursor = 'none';
Expand All @@ -57,11 +66,10 @@

ct.mouse.listenerMove = function(e) {
var rect = ct.pixiApp.view.getBoundingClientRect();
ct.mouse.rx = (e.clientX - rect.left) * ct.viewWidth / rect.width;
ct.mouse.ry = (e.clientY - rect.top) * ct.viewHeight / rect.height;
ct.mouse.x = ct.mouse.rx + ct.rooms.current.x;
ct.mouse.y = ct.mouse.ry + ct.rooms.current.y;
if (ct.mouse.rx > 0 && ct.mouse.ry > 0 && ct.mouse.ry < ct.viewHeight && ct.mouse.rx < ct.viewWidth) {
ct.mouse.xui = (e.clientX - rect.left) * ct.camera.width / rect.width;
ct.mouse.yui = (e.clientY - rect.top) * ct.camera.height / rect.height;
[ct.mouse.x, ct.mouse.y] = ct.u.uiToGameCoord(ct.mouse.xui, ct.mouse.yui);
if (ct.mouse.xui > 0 && ct.mouse.yui > 0 && ct.mouse.yui < ct.camera.height && ct.mouse.xui < ct.camera.width) {
ct.mouse.inside = true;
} else {
ct.mouse.inside = false;
Expand All @@ -88,25 +96,28 @@
e.preventDefault();
};
ct.mouse.listenerWheel = function (e) {
ct.mouse.wheel = e.wheelDelta || -e.detail < 0? -1 : 1;
setKey('wheel', ct.mouse.wheel);
e.preventDefault();
setKey('Wheel', e.wheelDelta || -e.detail < 0? -1 : 1);
//e.preventDefault();
};

ct.mouse.setupListeners = function () {
if (document.addEventListener) {
document.addEventListener('mousemove', ct.mouse.listenerMove, false);
document.addEventListener('mouseup', ct.mouse.listenerUp, false);
document.addEventListener('mousedown', ct.mouse.listenerDown, false);
document.addEventListener('wheel', ct.mouse.listenerWheel, false);
document.addEventListener('wheel', ct.mouse.listenerWheel, false, {
passive: false
});
document.addEventListener('contextmenu', ct.mouse.listenerContextMenu, false);
document.addEventListener('DOMMouseScroll', ct.mouse.listenerWheel, false);
document.addEventListener('DOMMouseScroll', ct.mouse.listenerWheel, {
passive: false
});
} else { // IE?
document.attachEvent('onmousemove', ct.mouse.listenerMove);
document.attachEvent('onmouseup', ct.mouse.listenerUp);
document.attachEvent('onmousedown', ct.mouse.listenerDown);
document.attachEvent('oncontextmenu', ct.mouse.listenerWheel);
document.attachEvent('onmousewheel', ct.mouse.listenerContextMenu);
document.attachEvent('onmousewheel', ct.mouse.listenerWheel);
document.attachEvent('oncontextmenu', ct.mouse.listenerContextMenu);
}
};
})();
3 changes: 3 additions & 0 deletions app/data/ct.libs/mouse/injects/afterroomdraw.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ct.mouse.xprev = ct.mouse.x;
ct.mouse.yprev = ct.mouse.y;
ct.mouse.xuiprev = ct.mouse.xui;
ct.mouse.yuiprev = ct.mouse.yui;
ct.mouse.pressed = ct.mouse.released = false;
ct.inputs.registry['mouse.Wheel'] = 0;
2 changes: 1 addition & 1 deletion app/data/ct.libs/mouse/module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"main": {
"name": "Mouse Input",
"version": "2.1.0",
"version": "3.0.0",
"authors": [{
"name": "Cosmo Myzrail Gorynych",
"mail": "admin@nersta.ru"
Expand Down
14 changes: 8 additions & 6 deletions app/data/ct.libs/mouse/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
declare namespace ct {
/** A module that tells mouse coordinates and integrates with ct.js' Actions system */
namespace mouse {
/** Current cursor position at horisontal and vertical axes, in world coordinates */
/** Current cursor position at horisontal and vertical axes, in game coordinates */
var x: number;
/** Current cursor position at horisontal and vertical axes, in world coordinates */
/** Current cursor position at horisontal and vertical axes, in game coordinates */
var y: number;
/** A cursor position relative to the current view (or camera), but not relative to the room. */
var rx: number;
/** A cursor position relative to the current view (or camera), but not relative to the room. */
var ry: number;
/** A cursor position in UI coordinates. */
var xui: number;
/** A cursor position in UI coordinates. */
var yui: number;
/** Can be either `true` or `false`. Determines whether a mouse button was pressed. */
var pressed: boolean;
/** Can be either `true` or `false`. Determines whether a mouse button is held down. */
Expand All @@ -19,5 +19,7 @@ declare namespace ct {
var inside: boolean;
/** Returns `true` if the mouse hovers over a given `copy`. This does **not** take scaling and rotation into account, as well as polygonal shapes (as they are hollow). */
function hovers(copy: Copy): boolean;
/** Returns `true` if the mouse hovers over a given `copy`. This does **not** take scaling and rotation into account, as well as polygonal shapes (as they are hollow). */
function hoversUi(copy: Copy): boolean;
}
}
10 changes: 9 additions & 1 deletion app/data/ct.libs/place/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
Checks collisions between copies.
Checks collisions between copies. See the "Reference" tab for the methods.


# Collisions in sense of UI and game coordinates

`ct.place` calculates collisions relative to a copy's parent. As UI layers and game layers live in different coordinates, you cannot reliably check collisions between copies of different coordinate spaces. Due to that, use different collision groups for UI elements and gameplay copies.


# Additions to copies

You can call `this.moveContinuous('CollisionGroup');` at any copy to perform precise movement with collision checks. It takes gravity into account, too, and uses the `ct.place.moveAlong` method.

Additional field in the type editor "Collision group" defines the `ctype` property used in most ct.place methods.


# Preparing types

> This is useful only when you are going to create copies of different shapes dynamically. In other cases, refer to the Docs tab, as ct.IDE defines these shapes itself.
Expand All @@ -23,6 +30,7 @@ It is important to create a new `shape` object and not to change existing one, b

Besides the `shape` parameter, each Copy can have a `ctype` parameter. It is used for grouping Copies into different collision groups, like 'Enemies', 'HeroBullets', 'Obstacles' etc.


# Collision shapes

* `'point'` — does not require any additional parameters;
Expand Down
3 changes: 0 additions & 3 deletions app/data/ct.libs/place/injects/ondestroy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
if (this.ctype) {
ct.place.ctypeCollections[this.$ctype].splice(ct.place.ctypeCollections[this.$ctype].indexOf(this), 1);
}
if (this.$chashes) {
for (const hash of this.$chashes) {
ct.place.grid[hash].splice(ct.place.grid[hash].indexOf(this), 1);
Expand Down
3 changes: 1 addition & 2 deletions app/data/ct.libs/place/injects/roomonleave.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
ct.place.grid = {};
ct.place.ctypeCollections = {};
ct.place.grid = {};
10 changes: 0 additions & 10 deletions app/data/ct.libs/place/injects/start.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
ct.place.ctypeCollections = {};
Object.defineProperty(ct.types.Copy.prototype, 'ctype', {
set: function(value) {
if (this.ctype) {
ct.place.ctypeCollections[this.ctype].splice(ct.place.ctypeCollections[this.ctype].indexOf(this), 1);
}
if (value) {
if (!(value in ct.place.ctypeCollections)) {
ct.place.ctypeCollections[value] = [];
}
ct.place.ctypeCollections[value].push(this);
}
this.$ctype = value;
},
get: function() {
Expand Down

0 comments on commit 902c92d

Please sign in to comment.