Compare commits
No commits in common. "main" and "0.20.0" have entirely different histories.
|
@ -7,13 +7,13 @@
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: test
|
- name: test
|
||||||
image: mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim
|
image: mcr.microsoft.com/dotnet/core/sdk:3.1
|
||||||
commands:
|
commands:
|
||||||
- dotnet build -c Release
|
- dotnet build -c Release
|
||||||
- dotnet test -c Release
|
- dotnet test -c Release
|
||||||
|
|
||||||
- name: deploy
|
- name: deploy
|
||||||
image: mcr.microsoft.com/dotnet/sdk:5.0.102-ca-patch-buster-slim
|
image: mcr.microsoft.com/dotnet/core/sdk:3.1
|
||||||
environment:
|
environment:
|
||||||
API_KEY:
|
API_KEY:
|
||||||
from_secret: API_KEY
|
from_secret: API_KEY
|
||||||
|
|
424
LICENSE
424
LICENSE
|
@ -1,19 +1,415 @@
|
||||||
encompass-cs
|
encompass-cs
|
||||||
|
Copyright Evan Hemsley 2019
|
||||||
|
|
||||||
Copyright (c) 2020 Evan Hemsley <evan@moonside.games>
|
COOPERATIVE SOFTWARE LICENSE
|
||||||
|
|
||||||
This software is provided 'as-is', without any express or implied
|
THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS
|
||||||
warranty. In no event will the authors be held liable for any damages
|
COPYFARLEFT PUBLIC LICENSE ("LICENSE"). THE WORK IS PROTECTED BY
|
||||||
arising from the use of this software.
|
COPYRIGHT AND ALL OTHER APPLICABLE LAWS. ANY USE OF THE WORK OTHER THAN
|
||||||
|
AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED. BY
|
||||||
|
EXERCISING ANY RIGHTS TO THE WORK PROVIDED IN THIS LICENSE, YOU AGREE
|
||||||
|
TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE
|
||||||
|
MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS
|
||||||
|
CONTAINED HERE IN AS CONSIDERATION FOR ACCEPTING THE TERMS AND
|
||||||
|
CONDITIONS OF THIS LICENSE AND FOR AGREEING TO BE BOUND BY THE TERMS
|
||||||
|
AND CONDITIONS OF THIS LICENSE.
|
||||||
|
|
||||||
Permission is granted to anyone to use this software for any purpose,
|
1. DEFINITIONS
|
||||||
including commercial applications, and to alter it and redistribute it
|
|
||||||
freely, subject to the following restrictions:
|
a. "Adaptation" means a work based upon the Work, or upon the
|
||||||
|
Work and other pre-existing works, such as a translation,
|
||||||
|
adaptation, derivative work, arrangement of music or other
|
||||||
|
alterations of a literary or artistic work, or phonogram or
|
||||||
|
performance and includes cinematographic adaptations or any
|
||||||
|
other form in which the Work may be recast, transformed, or
|
||||||
|
adapted including in any form recognizably derived from the
|
||||||
|
original, except that a work that constitutes a Collection will
|
||||||
|
not be considered an Adaptation for the purpose of this License.
|
||||||
|
For the avoidance of doubt, where the Work is a musical work,
|
||||||
|
performance or phonogram, the synchronization of the Work in
|
||||||
|
timed-relation with a moving image ("synching") will be
|
||||||
|
considered an Adaptation for the purpose of this License.
|
||||||
|
|
||||||
|
b. "Collection" means a collection of literary or artistic
|
||||||
|
works, such as encyclopedias and anthologies, or performances,
|
||||||
|
phonograms or broadcasts, or other works or subject matter other
|
||||||
|
than works listed in Section 1(f) below, which, by reason of the
|
||||||
|
selection and arrangement of their contents, constitute
|
||||||
|
intellectual creations, in which the Work is included in its
|
||||||
|
entirety in unmodified form along with one or more other
|
||||||
|
contributions, each constituting separate and independent works
|
||||||
|
in themselves, which together are assembled into a collective
|
||||||
|
whole. A work that constitutes a Collection will not be
|
||||||
|
considered an Adaptation (as defined above) for the purposes of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
c. "Distribute" means to make available to the public the
|
||||||
|
original and copies of the Work or Adaptation, as appropriate,
|
||||||
|
through sale, gift or any other transfer of possession or
|
||||||
|
ownership.
|
||||||
|
|
||||||
|
d. "Licensor" means the individual, individuals, entity or
|
||||||
|
entities that offer(s) the Work under the terms of this License.
|
||||||
|
|
||||||
|
e. "Original Author" means, in the case of a literary or
|
||||||
|
artistic work, the individual, individuals, entity or entities
|
||||||
|
who created the Work or if no individual or entity can be
|
||||||
|
identified, the publisher; and in addition (i) in the case of a
|
||||||
|
performance the actors, singers, musicians, dancers, and other
|
||||||
|
persons who act, sing, deliver, declaim, play in, interpret or
|
||||||
|
otherwise perform literary or artistic works or expressions of
|
||||||
|
folklore; (ii) in the case of a phonogram the producer being the
|
||||||
|
person or legal entity who first fixes the sounds of a
|
||||||
|
performance or other sounds; and, (iii) in the case of
|
||||||
|
|
||||||
|
f. "Work" means the literary and/or artistic work offered under
|
||||||
|
the terms of this License including without limitation any
|
||||||
|
production in the literary, scientific and artistic domain,
|
||||||
|
whatever may be the mode or form of its expression including
|
||||||
|
digital form, such as a book, pamphlet and other writing; a
|
||||||
|
lecture, address, sermon or other work of the same nature; a
|
||||||
|
dramatic or dramatico-musical work; a choreographic work or
|
||||||
|
entertainment in dumb show; a musical composition with or
|
||||||
|
without words; a cinematographic work to which are assimilated
|
||||||
|
works expressed by a process analogous to cinematography; a work
|
||||||
|
of drawing, painting, architecture, sculpture, engraving or
|
||||||
|
lithography; a photographic work to which are assimilated works
|
||||||
|
expressed by a process analogous to photography; a work of
|
||||||
|
applied art; an illustration, map, plan, sketch or
|
||||||
|
three-dimensional work relative to geography, topography,
|
||||||
|
architecture or science; a performance; a broadcast; a
|
||||||
|
phonogram; a compilation of data to the extent it is protected
|
||||||
|
as a copyrightable work; or a work performed by a variety or
|
||||||
|
circus performer to the extent it is not otherwise considered a
|
||||||
|
literary or artistic work.
|
||||||
|
|
||||||
|
g. "You" means an individual or entity exercising rights under
|
||||||
|
this License who has not previously violated the terms of this
|
||||||
|
License with respect to the Work, or who has received express
|
||||||
|
permission from the Licensor to exercise rights under this
|
||||||
|
License despite a previous violation.
|
||||||
|
|
||||||
|
h. "Publicly Perform" means to perform public recitations of the
|
||||||
|
Work and to communicate to the public those public recitations,
|
||||||
|
by any means or process, including by wire or wireless means or
|
||||||
|
public digital performances; to make available to the public
|
||||||
|
Works in such a way that members of the public may access these
|
||||||
|
Works from a place and at a place individually chosen by them;
|
||||||
|
to perform the Work to the public by any means or process and
|
||||||
|
the communication to the public of the performances of the Work,
|
||||||
|
including by public digital performance; to broadcast and
|
||||||
|
rebroadcast the Work by any means including signs, sounds or
|
||||||
|
images.
|
||||||
|
|
||||||
|
i. "Reproduce" means to make copies of the Work by any means
|
||||||
|
including without limitation by sound or visual recordings and
|
||||||
|
the right of fixation and reproducing fixations of the Work,
|
||||||
|
including storage of a protected performance or phonogram in
|
||||||
|
digital form or other electronic medium.
|
||||||
|
|
||||||
|
j. "Software" means any digital Work which, through use of a
|
||||||
|
third-party piece of Software or through the direct usage of
|
||||||
|
itself on a computer system, the memory of the computer is
|
||||||
|
modified dynamically or semi-dynamically. "Software",
|
||||||
|
secondly, processes or interprets information.
|
||||||
|
|
||||||
|
k. "Source Code" means the human-readable form of Software
|
||||||
|
through which the Original Author and/or Distributor originally
|
||||||
|
created, derived, and/or modified it.
|
||||||
|
|
||||||
|
l. "Web Service" means the use of a piece of Software to
|
||||||
|
interpret or modify information that is subsequently and directly
|
||||||
|
served to users over the Internet.
|
||||||
|
|
||||||
|
2. FAIR DEALING RIGHTS
|
||||||
|
|
||||||
|
Nothing in this License is intended to reduce, limit, or restrict any
|
||||||
|
uses free from copyright or rights arising from limitations or
|
||||||
|
exceptions that are provided for in connection with the copyright
|
||||||
|
protection under copyright law or other applicable laws.
|
||||||
|
|
||||||
|
3. LICENSE GRANT
|
||||||
|
|
||||||
|
Subject to the terms and conditions of this License, Licensor hereby
|
||||||
|
grants You a worldwide, royalty-free, non-exclusive, perpetual (for the
|
||||||
|
duration of the applicable copyright) license to exercise the rights in
|
||||||
|
the Work as stated below:
|
||||||
|
|
||||||
|
a. to Reproduce the Work, to incorporate the Work into one or
|
||||||
|
more Collections, and to Reproduce the Work as incorporated in
|
||||||
|
the Collections;
|
||||||
|
|
||||||
|
b. to create and Reproduce Adaptations provided that any such
|
||||||
|
Adaptation, including any translation in any medium, takes
|
||||||
|
reasonable steps to clearly label, demarcate or otherwise
|
||||||
|
identify that changes were made to the original Work. For
|
||||||
|
example, a translation could be marked "The original work was
|
||||||
|
translated from English to Spanish," or a modification could
|
||||||
|
indicate "The original work has been modified.";
|
||||||
|
|
||||||
|
c. to Distribute and Publicly Perform the Work including as
|
||||||
|
incorporated in Collections; and,
|
||||||
|
|
||||||
|
d. to Distribute and Publicly Perform Adaptations. The above
|
||||||
|
rights may be exercised in all media and formats whether now
|
||||||
|
known or hereafter devised. The above rights include the right
|
||||||
|
to make such modifications as are technically necessary to
|
||||||
|
exercise the rights in other media and formats. Subject to
|
||||||
|
Section 8(g), all rights not expressly granted by Licensor are
|
||||||
|
hereby reserved, including but not limited to the rights set
|
||||||
|
forth in Section 4(h).
|
||||||
|
|
||||||
|
4. RESTRICTIONS
|
||||||
|
|
||||||
|
The license granted in Section 3 above is expressly made subject to and
|
||||||
|
limited by the following restrictions:
|
||||||
|
|
||||||
|
a. You may Distribute or Publicly Perform the Work only under
|
||||||
|
the terms of this License. You must include a copy of, or the
|
||||||
|
Uniform Resource Identifier (URI) for, this License with every
|
||||||
|
copy of the Work You Distribute or Publicly Perform. You may not
|
||||||
|
offer or impose any terms on the Work that restrict the terms of
|
||||||
|
this License or the ability of the recipient of the Work to
|
||||||
|
exercise the rights granted to that recipient under the terms of
|
||||||
|
the License. You may not sublicense the Work. You must keep
|
||||||
|
intact all notices that refer to this License and to the
|
||||||
|
disclaimer of warranties with every copy of the Work You
|
||||||
|
Distribute or Publicly Perform. When You Distribute or Publicly
|
||||||
|
Perform the Work, You may not impose any effective technological
|
||||||
|
measures on the Work that restrict the ability of a recipient of
|
||||||
|
the Work from You to exercise the rights granted to that
|
||||||
|
recipient under the terms of the License. This Section 4(a)
|
||||||
|
applies to the Work as incorporated in a Collection, but this
|
||||||
|
does not require the Collection apart from the Work itself to be
|
||||||
|
made subject to the terms of this License. If You create a
|
||||||
|
Collection, upon notice from any Licensor You must, to the
|
||||||
|
extent practicable, remove from the Collection any credit as
|
||||||
|
required by Section 4(f), as requested. If You create an
|
||||||
|
Adaptation, upon notice from any Licensor You must, to the
|
||||||
|
extent practicable, remove from the Adaptation any credit as
|
||||||
|
required by Section 4(f), as requested.
|
||||||
|
|
||||||
|
b. Subject to the exception in Section 4(e), you may not
|
||||||
|
exercise any of the rights granted to You in Section 3 above in
|
||||||
|
any manner that is primarily intended for or directed toward
|
||||||
|
commercial advantage or private monetary compensation. The
|
||||||
|
exchange of the Work for other copyrighted works by means of
|
||||||
|
digital file-sharing or otherwise shall not be considered to be
|
||||||
|
intended for or directed toward commercial advantage or private
|
||||||
|
monetary compensation, provided there is no payment of any
|
||||||
|
monetary compensation in connection with the exchange of
|
||||||
|
copyrighted works.
|
||||||
|
|
||||||
|
c. If the Work meets the definition of Software, You may exercise
|
||||||
|
the rights granted in Section 3 only if You provide a copy of the
|
||||||
|
corresponding Source Code from which the Work was derived in digital
|
||||||
|
form, or You provide a URI for the corresponding Source Code of
|
||||||
|
the Work, to any recipients upon request.
|
||||||
|
|
||||||
|
d. If the Work is used as or for a Web Service, You may exercise
|
||||||
|
the rights granted in Section 3 only if You provide a copy of the
|
||||||
|
corresponding Source Code from which the Work was derived in digital
|
||||||
|
form, or You provide a URI for the corresponding Source Code to the
|
||||||
|
Work, to any recipients of the data served or modified by the Web
|
||||||
|
Service.
|
||||||
|
|
||||||
|
e. You may exercise the rights granted in Section 3 for
|
||||||
|
commercial purposes only if you statisy any of the following:
|
||||||
|
|
||||||
|
i. You are a worker-owned business or worker-owned
|
||||||
|
collective; and
|
||||||
|
ii. after tax, all financial gain, surplus, profits and
|
||||||
|
benefits produced by the business or collective are
|
||||||
|
distributed among the worker-owners
|
||||||
|
iii. You are not using such rights on behalf of a business
|
||||||
|
other than those specified in 4(e.i) and elaborated upon in
|
||||||
|
4(e.ii), nor are using such rights as a proxy on behalf of a
|
||||||
|
business with the intent to circumvent the aformentioned
|
||||||
|
restrictions on such a business.
|
||||||
|
|
||||||
|
f. Any use by a business that is privately owned and managed,
|
||||||
|
and that seeks to generate profit from the labor of employees
|
||||||
|
paid by salary or other wages, is not permitted under this
|
||||||
|
license.
|
||||||
|
|
||||||
|
g. If You Distribute, or Publicly Perform the Work or any
|
||||||
|
Adaptations or Collections, You must, unless a request has been
|
||||||
|
made pursuant to Section 4(a), keep intact all copyright notices
|
||||||
|
for the Work and provide, reasonable to the medium or means You
|
||||||
|
are utilizing: (i) the name of the Original Author (or
|
||||||
|
pseudonym, if applicable) if supplied, and/or if the Original
|
||||||
|
Author and/or Licensor designate another party or parties (e.g.,
|
||||||
|
a sponsor institute, publishing entity, journal) for attribution
|
||||||
|
("Attribution Parties") in Licensor!s copyright notice, terms of
|
||||||
|
service or by other reasonable means, the name of such party or
|
||||||
|
parties; (ii) the title of the Work if supplied; (iii) to the
|
||||||
|
extent reasonably practicable, the URI, if any, that Licensor
|
||||||
|
specifies to be associated with the Work, unless such URI does
|
||||||
|
not refer to the copyright notice or licensing information for
|
||||||
|
the Work; and, (iv) consistent with Section 3(b), in the case of
|
||||||
|
an Adaptation, a credit identifying the use of the Work in the
|
||||||
|
Adaptation (e.g., "French translation of the Work by Original
|
||||||
|
Author," or "Screenplay based on original Work by Original
|
||||||
|
Author"). The credit required by this Section 4(f) may be
|
||||||
|
implemented in any reasonable manner; provided, however, that in
|
||||||
|
the case of a Adaptation or Collection, at a minimum such credit
|
||||||
|
will appear, if a credit for all contributing authors of the
|
||||||
|
Adaptation or Collection appears, then as part of these credits
|
||||||
|
and in a manner at least as prominent as the credits for the
|
||||||
|
other contributing authors. For the avoidance of doubt, You may
|
||||||
|
only use the credit required by this Section for the purpose of
|
||||||
|
attribution in the manner set out above and, by exercising Your
|
||||||
|
rights under this License, You may not implicitly or explicitly
|
||||||
|
assert or imply any connection with, sponsorship or endorsement
|
||||||
|
by the Original Author, Licensor and/or Attribution Parties, as
|
||||||
|
appropriate, of You or Your use of the Work, without the
|
||||||
|
separate, express prior written permission of the Original
|
||||||
|
Author, Licensor and/or Attribution Parties.
|
||||||
|
|
||||||
|
h. For the avoidance of doubt:
|
||||||
|
|
||||||
|
i. Non-waivable Compulsory License Schemes. In those
|
||||||
|
jurisdictions in which the right to collect royalties
|
||||||
|
through any statutory or compulsory licensing scheme
|
||||||
|
cannot be waived, the Licensor reserves the exclusive
|
||||||
|
right to collect such royalties for any exercise by You of
|
||||||
|
the rights granted under this License;
|
||||||
|
|
||||||
|
ii. Waivable Compulsory License Schemes. In those
|
||||||
|
jurisdictions in which the right to collect royalties
|
||||||
|
through any statutory or compulsory licensing scheme can
|
||||||
|
be waived, the Licensor reserves the exclusive right to
|
||||||
|
collect such royalties for any exercise by You of the
|
||||||
|
rights granted under this License if Your exercise of such
|
||||||
|
rights is for a purpose or use which is otherwise than
|
||||||
|
noncommercial as permitted under Section 4(b) and
|
||||||
|
otherwise waives the right to collect royalties through
|
||||||
|
any statutory or compulsory licensing scheme; and,
|
||||||
|
iii.Voluntary License Schemes. The Licensor reserves the
|
||||||
|
right to collect royalties, whether individually or, in
|
||||||
|
the event that the Licensor is a member of a collecting
|
||||||
|
society that administers voluntary licensing schemes, via
|
||||||
|
that society, from any exercise by You of the rights
|
||||||
|
granted under this License that is for a purpose or use
|
||||||
|
which is otherwise than noncommercial as permitted under
|
||||||
|
Section 4(b).
|
||||||
|
|
||||||
|
i. Except as otherwise agreed in writing by the Licensor or as
|
||||||
|
may be otherwise permitted by applicable law, if You Reproduce,
|
||||||
|
Distribute or Publicly Perform the Work either by itself or as
|
||||||
|
part of any Adaptations or Collections, You must not distort,
|
||||||
|
mutilate, modify or take other derogatory action in relation to
|
||||||
|
the Work which would be prejudicial to the Original Author's
|
||||||
|
honor or reputation. Licensor agrees that in those jurisdictions
|
||||||
|
(e.g. Japan), in which any exercise of the right granted in
|
||||||
|
Section 3(b) of this License (the right to make Adaptations)
|
||||||
|
would be deemed to be a distortion, mutilation, modification or
|
||||||
|
other derogatory action prejudicial to the Original Author's
|
||||||
|
honor and reputation, the Licensor will waive or not assert, as
|
||||||
|
appropriate, this Section, to the fullest extent permitted by
|
||||||
|
the applicable national law, to enable You to reasonably
|
||||||
|
exercise Your right under Section 3(b) of this License (right to
|
||||||
|
make Adaptations) but not otherwise.
|
||||||
|
|
||||||
|
5. REPRESENTATIONS, WARRANTIES AND DISCLAIMER
|
||||||
|
|
||||||
|
UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR
|
||||||
|
OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY
|
||||||
|
KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE,
|
||||||
|
INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF
|
||||||
|
LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF
|
||||||
|
ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW
|
||||||
|
THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO
|
||||||
|
YOU.
|
||||||
|
|
||||||
|
6. LIMITATION ON LIABILITY
|
||||||
|
|
||||||
|
EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL
|
||||||
|
LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL,
|
||||||
|
INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF
|
||||||
|
THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED
|
||||||
|
OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
7. TERMINATION
|
||||||
|
|
||||||
|
a. This License and the rights granted hereunder will terminate
|
||||||
|
automatically upon any breach by You of the terms of this
|
||||||
|
License. Individuals or entities who have received Adaptations
|
||||||
|
or Collections from You under this License, however, will not
|
||||||
|
have their licenses terminated provided such individuals or
|
||||||
|
entities remain in full compliance with those licenses. Sections
|
||||||
|
1, 2, 5, 6, 7, and 8 will survive any termination of this
|
||||||
|
License.
|
||||||
|
|
||||||
|
b. Subject to the above terms and conditions, the license
|
||||||
|
granted here is perpetual (for the duration of the applicable
|
||||||
|
copyright in the Work). Notwithstanding the above, Licensor
|
||||||
|
reserves the right to release the Work under different license
|
||||||
|
terms or to stop distributing the Work at any time; provided,
|
||||||
|
however that any such election will not serve to withdraw this
|
||||||
|
License (or any other license that has been, or is required to
|
||||||
|
be, granted under the terms of this License), and this License
|
||||||
|
will continue in full force and effect unless terminated as
|
||||||
|
stated above.
|
||||||
|
|
||||||
|
8. MISCELLANEOUS
|
||||||
|
|
||||||
|
a. Each time You Distribute or Publicly Perform the Work or a
|
||||||
|
Collection, the Licensor offers to the recipient a license to
|
||||||
|
the Work on the same terms and conditions as the license granted
|
||||||
|
to You under this License.
|
||||||
|
|
||||||
|
b. Each time You Distribute or Publicly Perform an Adaptation,
|
||||||
|
Licensor offers to the recipient a license to the original Work
|
||||||
|
on the same terms and conditions as the license granted to You
|
||||||
|
under this License.
|
||||||
|
|
||||||
|
c. If the Work is classified as Software, each time You Distribute
|
||||||
|
or Publicly Perform an Adaptation, Licensor offers to the recipient
|
||||||
|
a copy and/or URI of the corresponding Source Code on the same
|
||||||
|
terms and conditions as the license granted to You under this License.
|
||||||
|
|
||||||
|
d. If the Work is used as a Web Service, each time You Distribute
|
||||||
|
or Publicly Perform an Adaptation, or serve data derived from the
|
||||||
|
Software, the Licensor offers to any recipients of the data a copy
|
||||||
|
and/or URI of the corresponding Source Code on the same terms and
|
||||||
|
conditions as the license granted to You under this License.
|
||||||
|
|
||||||
|
e. If any provision of this License is invalid or unenforceable
|
||||||
|
under applicable law, it shall not affect the validity or
|
||||||
|
enforceability of the remainder of the terms of this License,
|
||||||
|
and without further action by the parties to this agreement,
|
||||||
|
such provision shall be reformed to the minimum extent necessary
|
||||||
|
to make such provision valid and enforceable.
|
||||||
|
|
||||||
|
f. No term or provision of this License shall be deemed waived
|
||||||
|
and no breach consented to unless such waiver or consent shall
|
||||||
|
be in writing and signed by the party to be charged with such
|
||||||
|
waiver or consent.
|
||||||
|
|
||||||
|
g. This License constitutes the entire agreement between the
|
||||||
|
parties with respect to the Work licensed here. There are no
|
||||||
|
understandings, agreements or representations with respect to
|
||||||
|
the Work not specified here. Licensor shall not be bound by any
|
||||||
|
additional provisions that may appear in any communication from
|
||||||
|
You. This License may not be modified without the mutual written
|
||||||
|
agreement of the Licensor and You.
|
||||||
|
|
||||||
|
h. The rights granted under, and the subject matter referenced,
|
||||||
|
in this License were drafted utilizing the terminology of the
|
||||||
|
Berne Convention for the Protection of Literary and Artistic
|
||||||
|
Works (as amended on September 28, 1979), the Rome Convention of
|
||||||
|
1961, the WIPO Copyright Treaty of 1996, the WIPO Performances
|
||||||
|
and Phonograms Treaty of 1996 and the Universal Copyright
|
||||||
|
Convention (as revised on July 24, 1971). These rights and
|
||||||
|
subject matter take effect in the relevant jurisdiction in which
|
||||||
|
the License terms are sought to be enforced according to the
|
||||||
|
corresponding provisions of the implementation of those treaty
|
||||||
|
provisions in the applicable national law. If the standard suite
|
||||||
|
of rights granted under applicable copyright law includes
|
||||||
|
additional rights not granted under this License, such
|
||||||
|
additional rights are deemed to be included in the License; this
|
||||||
|
License is not intended to restrict the license of any rights
|
||||||
|
under applicable law.
|
||||||
|
|
||||||
1. The origin of this software must not be misrepresented; you must not
|
|
||||||
claim that you wrote the original software. If you use this software
|
|
||||||
in a product, an acknowledgment in the product documentation would be
|
|
||||||
appreciated but is not required.
|
|
||||||
2. Altered source versions must be plainly marked as such, and must not be
|
|
||||||
misrepresented as being the original software.
|
|
||||||
3. This notice may not be removed or altered from any source distribution.
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
- immutable component system?
|
||||||
|
|
||||||
|
- method to remove all components of a type without destroying Entities
|
||||||
|
- method to remove a component of a type without destroying entity
|
||||||
|
|
||||||
|
- look at test coverage
|
||||||
|
- docs
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 16
|
# Visual Studio 15
|
||||||
VisualStudioVersion = 16.0.30717.126
|
VisualStudioVersion = 15.0.26124.0
|
||||||
MinimumVisualStudioVersion = 15.0.26124.0
|
MinimumVisualStudioVersion = 15.0.26124.0
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "encompass-cs", "encompass-cs\encompass-cs.csproj", "{B862FC25-0740-4CEA-BC53-3C5F43DCD985}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "encompass-cs", "encompass-cs\encompass-cs.csproj", "{B862FC25-0740-4CEA-BC53-3C5F43DCD985}"
|
||||||
EndProject
|
EndProject
|
||||||
|
@ -9,28 +9,38 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "test", "test\test.csproj",
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
Debug|x64 = Debug|x64
|
Debug|x64 = Debug|x64
|
||||||
Debug|x86 = Debug|x86
|
Debug|x86 = Debug|x86
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
Release|x64 = Release|x64
|
Release|x64 = Release|x64
|
||||||
Release|x86 = Release|x86
|
Release|x86 = Release|x86
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.ActiveCfg = Debug|x64
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.Build.0 = Debug|x64
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.ActiveCfg = Debug|x86
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.Build.0 = Debug|x86
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.ActiveCfg = Release|x64
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.Build.0 = Release|x64
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.ActiveCfg = Release|x86
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.Build.0 = Release|x86
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.ActiveCfg = Debug|x64
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.Build.0 = Debug|x64
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.ActiveCfg = Debug|x86
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.Build.0 = Debug|x86
|
{B862FC25-0740-4CEA-BC53-3C5F43DCD985}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.ActiveCfg = Release|x64
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.Build.0 = Release|x64
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.ActiveCfg = Release|x86
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.Build.0 = Release|x86
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{23D6C29C-A310-4D3E-8455-8D0B8CA1FC06}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Encompass
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
|
||||||
public class Adds : Attribute
|
|
||||||
{
|
|
||||||
public readonly HashSet<Type> addTypes;
|
|
||||||
|
|
||||||
public Adds(params Type[] addTypes)
|
|
||||||
{
|
|
||||||
this.addTypes = new HashSet<Type>(addTypes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,11 +5,11 @@ namespace Encompass
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class DefaultWritePriority : Attribute
|
public class DefaultWritePriority : Attribute
|
||||||
{
|
{
|
||||||
public int WritePriority { get; }
|
public int writePriority;
|
||||||
|
|
||||||
public DefaultWritePriority(int writePriority)
|
public DefaultWritePriority(int writePriority)
|
||||||
{
|
{
|
||||||
WritePriority = writePriority;
|
this.writePriority = writePriority;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,27 @@
|
||||||
using Encompass.Exceptions;
|
using Encompass.Exceptions;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class QueryWith : Attribute
|
public class QueryWith : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> QueryWithTypes = new HashSet<Type>();
|
public readonly HashSet<Type> queryWithTypes = new HashSet<Type>();
|
||||||
|
|
||||||
public QueryWith(params Type[] queryWithTypes)
|
public QueryWith(params Type[] queryWithTypes)
|
||||||
{
|
{
|
||||||
foreach (var queryWithType in queryWithTypes)
|
foreach (var queryWithType in queryWithTypes)
|
||||||
{
|
{
|
||||||
QueryWithTypes.Add(queryWithType);
|
var isComponent = queryWithType.GetInterfaces().Contains(typeof(IComponent));
|
||||||
|
|
||||||
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalReadTypeException("{0} must be a Component", queryWithType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.queryWithTypes.Add(queryWithType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,27 @@
|
||||||
using System;
|
using Encompass.Exceptions;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class QueryWithout : Attribute
|
public class QueryWithout : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> QueryWithoutTypes = new HashSet<Type>();
|
public readonly HashSet<Type> queryWithoutTypes = new HashSet<Type>();
|
||||||
|
|
||||||
public QueryWithout(params Type[] queryWithoutTypes)
|
public QueryWithout(params Type[] queryWithoutTypes)
|
||||||
{
|
{
|
||||||
foreach (var type in queryWithoutTypes)
|
foreach (var type in queryWithoutTypes)
|
||||||
{
|
{
|
||||||
QueryWithoutTypes.Add(type);
|
var isComponent = type.GetInterfaces().Contains(typeof(IComponent));
|
||||||
|
|
||||||
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalReadTypeException("{0} must be a Component", type.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.queryWithoutTypes.Add(type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class Reads : Attribute
|
public class Reads : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> ReadTypes = new HashSet<Type>();
|
public readonly HashSet<Type> readTypes = new HashSet<Type>();
|
||||||
|
|
||||||
public Reads(params Type[] readTypes)
|
public Reads(params Type[] readTypes)
|
||||||
{
|
{
|
||||||
foreach (var readType in readTypes)
|
foreach (var readType in readTypes)
|
||||||
{
|
{
|
||||||
ReadTypes.Add(readType);
|
var isComponent = readType.GetInterfaces().Contains(typeof(IComponent));
|
||||||
|
|
||||||
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalReadTypeException("{0} must be a Component", readType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.readTypes.Add(readType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class ReadsImmediate : Attribute
|
public class ReadsImmediate : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> ReadImmediateTypes = new HashSet<Type>();
|
public readonly HashSet<Type> readImmediateTypes = new HashSet<Type>();
|
||||||
|
|
||||||
public ReadsImmediate(params Type[] readImmediateTypes)
|
public ReadsImmediate(params Type[] readImmediateTypes)
|
||||||
{
|
{
|
||||||
foreach (var readImmediateType in readImmediateTypes)
|
foreach (var readImmediateType in readImmediateTypes)
|
||||||
{
|
{
|
||||||
ReadImmediateTypes.Add(readImmediateType);
|
var isComponent = readImmediateType.GetInterfaces().Contains(typeof(IComponent));
|
||||||
|
|
||||||
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalReadTypeException("{0} must be a Component", readImmediateType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.readImmediateTypes.Add(readImmediateType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class Receives : Attribute
|
public class Receives : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> ReceiveTypes;
|
public readonly HashSet<Type> receiveTypes;
|
||||||
|
|
||||||
public Receives(params Type[] receiveTypes)
|
public Receives(params Type[] receiveTypes)
|
||||||
{
|
{
|
||||||
ReceiveTypes = new HashSet<Type>(receiveTypes);
|
foreach (var receiveType in receiveTypes)
|
||||||
|
{
|
||||||
|
var isMessage = receiveType.GetInterfaces().Contains(typeof(IMessage));
|
||||||
|
if (!isMessage)
|
||||||
|
{
|
||||||
|
throw new IllegalSendTypeException("{0} must be a Message", receiveType.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.receiveTypes = new HashSet<Type>(receiveTypes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class Sends : Attribute
|
public class Sends : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> SendTypes;
|
public readonly HashSet<Type> sendTypes;
|
||||||
|
|
||||||
public Sends(params Type[] sendTypes)
|
public Sends(params Type[] sendTypes)
|
||||||
{
|
{
|
||||||
this.SendTypes = new HashSet<Type>(sendTypes);
|
foreach (var sendType in sendTypes)
|
||||||
|
{
|
||||||
|
var isMessage = sendType.GetInterfaces().Contains(typeof(IMessage));
|
||||||
|
if (!isMessage)
|
||||||
|
{
|
||||||
|
throw new IllegalSendTypeException("{0} must be a Message", sendType.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sendTypes = new HashSet<Type>(sendTypes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,40 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
||||||
public class Writes : Attribute
|
public class Writes : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> WriteTypes = new HashSet<Type>();
|
public readonly HashSet<Type> writeTypes = new HashSet<Type>();
|
||||||
public readonly Dictionary<Type, int> Priorities = new Dictionary<Type, int>();
|
public Dictionary<Type, int> priorities = new Dictionary<Type, int>();
|
||||||
|
|
||||||
public Writes(params Type[] writeTypes)
|
public Writes(params Type[] writeTypes)
|
||||||
{
|
{
|
||||||
foreach (var writeType in writeTypes)
|
foreach (var writeType in writeTypes)
|
||||||
{
|
{
|
||||||
WriteTypes.Add(writeType);
|
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent));
|
||||||
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.writeTypes.Add(writeType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Writes(Type writeType, int priority)
|
public Writes(Type writeType, int priority)
|
||||||
{
|
{
|
||||||
WriteTypes.Add(writeType);
|
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent));
|
||||||
Priorities.Add(writeType, priority);
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
writeTypes.Add(writeType);
|
||||||
|
priorities.Add(writeType, priority);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,26 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class WritesImmediate : Attribute
|
public class WritesImmediate : Attribute
|
||||||
{
|
{
|
||||||
public readonly HashSet<Type> WriteImmediateTypes = new HashSet<Type>();
|
public readonly HashSet<Type> writeImmediateTypes = new HashSet<Type>();
|
||||||
|
|
||||||
public WritesImmediate(params Type[] writeImmediateTypes)
|
public WritesImmediate(params Type[] writeImmediateTypes)
|
||||||
{
|
{
|
||||||
foreach (var writeImmediateType in writeImmediateTypes)
|
foreach (var writeImmediateType in writeImmediateTypes)
|
||||||
{
|
{
|
||||||
WriteImmediateTypes.Add(writeImmediateType);
|
var isComponent = writeImmediateType.GetInterfaces().Contains(typeof(IComponent));
|
||||||
|
if (!isComponent)
|
||||||
|
{
|
||||||
|
throw new IllegalWriteImmediateTypeException("{0} must be a Component", writeImmediateType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.writeImmediateTypes.Add(writeImmediateType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,156 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Encompass
|
|
||||||
{
|
|
||||||
public static unsafe class MemoryHelper
|
|
||||||
{
|
|
||||||
public static void Copy(uint* src, uint* dest, int count)
|
|
||||||
{
|
|
||||||
for (; count != 0; count--) *dest++ = *src++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Fill(uint* p, int count, uint value)
|
|
||||||
{
|
|
||||||
for (; count != 0; count--) *p++ = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void And(uint* p, uint* q, uint* result, int count)
|
|
||||||
{
|
|
||||||
for (; count != 0; count--) *result++ = *p++ & *q++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Or(uint* p, uint* q, uint* result, int count)
|
|
||||||
{
|
|
||||||
for (; count != 0; count--) *result++ = *p++ | *q++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Not(uint* p, uint* result, int count)
|
|
||||||
{
|
|
||||||
for (; count != 0; count--) *result++ = ~*p++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool Equal(uint* p, uint* q, int count)
|
|
||||||
{
|
|
||||||
for (; count != 0; count--) if (*p++ != *q++) { return false; }
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public unsafe struct BitSet512 : IEquatable<BitSet512>
|
|
||||||
{
|
|
||||||
public static BitSet512 Zero { get; } = new BitSet512(0);
|
|
||||||
public static BitSet512 Ones { get; } = new BitSet512(uint.MaxValue);
|
|
||||||
|
|
||||||
private const int _uintLength = 16;
|
|
||||||
|
|
||||||
private fixed uint _buffer[_uintLength];
|
|
||||||
|
|
||||||
public BitSet512(uint value)
|
|
||||||
{
|
|
||||||
fixed (uint* p = _buffer) MemoryHelper.Fill(p, _uintLength, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BitSet512(uint* src)
|
|
||||||
{
|
|
||||||
fixed (uint* dest = _buffer) MemoryHelper.Copy(src, dest, _uintLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BitSet512 operator &(BitSet512 a, BitSet512 b)
|
|
||||||
{
|
|
||||||
var tmp = stackalloc uint[_uintLength];
|
|
||||||
MemoryHelper.And(a._buffer, b._buffer, tmp, _uintLength);
|
|
||||||
return new BitSet512(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BitSet512 operator |(BitSet512 a, BitSet512 b)
|
|
||||||
{
|
|
||||||
var tmp = stackalloc uint[_uintLength];
|
|
||||||
MemoryHelper.Or(a._buffer, b._buffer, tmp, _uintLength);
|
|
||||||
return new BitSet512(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BitSet512 operator ~(BitSet512 a)
|
|
||||||
{
|
|
||||||
var tmp = stackalloc uint[_uintLength];
|
|
||||||
MemoryHelper.Not(a._buffer, tmp, _uintLength);
|
|
||||||
return new BitSet512(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator ==(BitSet512 left, BitSet512 right)
|
|
||||||
{
|
|
||||||
return left.Equals(right);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator !=(BitSet512 left, BitSet512 right)
|
|
||||||
{
|
|
||||||
return !(left == right);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BitSet512 Set(int index)
|
|
||||||
{
|
|
||||||
var tmp = stackalloc uint[_uintLength];
|
|
||||||
fixed (uint* p = _buffer) MemoryHelper.Copy(p, tmp, _uintLength);
|
|
||||||
tmp[index / 32] |= (uint)(1 << index % 32);
|
|
||||||
return new BitSet512(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BitSet512 UnSet(int index)
|
|
||||||
{
|
|
||||||
var tmp = stackalloc uint[_uintLength];
|
|
||||||
fixed (uint* p = _buffer) MemoryHelper.Copy(p, tmp, _uintLength);
|
|
||||||
tmp[index / 32] &= ~(uint)(1 << index % 32);
|
|
||||||
return new BitSet512(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Get(int bitIndex)
|
|
||||||
{
|
|
||||||
var bitInt = (uint)(1 << bitIndex % 32);
|
|
||||||
return (_buffer[bitIndex / 32] & bitInt) == bitInt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool AllTrue()
|
|
||||||
{
|
|
||||||
return this == Ones;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool AllFalse()
|
|
||||||
{
|
|
||||||
return this == Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BitSet512 BitwiseAnd(BitSet512 left, BitSet512 right)
|
|
||||||
{
|
|
||||||
return left & right;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BitSet512 BitwiseOr(BitSet512 left, BitSet512 right)
|
|
||||||
{
|
|
||||||
return left | right;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BitSet512 OnesComplement(BitSet512 bitSet)
|
|
||||||
{
|
|
||||||
return ~bitSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
|
||||||
{
|
|
||||||
return obj is BitSet512 set && Equals(set);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Equals(BitSet512 other)
|
|
||||||
{
|
|
||||||
fixed (uint* p = _buffer) return MemoryHelper.Equal(p, other._buffer, _uintLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
var hc = 0;
|
|
||||||
for (var i = 0; i < _uintLength; i++)
|
|
||||||
{
|
|
||||||
hc ^= _buffer[i].GetHashCode();
|
|
||||||
}
|
|
||||||
return hc;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using MoonTools.FastCollections;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
@ -5,49 +6,49 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
internal class ComponentBitSet
|
internal class ComponentBitSet
|
||||||
{
|
{
|
||||||
private readonly Dictionary<int, BitSet512> _entities = new Dictionary<int, BitSet512>();
|
Dictionary<int, BitSet512> entities = new Dictionary<int, BitSet512>();
|
||||||
private readonly Dictionary<Type, int> _typeToIndex;
|
Dictionary<Type, int> TypeToIndex { get; }
|
||||||
|
|
||||||
public ComponentBitSet(Dictionary<Type, int> typeToIndex)
|
public ComponentBitSet(Dictionary<Type, int> typeToIndex)
|
||||||
{
|
{
|
||||||
_typeToIndex = typeToIndex;
|
TypeToIndex = typeToIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
_entities.Clear();
|
entities.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddEntity(int entityID)
|
public void AddEntity(int entityID)
|
||||||
{
|
{
|
||||||
_entities.Add(entityID, BitSet512.Zero);
|
entities.Add(entityID, BitSet512.Zero);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set<TComponent>(int entityID) where TComponent : struct
|
public void Set<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (!_entities.ContainsKey(entityID)) { AddEntity(entityID); }
|
if (!entities.ContainsKey(entityID)) { AddEntity(entityID); }
|
||||||
_entities[entityID] = _entities[entityID].Set(_typeToIndex[typeof(TComponent)]);
|
entities[entityID] = entities[entityID].Set(TypeToIndex[typeof(TComponent)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct
|
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (_entities.ContainsKey(entityID))
|
if (entities.ContainsKey(entityID))
|
||||||
{
|
{
|
||||||
_entities[entityID] = _entities[entityID].UnSet(_typeToIndex[typeof(TComponent)]);
|
entities[entityID] = entities[entityID].UnSet(TypeToIndex[typeof(TComponent)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveEntity(int entityID)
|
public void RemoveEntity(int entityID)
|
||||||
{
|
{
|
||||||
if (_entities.ContainsKey(entityID))
|
if (entities.ContainsKey(entityID))
|
||||||
{
|
{
|
||||||
_entities.Remove(entityID);
|
entities.Remove(entityID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public BitSet512 EntityBitArray(int entityID)
|
public BitSet512 EntityBitArray(int entityID)
|
||||||
{
|
{
|
||||||
return _entities.ContainsKey(entityID) ? _entities[entityID] : BitSet512.Zero;
|
return entities.ContainsKey(entityID) ? entities[entityID] : BitSet512.Zero;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,48 +3,39 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal class ComponentDeltaStore
|
internal class ComponentDeltaStore : ComponentStore
|
||||||
{
|
{
|
||||||
private readonly ComponentStore _store;
|
|
||||||
private readonly Dictionary<Type, Replayer> _replayers = new Dictionary<Type, Replayer>();
|
private readonly Dictionary<Type, Replayer> _replayers = new Dictionary<Type, Replayer>();
|
||||||
private readonly HashSet<Replayer> _currentReplayers = new HashSet<Replayer>();
|
private readonly HashSet<Replayer> _currentReplayers = new HashSet<Replayer>();
|
||||||
|
|
||||||
public IEnumerable<Replayer> CurrentReplayers { get { return _currentReplayers; } }
|
public IEnumerable<Replayer> CurrentReplayers { get { return _currentReplayers; } }
|
||||||
|
|
||||||
public ComponentDeltaStore(Dictionary<Type, int> typeToIndex)
|
public ComponentDeltaStore(Dictionary<Type, int> typeToIndex) : base(typeToIndex)
|
||||||
{
|
{
|
||||||
_store = new ComponentStore(typeToIndex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterComponentType<TComponent>() where TComponent : struct
|
public override void RegisterComponentType<TComponent>()
|
||||||
{
|
{
|
||||||
_store.RegisterComponentType<TComponent>();
|
base.RegisterComponentType<TComponent>();
|
||||||
if (!_replayers.ContainsKey(typeof(TComponent)))
|
if (!_replayers.ContainsKey(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
_replayers.Add(typeof(TComponent), new Replayer<TComponent>(this));
|
_replayers.Add(typeof(TComponent), new Replayer<TComponent>(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref readonly TComponent GetComponent<TComponent>(int entityID) where TComponent : struct
|
public override void Set<TComponent>(int entityID, TComponent component)
|
||||||
{
|
{
|
||||||
return ref _store.Get<TComponent>(entityID);
|
base.Set(entityID, component);
|
||||||
}
|
|
||||||
|
|
||||||
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
|
|
||||||
{
|
|
||||||
_store.Set(entityID, component);
|
|
||||||
RegisterComponentType<TComponent>();
|
|
||||||
var replayer = _replayers[typeof(TComponent)];
|
var replayer = _replayers[typeof(TComponent)];
|
||||||
_currentReplayers.Add(replayer);
|
_currentReplayers.Add(replayer);
|
||||||
replayer.UnMarkRemoval(entityID);
|
replayer.UnMarkRemoval(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
|
public override bool Set<TComponent>(int entityID, TComponent component, int priority)
|
||||||
{
|
{
|
||||||
var result = _store.Set(entityID, component, priority);
|
var result = base.Set(entityID, component, priority);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
RegisterComponentType<TComponent>();
|
|
||||||
var replayer = _replayers[typeof(TComponent)];
|
var replayer = _replayers[typeof(TComponent)];
|
||||||
_currentReplayers.Add(replayer);
|
_currentReplayers.Add(replayer);
|
||||||
replayer.UnMarkRemoval(entityID);
|
replayer.UnMarkRemoval(entityID);
|
||||||
|
@ -52,12 +43,11 @@ namespace Encompass
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
|
public override bool Remove<TComponent>(int entityID, int priority)
|
||||||
{
|
{
|
||||||
var result = _store.Remove<TComponent>(entityID, priority);
|
var result = base.Remove<TComponent>(entityID, priority);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
RegisterComponentType<TComponent>();
|
|
||||||
var replayer = _replayers[typeof(TComponent)];
|
var replayer = _replayers[typeof(TComponent)];
|
||||||
_currentReplayers.Add(replayer);
|
_currentReplayers.Add(replayer);
|
||||||
replayer.MarkRemoval(entityID);
|
replayer.MarkRemoval(entityID);
|
||||||
|
@ -65,23 +55,18 @@ namespace Encompass
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(int entityID)
|
public override void Remove(int entityID)
|
||||||
{
|
{
|
||||||
_store.Remove(entityID);
|
base.Remove(entityID);
|
||||||
foreach (var replayer in CurrentReplayers)
|
foreach (var replayer in CurrentReplayers)
|
||||||
{
|
{
|
||||||
replayer.MarkRemoval(entityID);
|
replayer.MarkRemoval(entityID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<Entity> AllEntities<TComponent>() where TComponent : struct
|
public override void ClearAll()
|
||||||
{
|
{
|
||||||
return _store.AllEntities<TComponent>();
|
base.ClearAll();
|
||||||
}
|
|
||||||
|
|
||||||
public void ClearAll()
|
|
||||||
{
|
|
||||||
_store.ClearAll();
|
|
||||||
foreach (var replayer in _currentReplayers)
|
foreach (var replayer in _currentReplayers)
|
||||||
{
|
{
|
||||||
replayer.Clear();
|
replayer.Clear();
|
||||||
|
|
|
@ -1,45 +1,49 @@
|
||||||
using Encompass.Exceptions;
|
using MoonTools.FastCollections;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal sealed class ComponentStore
|
internal class ComponentStore
|
||||||
{
|
{
|
||||||
private Dictionary<Type, TypedComponentStore> _stores = new Dictionary<Type, TypedComponentStore>(512);
|
private Dictionary<Type, TypedComponentStore> Stores = new Dictionary<Type, TypedComponentStore>(512);
|
||||||
public ComponentBitSet ComponentBitSet { get; private set; }
|
public ComponentBitSet ComponentBitSet { get; private set; }
|
||||||
private readonly Dictionary<Type, int> _typeToIndex;
|
|
||||||
|
|
||||||
public ComponentStore(Dictionary<Type, int> typeToIndex)
|
public ComponentStore(Dictionary<Type, int> typeToIndex)
|
||||||
{
|
{
|
||||||
_typeToIndex = typeToIndex;
|
|
||||||
ComponentBitSet = new ComponentBitSet(typeToIndex);
|
ComponentBitSet = new ComponentBitSet(typeToIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterComponentType<TComponent>() where TComponent : struct
|
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable()
|
||||||
{
|
{
|
||||||
if (!_stores.ContainsKey(typeof(TComponent)))
|
foreach (var entry in Stores)
|
||||||
|
{
|
||||||
|
yield return (entry.Key, entry.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
if (!Stores.ContainsKey(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
var store = new TypedComponentStore<TComponent>();
|
var store = new TypedComponentStore<TComponent>();
|
||||||
_stores.Add(typeof(TComponent), store);
|
Stores.Add(typeof(TComponent), store);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct
|
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
|
return Stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Has<TComponent>(int entityID) where TComponent : struct
|
public bool Has<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return Lookup<TComponent>().Has(entityID);
|
return Lookup<TComponent>().Has(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Has(Type type, int entityID)
|
public bool Has(Type type, int entityID)
|
||||||
{
|
{
|
||||||
return _stores[type].Has(entityID);
|
return Stores.ContainsKey(type) && Stores[type].Has(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BitSet512 EntityBitArray(int entityID)
|
public BitSet512 EntityBitArray(int entityID)
|
||||||
|
@ -47,32 +51,18 @@ namespace Encompass
|
||||||
return ComponentBitSet.EntityBitArray(entityID);
|
return ComponentBitSet.EntityBitArray(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref TComponent Get<TComponent>(int entityID) where TComponent : struct
|
public TComponent Get<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref Lookup<TComponent>().Get(entityID);
|
return Lookup<TComponent>().Get(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref readonly TComponent Singular<TComponent>() where TComponent : struct
|
public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
|
||||||
if (!Any<TComponent>()) { throw new NoComponentOfTypeException("No component of type {0} exists", typeof(TComponent).Name); }
|
|
||||||
|
|
||||||
return ref Lookup<TComponent>().Singular();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref readonly Entity SingularEntity<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
if (!Any<TComponent>()) { throw new NoComponentOfTypeException("No component of type {0} exists", typeof(TComponent).Name); }
|
|
||||||
|
|
||||||
return ref Lookup<TComponent>().SingularEntity();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
|
|
||||||
{
|
{
|
||||||
Lookup<TComponent>().Set(entityID, component);
|
Lookup<TComponent>().Set(entityID, component);
|
||||||
ComponentBitSet.Set<TComponent>(entityID);
|
ComponentBitSet.Set<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Set<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
|
public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (Lookup<TComponent>().Set(entityID, component, priority))
|
if (Lookup<TComponent>().Set(entityID, component, priority))
|
||||||
{
|
{
|
||||||
|
@ -82,7 +72,7 @@ namespace Encompass
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
|
public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (Lookup<TComponent>().Remove(entityID, priority))
|
if (Lookup<TComponent>().Remove(entityID, priority))
|
||||||
{
|
{
|
||||||
|
@ -92,63 +82,59 @@ namespace Encompass
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ForceRemove<TComponent>(int entityID) where TComponent : struct
|
public void ForceRemove<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
Lookup<TComponent>().ForceRemove(entityID);
|
Lookup<TComponent>().ForceRemove(entityID);
|
||||||
ComponentBitSet.RemoveComponent<TComponent>(entityID);
|
ComponentBitSet.RemoveComponent<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(int entityID)
|
public virtual void Remove(int entityID)
|
||||||
{
|
{
|
||||||
foreach (var entry in _stores.Values)
|
foreach (var entry in Stores.Values)
|
||||||
{
|
{
|
||||||
entry.ForceRemove(entityID);
|
entry.ForceRemove(entityID);
|
||||||
}
|
}
|
||||||
ComponentBitSet.RemoveEntity(entityID);
|
ComponentBitSet.RemoveEntity(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Any<TComponent>() where TComponent : struct
|
public bool Any<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return Lookup<TComponent>().Count > 0;
|
return Lookup<TComponent>().Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<TComponent> All<TComponent>() where TComponent : struct
|
public IEnumerable<(int, Type, IComponent)> AllInterfaceTyped()
|
||||||
{
|
{
|
||||||
return Lookup<TComponent>().AllComponents();
|
foreach (var store in Stores.Values)
|
||||||
|
{
|
||||||
|
foreach (var thing in store.AllInterfaceTyped())
|
||||||
|
{
|
||||||
|
yield return thing;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TComponent> AllAsEnumerable<TComponent>() where TComponent : struct
|
public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return Lookup<TComponent>().AllComponentsAsEnumerable();
|
return Lookup<TComponent>().All();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<Entity> AllEntities<TComponent>() where TComponent : struct
|
public void Clear<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
|
||||||
return Lookup<TComponent>().AllEntities();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<Entity> AllEntitiesAsEnumerable<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return Lookup<TComponent>().AllEntitiesAsEnumerable();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear<TComponent>() where TComponent : struct
|
|
||||||
{
|
{
|
||||||
Lookup<TComponent>().Clear();
|
Lookup<TComponent>().Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearAllPriorities()
|
public virtual void ClearAllPriorities()
|
||||||
{
|
{
|
||||||
foreach (var store in _stores.Values)
|
foreach (var store in Stores.Values)
|
||||||
{
|
{
|
||||||
store.ClearPriorities();
|
store.ClearPriorities();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearAll()
|
public virtual void ClearAll()
|
||||||
{
|
{
|
||||||
ComponentBitSet.Clear();
|
ComponentBitSet.Clear();
|
||||||
foreach (var store in _stores.Values)
|
foreach (var store in Stores.Values)
|
||||||
{
|
{
|
||||||
store.Clear();
|
store.Clear();
|
||||||
}
|
}
|
||||||
|
@ -156,7 +142,7 @@ namespace Encompass
|
||||||
|
|
||||||
public void SwapWith(ComponentStore other)
|
public void SwapWith(ComponentStore other)
|
||||||
{
|
{
|
||||||
(_stores, other._stores) = (other._stores, _stores);
|
(Stores, other.Stores) = (other.Stores, Stores);
|
||||||
(ComponentBitSet, other.ComponentBitSet) = (other.ComponentBitSet, ComponentBitSet);
|
(ComponentBitSet, other.ComponentBitSet) = (other.ComponentBitSet, ComponentBitSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,74 +1,66 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Encompass.Exceptions;
|
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal class MessageStore
|
internal class MessageStore
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Type, TypedMessageStore> _stores = new Dictionary<Type, TypedMessageStore>(512);
|
private Dictionary<Type, TypedMessageStore> Stores = new Dictionary<Type, TypedMessageStore>(512);
|
||||||
|
|
||||||
private void RegisterMessageType<TMessage>() where TMessage : struct
|
private void RegisterMessageType<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_stores.Add(typeof(TMessage), new TypedMessageStore<TMessage>());
|
Stores.Add(typeof(TMessage), new TypedMessageStore<TMessage>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct
|
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
if (!_stores.ContainsKey(typeof(TMessage))) { RegisterMessageType<TMessage>(); }
|
if (!Stores.ContainsKey(typeof(TMessage))) { RegisterMessageType<TMessage>(); }
|
||||||
return _stores[typeof(TMessage)] as TypedMessageStore<TMessage>;
|
return Stores[typeof(TMessage)] as TypedMessageStore<TMessage>;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddMessage<TMessage>(in TMessage message) where TMessage : struct
|
public void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
Lookup<TMessage>().Add(message);
|
Lookup<TMessage>().Add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct
|
public void AddMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
Lookup<TMessage>().Add(message, time);
|
Lookup<TMessage>().Add(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
|
public void AddMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
Lookup<TMessage>().AddIgnoringTimeDilation(message, time);
|
Lookup<TMessage>().AddIgnoringTimeDilation(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref readonly TMessage First<TMessage>() where TMessage : struct
|
public TMessage First<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
if (!Any<TMessage>()) { throw new NoMessageOfTypeException("No Message of type {0} exists", typeof(TMessage).Name); }
|
return Lookup<TMessage>().First();
|
||||||
|
|
||||||
return ref Lookup<TMessage>().First();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct
|
public IEnumerable<TMessage> All<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
return Lookup<TMessage>().All();
|
return Lookup<TMessage>().All();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Any<TMessage>() where TMessage : struct
|
public bool Any<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
return Lookup<TMessage>().Any();
|
return Lookup<TMessage>().Any();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
public IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
return Lookup<TMessage>().WithEntity(entityID);
|
return Lookup<TMessage>().WithEntity(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct
|
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
|
||||||
return ref Lookup<TMessage>().FirstWithEntity(entityID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
|
||||||
{
|
{
|
||||||
return Lookup<TMessage>().SomeWithEntity(entityID);
|
return Lookup<TMessage>().SomeWithEntity(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
|
public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
|
||||||
{
|
{
|
||||||
foreach (var store in _stores.Values)
|
foreach (var store in Stores.Values)
|
||||||
{
|
{
|
||||||
store.ProcessDelayedMessages(dilatedDelta, realtimeDelta);
|
store.ProcessDelayedMessages(dilatedDelta, realtimeDelta);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +68,7 @@ namespace Encompass
|
||||||
|
|
||||||
public void ClearAll()
|
public void ClearAll()
|
||||||
{
|
{
|
||||||
foreach (var store in _stores.Values)
|
foreach (var store in Stores.Values)
|
||||||
{
|
{
|
||||||
store.Clear();
|
store.Clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace Encompass
|
||||||
public abstract void Clear();
|
public abstract void Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class Replayer<TComponent> : Replayer where TComponent : struct
|
internal class Replayer<TComponent> : Replayer where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
private readonly ComponentDeltaStore _deltaStore;
|
private readonly ComponentDeltaStore _deltaStore;
|
||||||
private readonly HashSet<int> _removals = new HashSet<int>();
|
private readonly HashSet<int> _removals = new HashSet<int>();
|
||||||
|
@ -22,10 +22,9 @@ namespace Encompass
|
||||||
|
|
||||||
public override void Replay(ComponentStore store)
|
public override void Replay(ComponentStore store)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in _deltaStore.AllEntities<TComponent>())
|
foreach (var (component, entityID) in _deltaStore.All<TComponent>())
|
||||||
{
|
{
|
||||||
ref readonly var component = ref _deltaStore.GetComponent<TComponent>(entity.ID);
|
store.Set(entityID, component);
|
||||||
store.Set(entity.ID, component);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var entityID in _removals)
|
foreach (var entityID in _removals)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal abstract class TypedComponentStore
|
internal abstract class TypedComponentStore
|
||||||
{
|
{
|
||||||
public abstract int Count { get; }
|
public abstract int Count { get; }
|
||||||
|
public abstract IEnumerable<(int, Type, IComponent)> AllInterfaceTyped();
|
||||||
public abstract bool Has(int entity);
|
public abstract bool Has(int entity);
|
||||||
public abstract bool Remove(int entity, int priority);
|
public abstract bool Remove(int entity, int priority);
|
||||||
public abstract void ForceRemove(int entity);
|
public abstract void ForceRemove(int entity);
|
||||||
|
@ -14,72 +14,43 @@ namespace Encompass
|
||||||
public abstract void ClearPriorities();
|
public abstract void ClearPriorities();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct
|
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
private int _nextID = 0;
|
private readonly Dictionary<int, TComponent> store = new Dictionary<int, TComponent>(128);
|
||||||
private readonly Dictionary<int, int> _entityIDToStorageIndex = new Dictionary<int, int>(128);
|
private readonly Dictionary<int, int> priorities = new Dictionary<int, int>(128);
|
||||||
private readonly Dictionary<int, int> _priorities = new Dictionary<int, int>(128);
|
|
||||||
private Entity[] _storageIndexToEntities = new Entity[128];
|
|
||||||
private TComponent[] _components = new TComponent[128];
|
|
||||||
|
|
||||||
public override int Count { get => _entityIDToStorageIndex.Count; }
|
public override int Count { get => store.Count; }
|
||||||
|
|
||||||
public ref TComponent Get(int entityID)
|
public TComponent Get(int entityID)
|
||||||
{
|
{
|
||||||
if (!_entityIDToStorageIndex.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); }
|
if (!store.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); }
|
||||||
return ref _components[_entityIDToStorageIndex[entityID]];
|
return store[entityID];
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref TComponent Singular()
|
public void Set(int entityID, TComponent component)
|
||||||
{
|
{
|
||||||
return ref _components[0];
|
store[entityID] = component;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref Entity SingularEntity()
|
public bool Set(int entityID, TComponent component, int priority)
|
||||||
{
|
{
|
||||||
return ref _storageIndexToEntities[0];
|
if (!priorities.ContainsKey(entityID) || priority < priorities[entityID])
|
||||||
}
|
|
||||||
|
|
||||||
public void Set(int entityID, in TComponent component)
|
|
||||||
{
|
|
||||||
InternalSet(entityID, component);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Set(int entityID, in TComponent component, int priority)
|
|
||||||
{
|
|
||||||
if (!_priorities.ContainsKey(entityID) || priority <= _priorities[entityID]) // if priorities are equal that means it's the same engine
|
|
||||||
{
|
{
|
||||||
InternalSet(entityID, component);
|
store[entityID] = component;
|
||||||
_priorities[entityID] = priority;
|
priorities[entityID] = priority;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InternalSet(int entityID, in TComponent component)
|
|
||||||
{
|
|
||||||
if (!_entityIDToStorageIndex.ContainsKey(entityID))
|
|
||||||
{
|
|
||||||
var index = _nextID++;
|
|
||||||
if (index >= _components.Length)
|
|
||||||
{
|
|
||||||
System.Array.Resize(ref _components, _components.Length * 2);
|
|
||||||
System.Array.Resize(ref _storageIndexToEntities, _storageIndexToEntities.Length * 2);
|
|
||||||
}
|
|
||||||
_entityIDToStorageIndex[entityID] = index;
|
|
||||||
_storageIndexToEntities[index] = new Entity(entityID);
|
|
||||||
}
|
|
||||||
|
|
||||||
_components[_entityIDToStorageIndex[entityID]] = component;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool Remove(int entityID, int priority)
|
public override bool Remove(int entityID, int priority)
|
||||||
{
|
{
|
||||||
if (!_priorities.ContainsKey(entityID) || priority <= _priorities[entityID]) // if priorities are equal that means it's the same engine
|
if (!priorities.ContainsKey(entityID) || priority < priorities[entityID])
|
||||||
{
|
{
|
||||||
ForceRemove(entityID);
|
priorities[entityID] = priority;
|
||||||
_priorities[entityID] = priority;
|
store.Remove(entityID);
|
||||||
|
priorities.Remove(entityID);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,62 +59,40 @@ namespace Encompass
|
||||||
|
|
||||||
public override void ForceRemove(int entityID)
|
public override void ForceRemove(int entityID)
|
||||||
{
|
{
|
||||||
if (_entityIDToStorageIndex.ContainsKey(entityID))
|
store.Remove(entityID);
|
||||||
{
|
priorities.Remove(entityID);
|
||||||
var storageIndex = _entityIDToStorageIndex[entityID];
|
|
||||||
_entityIDToStorageIndex.Remove(entityID);
|
|
||||||
|
|
||||||
// move a component into the hole to maintain contiguous memory
|
|
||||||
if (_nextID > 1 && storageIndex != _nextID - 1)
|
|
||||||
{
|
|
||||||
var lastStorageIndex = _nextID - 1;
|
|
||||||
ref readonly var lastEntity = ref _storageIndexToEntities[lastStorageIndex];
|
|
||||||
|
|
||||||
_entityIDToStorageIndex[lastEntity.ID] = storageIndex;
|
|
||||||
_storageIndexToEntities[storageIndex] = lastEntity;
|
|
||||||
_components[storageIndex] = _components[lastStorageIndex];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
_nextID--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Has(int entityID)
|
public override bool Has(int entityID)
|
||||||
{
|
{
|
||||||
return _entityIDToStorageIndex.ContainsKey(entityID);
|
return store.ContainsKey(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Clear()
|
public override void Clear()
|
||||||
{
|
{
|
||||||
_nextID = 0;
|
store.Clear();
|
||||||
_entityIDToStorageIndex.Clear();
|
priorities.Clear();
|
||||||
_priorities.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void ClearPriorities()
|
public override void ClearPriorities()
|
||||||
{
|
{
|
||||||
_priorities.Clear();
|
priorities.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<Entity> AllEntities()
|
public IEnumerable<(TComponent, int)> All()
|
||||||
{
|
{
|
||||||
return new ReadOnlySpan<Entity>(_storageIndexToEntities, 0, _nextID);
|
foreach (var kvp in store)
|
||||||
|
{
|
||||||
|
yield return (kvp.Value, kvp.Key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Entity> AllEntitiesAsEnumerable()
|
public override IEnumerable<(int, Type, IComponent)> AllInterfaceTyped()
|
||||||
{
|
{
|
||||||
return new ArraySegment<Entity>(_storageIndexToEntities, 0, _nextID);
|
foreach (var kvp in store)
|
||||||
}
|
{
|
||||||
|
yield return (kvp.Key, typeof(TComponent), (IComponent)kvp.Value);
|
||||||
public ReadOnlySpan<TComponent> AllComponents()
|
}
|
||||||
{
|
|
||||||
return new ReadOnlySpan<TComponent>(_components, 0, _nextID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<TComponent> AllComponentsAsEnumerable()
|
|
||||||
{
|
|
||||||
return new ArraySegment<TComponent>(_components, 0, _nextID);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
|
@ -9,119 +8,100 @@ namespace Encompass
|
||||||
public abstract void Clear();
|
public abstract void Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct
|
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
// messages are placed in a contiguous region
|
private readonly List<TMessage> store = new List<TMessage>(128);
|
||||||
// so we can return the collection as a Span
|
private readonly List<(TMessage, double)> delayedStore = new List<(TMessage, double)>(128);
|
||||||
private int _nextIndex = 0;
|
private readonly List<(TMessage, double)> delayedStoreIgnoringTimeDilation = new List<(TMessage, double)>(128);
|
||||||
private TMessage[] _store = new TMessage[128];
|
private readonly Dictionary<int, List<TMessage>> entityToMessage = new Dictionary<int, List<TMessage>>();
|
||||||
private readonly List<(TMessage, double)> _delayedStore = new List<(TMessage, double)>(128);
|
|
||||||
private readonly List<(TMessage, double)> _delayedStoreIgnoringTimeDilation = new List<(TMessage, double)>(128);
|
|
||||||
private readonly Dictionary<int, List<int>> _entityToIndices = new Dictionary<int, List<int>>();
|
|
||||||
|
|
||||||
public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
|
public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
|
||||||
{
|
{
|
||||||
for (var i = _delayedStore.Count - 1; i >= 0; i--)
|
for (int i = delayedStore.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var (message, time) = _delayedStore[i];
|
var (message, time) = delayedStore[i];
|
||||||
|
|
||||||
var updatedTime = time - dilatedDelta;
|
var updatedTime = time - dilatedDelta;
|
||||||
|
|
||||||
if (updatedTime <= 0)
|
if (updatedTime <= 0)
|
||||||
{
|
{
|
||||||
Add(message);
|
Add(message);
|
||||||
_delayedStore.RemoveAt(i);
|
delayedStore.RemoveAt(i);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_delayedStore[i] = (message, updatedTime);
|
delayedStore[i] = (message, updatedTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = _delayedStoreIgnoringTimeDilation.Count - 1; i >= 0; i--)
|
for (int i = delayedStoreIgnoringTimeDilation.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var (message, time) = _delayedStoreIgnoringTimeDilation[i];
|
var (message, time) = delayedStoreIgnoringTimeDilation[i];
|
||||||
|
|
||||||
var updatedTime = time - realtimeDelta;
|
var updatedTime = time - realtimeDelta;
|
||||||
|
|
||||||
if (updatedTime <= 0)
|
if (updatedTime <= 0)
|
||||||
{
|
{
|
||||||
Add(message);
|
Add(message);
|
||||||
_delayedStoreIgnoringTimeDilation.RemoveAt(i);
|
delayedStoreIgnoringTimeDilation.RemoveAt(i);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_delayedStoreIgnoringTimeDilation[i] = (message, updatedTime);
|
delayedStoreIgnoringTimeDilation[i] = (message, updatedTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(in TMessage message)
|
public void Add(TMessage message)
|
||||||
{
|
{
|
||||||
var index = _nextIndex++;
|
store.Add(message);
|
||||||
if (index >= _store.Length)
|
|
||||||
{
|
|
||||||
Array.Resize(ref _store, _store.Length * 2);
|
|
||||||
}
|
|
||||||
_store[index] = message;
|
|
||||||
if (message is IHasEntity entityMessage)
|
if (message is IHasEntity entityMessage)
|
||||||
{
|
{
|
||||||
var entityID = entityMessage.Entity.ID;
|
var entityID = entityMessage.Entity.ID;
|
||||||
if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new List<int>()); }
|
if (!entityToMessage.ContainsKey(entityID)) { entityToMessage.Add(entityID, new List<TMessage>()); }
|
||||||
_entityToIndices[entityID].Add(index);
|
entityToMessage[entityID].Add(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(in TMessage message, double time)
|
public void Add(TMessage message, double time)
|
||||||
{
|
{
|
||||||
_delayedStore.Add((message, time));
|
delayedStore.Add((message, time));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddIgnoringTimeDilation(in TMessage message, double time)
|
public void AddIgnoringTimeDilation(TMessage message, double time)
|
||||||
{
|
{
|
||||||
_delayedStoreIgnoringTimeDilation.Add((message, time));
|
delayedStoreIgnoringTimeDilation.Add((message, time));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ref readonly TMessage First()
|
public TMessage First()
|
||||||
{
|
{
|
||||||
return ref _store[0];
|
return store[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Any()
|
public bool Any()
|
||||||
{
|
{
|
||||||
return _nextIndex != 0;
|
return store.Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReadOnlySpan<TMessage> All()
|
public IEnumerable<TMessage> All()
|
||||||
{
|
{
|
||||||
return new ReadOnlySpan<TMessage>(_store, 0, _nextIndex);
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<TMessage> WithEntity(int entityID)
|
public IEnumerable<TMessage> WithEntity(int entityID)
|
||||||
{
|
{
|
||||||
if (_entityToIndices.ContainsKey(entityID))
|
return entityToMessage.ContainsKey(entityID) ? entityToMessage[entityID] : System.Linq.Enumerable.Empty<TMessage>();
|
||||||
{
|
|
||||||
foreach (var index in _entityToIndices[entityID])
|
|
||||||
{
|
|
||||||
yield return _store[index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref readonly TMessage FirstWithEntity(int entityID)
|
|
||||||
{
|
|
||||||
return ref _store[_entityToIndices[entityID][0]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SomeWithEntity(int entityID)
|
public bool SomeWithEntity(int entityID)
|
||||||
{
|
{
|
||||||
return _entityToIndices.ContainsKey(entityID) && _entityToIndices[entityID].Count > 0;
|
return entityToMessage.ContainsKey(entityID) && entityToMessage[entityID].Count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Clear()
|
public override void Clear()
|
||||||
{
|
{
|
||||||
_nextIndex = 0;
|
store.Clear();
|
||||||
foreach (var set in _entityToIndices.Values)
|
foreach (var set in entityToMessage.Values)
|
||||||
{
|
{
|
||||||
set.Clear();
|
set.Clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,288 +1,272 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using MoonTools.FastCollections;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal class ComponentManager
|
internal class ComponentManager
|
||||||
{
|
{
|
||||||
private readonly ComponentStore _existingComponentStore;
|
private readonly DrawLayerManager drawLayerManager;
|
||||||
private readonly ComponentStore _immediateComponentStore;
|
|
||||||
private readonly ComponentDeltaStore _replayStore;
|
private readonly ComponentStore existingComponentStore;
|
||||||
private readonly ComponentStore _upToDateComponentStore;
|
private readonly ComponentStore immediateComponentStore;
|
||||||
|
private readonly ComponentDeltaStore replayStore;
|
||||||
|
private ComponentStore upToDateComponentStore;
|
||||||
|
|
||||||
public Dictionary<Type, int> TypeToIndex { get; }
|
public Dictionary<Type, int> TypeToIndex { get; }
|
||||||
|
|
||||||
private readonly HashSet<int> _entitiesMarkedForRemoval = new HashSet<int>();
|
private readonly HashSet<int> entitiesMarkedForRemoval = new HashSet<int>();
|
||||||
|
|
||||||
internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } }
|
internal ComponentBitSet ImmediateBits { get { return immediateComponentStore.ComponentBitSet; } }
|
||||||
internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } }
|
internal ComponentBitSet ExistingBits { get { return existingComponentStore.ComponentBitSet; } }
|
||||||
|
|
||||||
public ComponentManager(Dictionary<Type, int> typeToIndex)
|
public ComponentManager(DrawLayerManager drawLayerManager, Dictionary<Type, int> typeToIndex)
|
||||||
{
|
{
|
||||||
_existingComponentStore = new ComponentStore(typeToIndex);
|
this.drawLayerManager = drawLayerManager;
|
||||||
_immediateComponentStore = new ComponentStore(typeToIndex);
|
existingComponentStore = new ComponentStore(typeToIndex);
|
||||||
_replayStore = new ComponentDeltaStore(typeToIndex);
|
immediateComponentStore = new ComponentStore(typeToIndex);
|
||||||
_upToDateComponentStore = new ComponentStore(typeToIndex);
|
replayStore = new ComponentDeltaStore(typeToIndex);
|
||||||
|
upToDateComponentStore = new ComponentStore(typeToIndex);
|
||||||
TypeToIndex = typeToIndex;
|
TypeToIndex = typeToIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterComponentType<TComponent>() where TComponent : struct
|
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
_existingComponentStore.RegisterComponentType<TComponent>();
|
existingComponentStore.RegisterComponentType<TComponent>();
|
||||||
_immediateComponentStore.RegisterComponentType<TComponent>();
|
immediateComponentStore.RegisterComponentType<TComponent>();
|
||||||
_replayStore.RegisterComponentType<TComponent>();
|
replayStore.RegisterComponentType<TComponent>();
|
||||||
_upToDateComponentStore.RegisterComponentType<TComponent>();
|
upToDateComponentStore.RegisterComponentType<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetExistingComponentStore(ComponentStore componentStore)
|
internal void SetExistingComponentStore(ComponentStore componentStore)
|
||||||
{
|
{
|
||||||
_existingComponentStore.SwapWith(componentStore);
|
existingComponentStore.SwapWith(componentStore);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetUpToDateComponentStore(ComponentStore componentStore)
|
internal void SetUpToDateComponentStore(ComponentStore componentStore)
|
||||||
{
|
{
|
||||||
_upToDateComponentStore.SwapWith(componentStore);
|
upToDateComponentStore.SwapWith(componentStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void RegisterDrawableComponent<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
drawLayerManager.RegisterComponentWithLayer(entityID, component, layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void WriteComponents()
|
internal void WriteComponents()
|
||||||
{
|
{
|
||||||
_existingComponentStore.UpdateUsing(_replayStore);
|
existingComponentStore.UpdateUsing(replayStore);
|
||||||
_existingComponentStore.ClearAllPriorities();
|
existingComponentStore.ClearAllPriorities();
|
||||||
_upToDateComponentStore.ClearAllPriorities();
|
upToDateComponentStore.ClearAllPriorities();
|
||||||
_immediateComponentStore.ClearAll();
|
immediateComponentStore.ClearAll();
|
||||||
_replayStore.ClearAll();
|
replayStore.ClearAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool AddImmediateComponent<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
|
internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (_immediateComponentStore.Set(entityID, component, priority))
|
if (immediateComponentStore.Set(entityID, component, priority))
|
||||||
{
|
{
|
||||||
_replayStore.Set(entityID, component);
|
replayStore.Set(entityID, component);
|
||||||
_upToDateComponentStore.Set(entityID, component);
|
upToDateComponentStore.Set(entityID, component);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddImmediateComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
|
internal void AddImmediateComponent<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
_immediateComponentStore.Set(entityID, component);
|
immediateComponentStore.Set(entityID, component);
|
||||||
_replayStore.Set(entityID, component);
|
replayStore.Set(entityID, component);
|
||||||
_upToDateComponentStore.Set(entityID, component);
|
upToDateComponentStore.Set(entityID, component);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool UpdateComponent<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
|
internal bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var result = _upToDateComponentStore.Set(entityID, component, priority);
|
var result = upToDateComponentStore.Set(entityID, component, priority);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
_replayStore.Set(entityID, component);
|
replayStore.Set(entityID, component);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
|
internal void AddComponent<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
_upToDateComponentStore.Set(entityID, component);
|
upToDateComponentStore.Set(entityID, component);
|
||||||
_replayStore.Set(entityID, component);
|
replayStore.Set(entityID, component);
|
||||||
}
|
}
|
||||||
|
|
||||||
// existing or immediate reads
|
// existing or immediate reads
|
||||||
|
|
||||||
internal ReadOnlySpan<TComponent> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct
|
internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _upToDateComponentStore.All<TComponent>();
|
return upToDateComponentStore.All<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref readonly TComponent ExistingOrImmediateSingular<TComponent>() where TComponent : struct
|
internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _upToDateComponentStore.Singular<TComponent>();
|
if (!SomeExistingOrImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
||||||
|
var enumerator = ReadExistingAndImmediateComponentsByType<TComponent>().GetEnumerator();
|
||||||
|
enumerator.MoveNext();
|
||||||
|
return enumerator.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ReadOnlySpan<Entity> GetExistingAndImmediateEntities<TComponent>() where TComponent : struct
|
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _upToDateComponentStore.AllEntities<TComponent>();
|
return upToDateComponentStore.Any<TComponent>();
|
||||||
}
|
|
||||||
|
|
||||||
internal ref readonly Entity ExistingOrImmediateSingularEntity<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return ref _upToDateComponentStore.SingularEntity<TComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return _upToDateComponentStore.Any<TComponent>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// existing reads
|
// existing reads
|
||||||
|
|
||||||
internal ReadOnlySpan<TComponent> GetExistingComponents<TComponent>() where TComponent : struct
|
internal (TComponent, int) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _existingComponentStore.All<TComponent>();
|
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
||||||
|
var enumerator = GetComponentsIncludingEntity<TComponent>().GetEnumerator();
|
||||||
|
enumerator.MoveNext();
|
||||||
|
return enumerator.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref readonly TComponent ExistingSingular<TComponent>() where TComponent : struct
|
internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _existingComponentStore.Singular<TComponent>();
|
return existingComponentStore.Any<TComponent>();
|
||||||
}
|
|
||||||
|
|
||||||
internal ReadOnlySpan<Entity> GetExistingEntities<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return _existingComponentStore.AllEntities<TComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal IEnumerable<Entity> GetExistingEntitiesAsEnumerable<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return _existingComponentStore.AllEntitiesAsEnumerable<TComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal ref readonly Entity ExistingSingularEntity<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return ref _existingComponentStore.SingularEntity<TComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal bool SomeExistingComponent<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return _existingComponentStore.Any<TComponent>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// immediate reads
|
// immediate reads
|
||||||
|
|
||||||
internal ReadOnlySpan<TComponent> ReadImmediateComponentsByType<TComponent>() where TComponent : struct
|
internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _immediateComponentStore.All<TComponent>();
|
return immediateComponentStore.All<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref readonly TComponent ImmediateSingular<TComponent>() where TComponent : struct
|
internal (TComponent, int) ReadFirstImmediateComponentByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _immediateComponentStore.Singular<TComponent>();
|
if (!SomeImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
|
||||||
|
var enumerator = ReadImmediateComponentsByType<TComponent>().GetEnumerator();
|
||||||
|
enumerator.MoveNext();
|
||||||
|
return enumerator.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ReadOnlySpan<Entity> GetImmediateEntities<TComponent>() where TComponent : struct
|
internal bool SomeImmediateComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _immediateComponentStore.AllEntities<TComponent>();
|
return immediateComponentStore.Any<TComponent>();
|
||||||
}
|
|
||||||
|
|
||||||
internal ref readonly Entity ImmediateSingularEntity<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return ref _immediateComponentStore.SingularEntity<TComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal bool SomeImmediateComponent<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
return _immediateComponentStore.Any<TComponent>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// component getters
|
// component getters
|
||||||
|
|
||||||
internal ref TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
|
internal TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _upToDateComponentStore.Get<TComponent>(entityID);
|
return upToDateComponentStore.Get<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
|
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _existingComponentStore.Get<TComponent>(entityID);
|
return existingComponentStore.Get<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
|
internal TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _immediateComponentStore.Get<TComponent>(entityID);
|
return immediateComponentStore.Get<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// has checkers
|
// has checkers
|
||||||
|
|
||||||
internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : struct
|
internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _upToDateComponentStore.Has<TComponent>(entityID);
|
return upToDateComponentStore.Has<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool HasExistingOrImmediateComponent(int entityID, Type type)
|
internal bool HasExistingOrImmediateComponent(int entityID, Type type)
|
||||||
{
|
{
|
||||||
return _upToDateComponentStore.Has(type, entityID);
|
return upToDateComponentStore.Has(type, entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : struct
|
internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _existingComponentStore.Has<TComponent>(entityID);
|
return existingComponentStore.Has<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool HasExistingComponent(int entityID, Type type)
|
internal bool HasExistingComponent(int entityID, Type type)
|
||||||
{
|
{
|
||||||
return _existingComponentStore.Has(type, entityID);
|
return existingComponentStore.Has(type, entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : struct
|
internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _immediateComponentStore.Has<TComponent>(entityID);
|
return immediateComponentStore.Has<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool HasImmediateComponent(int entityID, Type type)
|
internal bool HasImmediateComponent(int entityID, Type type)
|
||||||
{
|
{
|
||||||
return _immediateComponentStore.Has(type, entityID);
|
return immediateComponentStore.Has(type, entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ReadOnlySpan<TComponent> GetComponentsByType<TComponent>() where TComponent : struct
|
internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _existingComponentStore.All<TComponent>();
|
return existingComponentStore.All<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IEnumerable<TComponent> GetComponentsByTypeEnumerable<TComponent>() where TComponent : struct
|
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _existingComponentStore.AllAsEnumerable<TComponent>();
|
foreach (var pair in existingComponentStore.All<TComponent>())
|
||||||
|
{
|
||||||
|
yield return pair.Item1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
|
internal TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _existingComponentStore.Get<TComponent>(entityID);
|
return existingComponentStore.Get<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : struct
|
internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _existingComponentStore.Has<TComponent>(entityID);
|
return existingComponentStore.Has<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void MarkAllComponentsOnEntityForRemoval(int entityID)
|
internal void MarkAllComponentsOnEntityForRemoval(int entityID)
|
||||||
{
|
{
|
||||||
_entitiesMarkedForRemoval.Add(entityID);
|
entitiesMarkedForRemoval.Add(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void RemoveMarkedComponents()
|
internal void RemoveMarkedComponents()
|
||||||
{
|
{
|
||||||
foreach (var entityID in _entitiesMarkedForRemoval)
|
foreach (var entityID in entitiesMarkedForRemoval)
|
||||||
{
|
{
|
||||||
_existingComponentStore.Remove(entityID);
|
existingComponentStore.Remove(entityID);
|
||||||
_immediateComponentStore.Remove(entityID);
|
immediateComponentStore.Remove(entityID);
|
||||||
_replayStore.Remove(entityID);
|
replayStore.Remove(entityID);
|
||||||
_upToDateComponentStore.Remove(entityID);
|
upToDateComponentStore.Remove(entityID);
|
||||||
|
drawLayerManager.UnRegisterEntityWithLayer(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
_entitiesMarkedForRemoval.Clear();
|
entitiesMarkedForRemoval.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : struct
|
public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (_immediateComponentStore.Remove<TComponent>(entityID, priority))
|
if (immediateComponentStore.Remove<TComponent>(entityID, priority))
|
||||||
{
|
{
|
||||||
_replayStore.Remove<TComponent>(entityID, priority);
|
replayStore.Remove<TComponent>(entityID, priority);
|
||||||
_upToDateComponentStore.Remove<TComponent>(entityID, priority);
|
upToDateComponentStore.Remove<TComponent>(entityID, priority);
|
||||||
|
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove<TComponent>(int entityID, int priority) where TComponent : struct
|
public void Remove<TComponent>(int entityID, int priority) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (_upToDateComponentStore.Remove<TComponent>(entityID, priority))
|
if (upToDateComponentStore.Remove<TComponent>(entityID, priority))
|
||||||
{
|
{
|
||||||
_replayStore.Remove<TComponent>(entityID, priority);
|
replayStore.Remove<TComponent>(entityID, priority);
|
||||||
|
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool UpToDateEntityIsEmpty(int entityID)
|
public bool UpToDateEntityIsEmpty(int entityID)
|
||||||
{
|
{
|
||||||
return _upToDateComponentStore.EntityBitArray(entityID).AllFalse();
|
return upToDateComponentStore.EntityBitArray(entityID).AllFalse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,118 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Encompass.Exceptions;
|
||||||
|
|
||||||
|
namespace Encompass
|
||||||
|
{
|
||||||
|
internal class DrawLayerManager
|
||||||
|
{
|
||||||
|
private readonly SortedList<int, int> layerOrder = new SortedList<int, int>();
|
||||||
|
|
||||||
|
private readonly Dictionary<int, ComponentStore> layerIndexToComponentStore = new Dictionary<int, ComponentStore>(512);
|
||||||
|
private readonly Dictionary<int, HashSet<GeneralRenderer>> layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(512);
|
||||||
|
|
||||||
|
private readonly Dictionary<Type, Dictionary<int, int>> typeToEntityToLayer = new Dictionary<Type, Dictionary<int, int>>(512);
|
||||||
|
private Dictionary<Type, int> typeToIndex;
|
||||||
|
public IEnumerable<int> LayerOrder { get { return layerOrder.Values; } }
|
||||||
|
|
||||||
|
public DrawLayerManager(Dictionary<Type, int> typeToIndex)
|
||||||
|
{
|
||||||
|
this.typeToIndex = typeToIndex;
|
||||||
|
RegisterDrawLayer(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterDrawLayer(int layer)
|
||||||
|
{
|
||||||
|
if (!layerIndexToComponentStore.ContainsKey(layer))
|
||||||
|
{
|
||||||
|
layerOrder.Add(layer, layer);
|
||||||
|
layerIndexToGeneralRenderers.Add(layer, new HashSet<GeneralRenderer>());
|
||||||
|
layerIndexToComponentStore.Add(layer, new ComponentStore(typeToIndex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterOrderedDrawable<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
if (!typeToEntityToLayer.ContainsKey(typeof(TComponent)))
|
||||||
|
{
|
||||||
|
typeToEntityToLayer.Add(typeof(TComponent), new Dictionary<int, int>(128));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var pair in layerIndexToComponentStore)
|
||||||
|
{
|
||||||
|
pair.Value.RegisterComponentType<TComponent>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
|
||||||
|
{
|
||||||
|
RegisterDrawLayer(layer);
|
||||||
|
var set = layerIndexToGeneralRenderers[layer];
|
||||||
|
set.Add(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnregisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
|
||||||
|
{
|
||||||
|
if (layerIndexToGeneralRenderers.ContainsKey(layer))
|
||||||
|
{
|
||||||
|
layerIndexToGeneralRenderers[layer].Remove(renderer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AdjustRendererLayer(GeneralRenderer renderer, int oldLayer, int newLayer)
|
||||||
|
{
|
||||||
|
UnregisterGeneralRendererWithLayer(renderer, oldLayer);
|
||||||
|
RegisterGeneralRendererWithLayer(renderer, newLayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterComponentWithLayer<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
if (!layerIndexToComponentStore.ContainsKey(layer))
|
||||||
|
{
|
||||||
|
throw new UndefinedLayerException("Layer {0} is not defined. Use WorldBuilder.RegisterDrawLayer to register the layer.", layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) { UnRegisterComponentWithLayer<TComponent>(entityID); }
|
||||||
|
|
||||||
|
var set = layerIndexToComponentStore[layer];
|
||||||
|
set.Set<TComponent>(entityID, component);
|
||||||
|
|
||||||
|
typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
if (!typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; }
|
||||||
|
|
||||||
|
if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
|
||||||
|
{
|
||||||
|
var layer = typeToEntityToLayer[typeof(TComponent)][entityID];
|
||||||
|
layerIndexToComponentStore[layer].ForceRemove<TComponent>(entityID);
|
||||||
|
}
|
||||||
|
typeToEntityToLayer[typeof(TComponent)].Remove(entityID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnRegisterEntityWithLayer(int entityID)
|
||||||
|
{
|
||||||
|
foreach (var store in layerIndexToComponentStore.Values)
|
||||||
|
{
|
||||||
|
store.Remove(entityID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
|
||||||
|
{
|
||||||
|
return layerIndexToGeneralRenderers.ContainsKey(layer) ?
|
||||||
|
layerIndexToGeneralRenderers[layer] :
|
||||||
|
Enumerable.Empty<GeneralRenderer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<(int, Type, IComponent)> AllInLayer(int layer)
|
||||||
|
{
|
||||||
|
return layerIndexToComponentStore.ContainsKey(layer) ?
|
||||||
|
layerIndexToComponentStore[layer].AllInterfaceTyped() :
|
||||||
|
Enumerable.Empty<(int, Type, IComponent)>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ using System.Reflection;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Encompass.Exceptions;
|
using Encompass.Exceptions;
|
||||||
|
using MoonTools.FastCollections;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
|
@ -13,121 +14,104 @@ namespace Encompass
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class Engine : IEquatable<Engine>
|
public abstract class Engine : IEquatable<Engine>
|
||||||
{
|
{
|
||||||
internal Guid _id;
|
internal Guid ID;
|
||||||
|
|
||||||
internal readonly HashSet<Type> ReadTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> readTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> ReadImmediateTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> readImmediateTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> SendTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> sendTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> ReceiveTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> receiveTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> WriteTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> writeTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> WriteImmediateTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> writeImmediateTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> QueryWithTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> queryWithTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> QueryWithoutTypes = new HashSet<Type>();
|
internal readonly HashSet<Type> queryWithoutTypes = new HashSet<Type>();
|
||||||
internal readonly HashSet<Type> AddTypes = new HashSet<Type>();
|
internal readonly Dictionary<Type, int> writePriorities = new Dictionary<Type, int>();
|
||||||
internal readonly Dictionary<Type, int> WritePriorities = new Dictionary<Type, int>();
|
internal readonly int defaultWritePriority = 0;
|
||||||
internal readonly int DefaultWritePriority = 0;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If false, the Engine will ignore time dilation.
|
/// If false, the Engine will ignore time dilation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal bool _usesTimeDilation = true;
|
internal bool usesTimeDilation = true;
|
||||||
public bool TimeDilationActive { get => _usesTimeDilation && _timeManager.TimeDilationActive; }
|
public bool TimeDilationActive { get => usesTimeDilation && timeManager.TimeDilationActive; }
|
||||||
|
|
||||||
private EntityManager _entityManager;
|
private EntityManager entityManager;
|
||||||
private MessageManager _messageManager;
|
private MessageManager messageManager;
|
||||||
private ComponentManager _componentManager;
|
private ComponentManager componentManager;
|
||||||
private TimeManager _timeManager;
|
private TimeManager timeManager;
|
||||||
private TrackingManager _trackingManager;
|
private TrackingManager trackingManager;
|
||||||
|
|
||||||
private EntitySetQuery _entityQuery;
|
private EntitySetQuery entityQuery;
|
||||||
|
|
||||||
private readonly HashSet<int> _trackedEntities = new HashSet<int>();
|
private HashSet<int> _trackedEntities = new HashSet<int>();
|
||||||
protected IEnumerable<Entity> TrackedEntities
|
protected IEnumerable<Entity> TrackedEntities
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
foreach (var entityID in _trackedEntities)
|
foreach (var entityID in _trackedEntities)
|
||||||
{
|
{
|
||||||
yield return _entityManager.GetEntity(entityID);
|
yield return entityManager.GetEntity(entityID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly HashSet<int> _newlyCreatedEntities = new HashSet<int>();
|
private HashSet<int> _newlyCreatedEntities = new HashSet<int>();
|
||||||
|
|
||||||
protected Engine()
|
protected Engine()
|
||||||
{
|
{
|
||||||
_id = Guid.NewGuid();
|
ID = Guid.NewGuid();
|
||||||
|
|
||||||
var addsAttribute = GetType().GetCustomAttribute<Adds>(false);
|
|
||||||
if (addsAttribute != null)
|
|
||||||
{
|
|
||||||
AddTypes = addsAttribute.addTypes;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
|
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
|
||||||
if (sendsAttribute != null)
|
if (sendsAttribute != null)
|
||||||
{
|
{
|
||||||
SendTypes = sendsAttribute.SendTypes;
|
sendTypes = sendsAttribute.sendTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
var activatesAttribute = GetType().GetCustomAttribute<WritesImmediate>(false);
|
var activatesAttribute = GetType().GetCustomAttribute<WritesImmediate>(false);
|
||||||
if (activatesAttribute != null)
|
if (activatesAttribute != null)
|
||||||
{
|
{
|
||||||
WriteImmediateTypes = activatesAttribute.WriteImmediateTypes;
|
writeImmediateTypes = activatesAttribute.writeImmediateTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultWritePriorityAttribute = GetType().GetCustomAttribute<DefaultWritePriority>(false);
|
var defaultWritePriorityAttribute = GetType().GetCustomAttribute<DefaultWritePriority>(false);
|
||||||
|
|
||||||
if (defaultWritePriorityAttribute != null)
|
if (defaultWritePriorityAttribute != null)
|
||||||
{
|
{
|
||||||
DefaultWritePriority = defaultWritePriorityAttribute.WritePriority;
|
defaultWritePriority = defaultWritePriorityAttribute.writePriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var writesAttribute in GetType().GetCustomAttributes<Writes>(false))
|
foreach (var writesAttribute in GetType().GetCustomAttributes<Writes>(false))
|
||||||
{
|
{
|
||||||
WriteTypes.UnionWith(writesAttribute.WriteTypes);
|
writeTypes.UnionWith(writesAttribute.writeTypes);
|
||||||
WritePriorities = new Dictionary<Type, int>[2] { WritePriorities, writesAttribute.Priorities }.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value);
|
writePriorities = new Dictionary<Type, int>[2] { writePriorities, writesAttribute.priorities }.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
var receivesAttribute = GetType().GetCustomAttribute<Receives>(false);
|
var receivesAttribute = GetType().GetCustomAttribute<Receives>(false);
|
||||||
if (receivesAttribute != null)
|
if (receivesAttribute != null)
|
||||||
{
|
{
|
||||||
ReceiveTypes = receivesAttribute.ReceiveTypes;
|
receiveTypes = receivesAttribute.receiveTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
||||||
if (readsAttribute != null)
|
if (readsAttribute != null)
|
||||||
{
|
{
|
||||||
ReadTypes = readsAttribute.ReadTypes;
|
readTypes = readsAttribute.readTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
var readsImmediateAttribute = GetType().GetCustomAttribute<ReadsImmediate>(false);
|
var readsImmediateAttribute = GetType().GetCustomAttribute<ReadsImmediate>(false);
|
||||||
if (readsImmediateAttribute != null)
|
if (readsImmediateAttribute != null)
|
||||||
{
|
{
|
||||||
ReadImmediateTypes = readsImmediateAttribute.ReadImmediateTypes;
|
readImmediateTypes = readsImmediateAttribute.readImmediateTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
var queryWithAttribute = GetType().GetCustomAttribute<QueryWith>(false);
|
var queryWithAttribute = GetType().GetCustomAttribute<QueryWith>(false);
|
||||||
if (queryWithAttribute != null)
|
if (queryWithAttribute != null)
|
||||||
{
|
{
|
||||||
QueryWithTypes = queryWithAttribute.QueryWithTypes;
|
queryWithTypes = queryWithAttribute.queryWithTypes;
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var queryType in QueryWithTypes)
|
|
||||||
{
|
|
||||||
ReadTypes.Add(queryType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var queryWithoutAttribute = GetType().GetCustomAttribute<QueryWithout>(false);
|
var queryWithoutAttribute = GetType().GetCustomAttribute<QueryWithout>(false);
|
||||||
if (queryWithoutAttribute != null)
|
if (queryWithoutAttribute != null)
|
||||||
{
|
{
|
||||||
QueryWithoutTypes = queryWithoutAttribute.QueryWithoutTypes;
|
queryWithoutTypes = queryWithoutAttribute.queryWithoutTypes;
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var queryType in QueryWithoutTypes)
|
|
||||||
{
|
|
||||||
ReadTypes.Add(queryType);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,42 +127,42 @@ namespace Encompass
|
||||||
|
|
||||||
public bool Equals(Engine other)
|
public bool Equals(Engine other)
|
||||||
{
|
{
|
||||||
return other._id == _id;
|
return other.ID == ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
return HashCode.Combine(_id);
|
return ID.GetHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AssignEntityManager(EntityManager entityManager)
|
internal void AssignEntityManager(EntityManager entityManager)
|
||||||
{
|
{
|
||||||
_entityManager = entityManager;
|
this.entityManager = entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AssignComponentManager(ComponentManager componentManager)
|
internal void AssignComponentManager(ComponentManager componentManager)
|
||||||
{
|
{
|
||||||
_componentManager = componentManager;
|
this.componentManager = componentManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AssignMessageManager(MessageManager messageManager)
|
internal void AssignMessageManager(MessageManager messageManager)
|
||||||
{
|
{
|
||||||
_messageManager = messageManager;
|
this.messageManager = messageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AssignTimeManager(TimeManager timeManager)
|
internal void AssignTimeManager(TimeManager timeManager)
|
||||||
{
|
{
|
||||||
_timeManager = timeManager;
|
this.timeManager = timeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AssignTrackingManager(TrackingManager trackingManager)
|
internal void AssignTrackingManager(TrackingManager trackingManager)
|
||||||
{
|
{
|
||||||
_trackingManager = trackingManager;
|
this.trackingManager = trackingManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void CheckMessageRead<TMessage>() where TMessage : struct
|
internal void CheckMessageRead<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
if (!ReceiveTypes.Contains(typeof(TMessage)))
|
if (!receiveTypes.Contains(typeof(TMessage)))
|
||||||
{
|
{
|
||||||
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
||||||
}
|
}
|
||||||
|
@ -205,7 +189,7 @@ namespace Encompass
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Entity CreateEntity()
|
protected Entity CreateEntity()
|
||||||
{
|
{
|
||||||
var entity = _entityManager.CreateEntity();
|
var entity = entityManager.CreateEntity();
|
||||||
_newlyCreatedEntities.Add(entity.ID);
|
_newlyCreatedEntities.Add(entity.ID);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
@ -213,9 +197,9 @@ namespace Encompass
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if an Entity with the specified ID exists.
|
/// Returns true if an Entity with the specified ID exists.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected bool EntityExists(in Entity entity)
|
protected bool EntityExists(Entity entity)
|
||||||
{
|
{
|
||||||
return _entityManager.EntityExists(entity.ID);
|
return entityManager.EntityExists(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -223,52 +207,50 @@ namespace Encompass
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected bool EntityExists(int entityID)
|
protected bool EntityExists(int entityID)
|
||||||
{
|
{
|
||||||
return _entityManager.EntityExists(entityID);
|
return entityManager.EntityExists(entityID);
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns all Entities containing the specified Component type.
|
|
||||||
/// </summary>
|
|
||||||
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
|
||||||
if (existingRead && immediateRead)
|
|
||||||
{
|
|
||||||
return _componentManager.GetExistingAndImmediateEntities<TComponent>();
|
|
||||||
}
|
|
||||||
else if (existingRead)
|
|
||||||
{
|
|
||||||
return _componentManager.GetExistingEntities<TComponent>();
|
|
||||||
}
|
|
||||||
else if (immediateRead)
|
|
||||||
{
|
|
||||||
return _componentManager.GetImmediateEntities<TComponent>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns an Entity containing the specified Component type.
|
/// Returns an Entity containing the specified Component type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
|
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
return entityManager.GetEntity(ReadComponentHelper<TComponent>().Item2);
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns all Entities containing the specified Component type.
|
||||||
|
/// </summary>
|
||||||
|
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
foreach (var pair in ReadComponentsHelper<TComponent>())
|
||||||
|
{
|
||||||
|
yield return entityManager.GetEntity(pair.Item2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// these next two are for the ComponentMessageEmitter only
|
||||||
|
|
||||||
|
internal IEnumerable<TComponent> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
return componentManager.GetComponentsByType<TComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<(TComponent, int)> ReadComponentsHelper<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
||||||
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
||||||
if (existingRead && immediateRead)
|
if (existingRead && immediateRead)
|
||||||
{
|
{
|
||||||
return ref _componentManager.ExistingOrImmediateSingularEntity<TComponent>();
|
return componentManager.ReadExistingAndImmediateComponentsByType<TComponent>();
|
||||||
}
|
}
|
||||||
else if (existingRead)
|
else if (existingRead)
|
||||||
{
|
{
|
||||||
return ref _componentManager.ExistingSingularEntity<TComponent>();
|
return componentManager.GetComponentsIncludingEntity<TComponent>();
|
||||||
}
|
}
|
||||||
else if (immediateRead)
|
else if (immediateRead)
|
||||||
{
|
{
|
||||||
return ref _componentManager.ImmediateSingularEntity<TComponent>();
|
return componentManager.ReadImmediateComponentsByType<TComponent>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -279,21 +261,40 @@ namespace Encompass
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns all of the Components with the specified Component Type.
|
/// Returns all of the Components with the specified Component Type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
|
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
foreach (var pair in ReadComponentsHelper<TComponent>())
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
{
|
||||||
|
yield return pair.Item1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns all of the components of the specified type including an Entity reference for each Component.
|
||||||
|
/// </summary>
|
||||||
|
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
foreach (var (component, id) in ReadComponentsHelper<TComponent>())
|
||||||
|
{
|
||||||
|
yield return (component, entityManager.GetEntity(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private (TComponent, int) ReadComponentHelper<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
||||||
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
||||||
if (existingRead && immediateRead)
|
if (existingRead && immediateRead)
|
||||||
{
|
{
|
||||||
return _componentManager.ReadExistingAndImmediateComponentsByType<TComponent>();
|
return componentManager.ReadFirstExistingOrImmediateComponentByType<TComponent>();
|
||||||
}
|
}
|
||||||
else if (existingRead)
|
else if (existingRead)
|
||||||
{
|
{
|
||||||
return _componentManager.GetExistingComponents<TComponent>();
|
return componentManager.ReadFirstExistingComponentByType<TComponent>();
|
||||||
}
|
}
|
||||||
else if (immediateRead)
|
else if (immediateRead)
|
||||||
{
|
{
|
||||||
return _componentManager.ReadImmediateComponentsByType<TComponent>();
|
return componentManager.ReadFirstImmediateComponentByType<TComponent>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -304,46 +305,38 @@ namespace Encompass
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
|
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
|
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
return ReadComponentHelper<TComponent>().Item1;
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
}
|
||||||
if (existingRead && immediateRead)
|
|
||||||
{
|
/// <summary>
|
||||||
return ref _componentManager.ExistingOrImmediateSingular<TComponent>();
|
/// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned.
|
||||||
}
|
/// </summary>
|
||||||
else if (existingRead)
|
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _componentManager.ExistingSingular<TComponent>();
|
var (component, id) = ReadComponentHelper<TComponent>();
|
||||||
}
|
return (component, entityManager.GetEntity(id));
|
||||||
else if (immediateRead)
|
|
||||||
{
|
|
||||||
return ref _componentManager.ImmediateSingular<TComponent>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if any Component with the specified Component Type exists.
|
/// Returns true if any Component with the specified Component Type exists.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected bool SomeComponent<TComponent>() where TComponent : struct
|
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
||||||
if (existingRead && immediateRead)
|
if (existingRead && immediateRead)
|
||||||
{
|
{
|
||||||
return _componentManager.SomeExistingOrImmediateComponent<TComponent>();
|
return componentManager.SomeExistingOrImmediateComponent<TComponent>();
|
||||||
}
|
}
|
||||||
else if (existingRead)
|
else if (existingRead)
|
||||||
{
|
{
|
||||||
return _componentManager.SomeExistingComponent<TComponent>();
|
return componentManager.SomeExistingComponent<TComponent>();
|
||||||
}
|
}
|
||||||
else if (immediateRead)
|
else if (immediateRead)
|
||||||
{
|
{
|
||||||
return _componentManager.SomeImmediateComponent<TComponent>();
|
return componentManager.SomeImmediateComponent<TComponent>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -351,21 +344,21 @@ namespace Encompass
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct
|
private TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
||||||
if (existingRead && immediateRead)
|
if (existingRead && immediateRead)
|
||||||
{
|
{
|
||||||
return ref _componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entityID);
|
return componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
else if (existingRead)
|
else if (existingRead)
|
||||||
{
|
{
|
||||||
return ref _componentManager.ReadExistingComponentByEntityAndType<TComponent>(entityID);
|
return componentManager.ReadExistingComponentByEntityAndType<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
else if (immediateRead)
|
else if (immediateRead)
|
||||||
{
|
{
|
||||||
return ref _componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entityID);
|
return componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entityID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -382,9 +375,9 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
||||||
/// Thrown when the Engine does not declare that it reads the given Component Type.
|
/// Thrown when the Engine does not declare that it reads the given Component Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected ref readonly TComponent GetComponent<TComponent>(in Entity entity) where TComponent : struct
|
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref GetComponentHelper<TComponent>(entity.ID);
|
return GetComponentHelper<TComponent>(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -393,22 +386,22 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
||||||
/// Thrown when the Engine does not declare that is Reads the given Component Type.
|
/// Thrown when the Engine does not declare that is Reads the given Component Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected bool HasComponent<TComponent>(in Entity entity) where TComponent : struct
|
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
||||||
var existingRead = ReadTypes.Contains(typeof(TComponent));
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
||||||
|
|
||||||
if (immediateRead && existingRead)
|
if (immediateRead && existingRead)
|
||||||
{
|
{
|
||||||
return _componentManager.HasExistingOrImmediateComponent<TComponent>(entity.ID);
|
return componentManager.HasExistingOrImmediateComponent<TComponent>(entity.ID);
|
||||||
}
|
}
|
||||||
else if (existingRead)
|
else if (existingRead)
|
||||||
{
|
{
|
||||||
return _componentManager.HasExistingComponent<TComponent>(entity.ID);
|
return componentManager.HasExistingComponent<TComponent>(entity.ID);
|
||||||
}
|
}
|
||||||
else if (immediateRead)
|
else if (immediateRead)
|
||||||
{
|
{
|
||||||
return _componentManager.HasImmediateComponent<TComponent>(entity.ID);
|
return componentManager.HasImmediateComponent<TComponent>(entity.ID);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -422,22 +415,22 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
||||||
/// Thrown when the Engine does not declare that is Reads the given Component Type.
|
/// Thrown when the Engine does not declare that is Reads the given Component Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected bool HasComponent(in Entity entity, Type type)
|
protected bool HasComponent(Entity entity, Type type)
|
||||||
{
|
{
|
||||||
var immediateRead = ReadImmediateTypes.Contains(type);
|
var immediateRead = readImmediateTypes.Contains(type);
|
||||||
var existingRead = ReadTypes.Contains(type);
|
var existingRead = readTypes.Contains(type);
|
||||||
|
|
||||||
if (immediateRead && existingRead)
|
if (immediateRead && existingRead)
|
||||||
{
|
{
|
||||||
return _componentManager.HasExistingOrImmediateComponent(entity.ID, type);
|
return componentManager.HasExistingOrImmediateComponent(entity.ID, type);
|
||||||
}
|
}
|
||||||
else if (existingRead)
|
else if (existingRead)
|
||||||
{
|
{
|
||||||
return _componentManager.HasExistingComponent(entity.ID, type);
|
return componentManager.HasExistingComponent(entity.ID, type);
|
||||||
}
|
}
|
||||||
else if (immediateRead)
|
else if (immediateRead)
|
||||||
{
|
{
|
||||||
return _componentManager.HasImmediateComponent(entity.ID, type);
|
return componentManager.HasImmediateComponent(entity.ID, type);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -451,32 +444,37 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
|
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
|
||||||
/// Thrown when the Engine does not declare that it Writes the given Component Type.
|
/// Thrown when the Engine does not declare that it Writes the given Component Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected void SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
|
protected void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
|
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority;
|
||||||
|
|
||||||
if (!WriteTypes.Contains(typeof(TComponent)))
|
if (!writeTypes.Contains(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool written;
|
bool written;
|
||||||
if (WriteImmediateTypes.Contains(typeof(TComponent)))
|
if (writeImmediateTypes.Contains(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
written = _componentManager.AddImmediateComponent(entity.ID, component, priority);
|
written = componentManager.AddImmediateComponent(entity.ID, component, priority);
|
||||||
if (written)
|
if (written)
|
||||||
{
|
{
|
||||||
_trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
written = _componentManager.UpdateComponent(entity.ID, component, priority);
|
written = componentManager.UpdateComponent(entity.ID, component, priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_componentManager.HasExistingComponent<TComponent>(entity.ID))
|
if (!componentManager.HasExistingComponent<TComponent>(entity.ID))
|
||||||
{
|
{
|
||||||
_trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
|
trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (written && component is IDrawableComponent drawableComponent)
|
||||||
|
{
|
||||||
|
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -486,29 +484,29 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
|
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
|
||||||
/// Thrown when the Engine does not declare that it Writes the given Component Type.
|
/// Thrown when the Engine does not declare that it Writes the given Component Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected void AddComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct
|
protected void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
if (!AddTypes.Contains(typeof(TComponent)))
|
|
||||||
{
|
|
||||||
throw new IllegalWriteException("Engine {0} tried to add undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!EntityCreatedThisFrame(entity.ID))
|
if (!EntityCreatedThisFrame(entity.ID))
|
||||||
{
|
{
|
||||||
throw new IllegalWriteException("AddComponent used on Entity that was not created in this context. Use SetComponent instead.");
|
throw new IllegalWriteException("AddComponent used on Entity that was not created in this context. Use SetComponent instead.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (WriteImmediateTypes.Contains(typeof(TComponent)))
|
if (writeImmediateTypes.Contains(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
_componentManager.AddImmediateComponent(entity.ID, component);
|
componentManager.AddImmediateComponent(entity.ID, component);
|
||||||
_trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_componentManager.AddComponent(entity.ID, component);
|
componentManager.AddComponent(entity.ID, component);
|
||||||
}
|
}
|
||||||
|
|
||||||
_trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
|
trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
|
||||||
|
|
||||||
|
if (component is IDrawableComponent drawableComponent)
|
||||||
|
{
|
||||||
|
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -517,32 +515,32 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalSendException">
|
/// <exception cref="Encompass.Exceptions.IllegalSendException">
|
||||||
/// Thrown when the Engine does not declare that it Sends the Message Type.
|
/// Thrown when the Engine does not declare that it Sends the Message Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected void SendMessage<TMessage>(in TMessage message) where TMessage : struct
|
protected void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
if (!SendTypes.Contains(typeof(TMessage)))
|
if (!sendTypes.Contains(typeof(TMessage)))
|
||||||
{
|
{
|
||||||
throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
|
throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
_messageManager.AddMessage(message);
|
messageManager.AddMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a message after the specified number of seconds, respecting time dilation.
|
/// Sends a message after the specified number of seconds, respecting time dilation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
||||||
protected void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct
|
protected void SendMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageManager.AddMessage(message, time);
|
messageManager.AddMessage(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a message after the specified number of seconds, ignoring time dilation.
|
/// Sends a message after the specified number of seconds, ignoring time dilation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
||||||
protected void SendMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
|
protected void SendMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageManager.AddMessageIgnoringTimeDilation(message, time);
|
messageManager.AddMessageIgnoringTimeDilation(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -551,10 +549,10 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
||||||
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct
|
protected IEnumerable<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
CheckMessageRead<TMessage>();
|
CheckMessageRead<TMessage>();
|
||||||
return _messageManager.GetMessagesByType<TMessage>();
|
return messageManager.GetMessagesByType<TMessage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -563,10 +561,10 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
||||||
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected ref readonly TMessage ReadMessage<TMessage>() where TMessage : struct
|
protected TMessage ReadMessage<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
CheckMessageRead<TMessage>();
|
CheckMessageRead<TMessage>();
|
||||||
return ref _messageManager.First<TMessage>();
|
return messageManager.First<TMessage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -575,26 +573,26 @@ namespace Encompass
|
||||||
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
||||||
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected bool SomeMessage<TMessage>() where TMessage : struct
|
protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
CheckMessageRead<TMessage>();
|
CheckMessageRead<TMessage>();
|
||||||
return _messageManager.Any<TMessage>();
|
return messageManager.Any<TMessage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Destroys the specified Entity. This also removes all of the Components associated with the Entity.
|
/// Destroys the specified Entity. This also removes all of the Components associated with the Entity.
|
||||||
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void Destroy(in Entity entity)
|
protected void Destroy(Entity entity)
|
||||||
{
|
{
|
||||||
_entityManager.MarkForDestroy(entity.ID);
|
entityManager.MarkForDestroy(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Destroys an arbitrary Entity containing a Component of the specified Type.
|
/// Destroys an arbitrary Entity containing a Component of the specified Type.
|
||||||
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void DestroyWith<TComponent>() where TComponent : struct
|
protected void DestroyWith<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
Destroy(ReadEntity<TComponent>());
|
Destroy(ReadEntity<TComponent>());
|
||||||
}
|
}
|
||||||
|
@ -603,7 +601,7 @@ namespace Encompass
|
||||||
/// Destroys all Entities containing a Component of the specified Type.
|
/// Destroys all Entities containing a Component of the specified Type.
|
||||||
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void DestroyAllWith<TComponent>() where TComponent : struct
|
protected void DestroyAllWith<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
foreach (var entity in ReadEntities<TComponent>())
|
foreach (var entity in ReadEntities<TComponent>())
|
||||||
{
|
{
|
||||||
|
@ -616,30 +614,30 @@ namespace Encompass
|
||||||
/// Note that the Engine must Read the Component type that is being removed.
|
/// Note that the Engine must Read the Component type that is being removed.
|
||||||
/// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
|
/// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void RemoveComponent<TComponent>(in Entity entity) where TComponent : struct
|
protected void RemoveComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
|
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority;
|
||||||
|
|
||||||
if (!WriteTypes.Contains(typeof(TComponent)))
|
if (!writeTypes.Contains(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
throw new IllegalWriteException("Engine {0} tried to remove undeclared Component {1}. Declare with Writes attribute.", GetType().Name, typeof(TComponent).Name);
|
throw new IllegalWriteException("Engine {0} tried to remove undeclared Component {1}. Declare with Writes attribute.", GetType().Name, typeof(TComponent).Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (WriteImmediateTypes.Contains(typeof(TComponent)))
|
if (writeImmediateTypes.Contains(typeof(TComponent)))
|
||||||
{
|
{
|
||||||
if (_componentManager.RemoveImmediate<TComponent>(entity.ID, priority))
|
if (componentManager.RemoveImmediate<TComponent>(entity.ID, priority))
|
||||||
{
|
{
|
||||||
_trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_componentManager.Remove<TComponent>(entity.ID, priority);
|
componentManager.Remove<TComponent>(entity.ID, priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_componentManager.HasExistingComponent<TComponent>(entity.ID))
|
if (componentManager.HasExistingComponent<TComponent>(entity.ID))
|
||||||
{
|
{
|
||||||
_trackingManager.RegisterRemoval(entity.ID, typeof(TComponent));
|
trackingManager.RegisterRemoval(entity.ID, typeof(TComponent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -654,7 +652,7 @@ namespace Encompass
|
||||||
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
||||||
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime)
|
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime)
|
||||||
{
|
{
|
||||||
_timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime);
|
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -669,7 +667,7 @@ namespace Encompass
|
||||||
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
||||||
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime)
|
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime)
|
||||||
{
|
{
|
||||||
_timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime);
|
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -684,7 +682,7 @@ namespace Encompass
|
||||||
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
|
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
|
||||||
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
||||||
{
|
{
|
||||||
_timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime, easeOutFunction);
|
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime, easeOutFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -700,7 +698,7 @@ namespace Encompass
|
||||||
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
|
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
|
||||||
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
||||||
{
|
{
|
||||||
_timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
|
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -709,38 +707,38 @@ namespace Encompass
|
||||||
/// <typeparam name="TMessage">The Message subtype.</typeparam>
|
/// <typeparam name="TMessage">The Message subtype.</typeparam>
|
||||||
/// <param name="entity">The entity that all messages in the IEnumerable refer to.</param>
|
/// <param name="entity">The entity that all messages in the IEnumerable refer to.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
|
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
CheckMessageRead<TMessage>();
|
CheckMessageRead<TMessage>();
|
||||||
return _messageManager.WithEntity<TMessage>(entity.ID);
|
return messageManager.WithEntity<TMessage>(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Efficiently reads a single Message of a given type that references a given Entity.
|
/// Efficiently reads a single Message of a given type that references a given Entity.
|
||||||
/// It is recommended to use this method in conjunction with SomeMessageWithEntity to prevent errors.
|
/// It is recommended to use this method in conjunction with SomeMessageWithEntity to prevent errors.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
|
protected TMessage ReadMessageWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
CheckMessageRead<TMessage>();
|
CheckMessageRead<TMessage>();
|
||||||
return ref _messageManager.WithEntitySingular<TMessage>(entity.ID);
|
return messageManager.WithEntitySingular<TMessage>(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Efficiently checks if any Message of a given type referencing a given Entity exists.
|
/// Efficiently checks if any Message of a given type referencing a given Entity exists.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IHasEntity
|
protected bool SomeMessageWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
CheckMessageRead<TMessage>();
|
CheckMessageRead<TMessage>();
|
||||||
return _messageManager.SomeWithEntity<TMessage>(entity.ID);
|
return messageManager.SomeWithEntity<TMessage>(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void CheckAndUpdateTracking(int entityID)
|
internal void CheckAndUpdateTracking(int entityID)
|
||||||
{
|
{
|
||||||
if (_trackedEntities.Contains(entityID) && !_entityQuery.CheckEntity(entityID, _componentManager.ExistingBits))
|
if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
|
||||||
{
|
{
|
||||||
_trackedEntities.Remove(entityID);
|
_trackedEntities.Remove(entityID);
|
||||||
}
|
}
|
||||||
else if (!_trackedEntities.Contains(entityID) && _entityQuery.CheckEntity(entityID, _componentManager.ExistingBits))
|
else if (!_trackedEntities.Contains(entityID) && entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
|
||||||
{
|
{
|
||||||
_trackedEntities.Add(entityID);
|
_trackedEntities.Add(entityID);
|
||||||
}
|
}
|
||||||
|
@ -748,11 +746,11 @@ namespace Encompass
|
||||||
|
|
||||||
internal void ImmediateCheckAndUpdateTracking(int entityID)
|
internal void ImmediateCheckAndUpdateTracking(int entityID)
|
||||||
{
|
{
|
||||||
if (_trackedEntities.Contains(entityID) && !_entityQuery.ImmediateCheckEntity(entityID, _componentManager.ImmediateBits, _componentManager.ExistingBits))
|
if (_trackedEntities.Contains(entityID) && !entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
|
||||||
{
|
{
|
||||||
_trackedEntities.Remove(entityID);
|
_trackedEntities.Remove(entityID);
|
||||||
}
|
}
|
||||||
else if (!_trackedEntities.Contains(entityID) && _entityQuery.ImmediateCheckEntity(entityID, _componentManager.ImmediateBits, _componentManager.ExistingBits))
|
else if (!_trackedEntities.Contains(entityID) && entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
|
||||||
{
|
{
|
||||||
_trackedEntities.Add(entityID);
|
_trackedEntities.Add(entityID);
|
||||||
}
|
}
|
||||||
|
@ -764,34 +762,30 @@ namespace Encompass
|
||||||
internal void BuildEntityQuery()
|
internal void BuildEntityQuery()
|
||||||
{
|
{
|
||||||
var withMask = BitSet512.Zero;
|
var withMask = BitSet512.Zero;
|
||||||
foreach (var type in QueryWithTypes)
|
foreach (var type in queryWithTypes)
|
||||||
{
|
{
|
||||||
if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
|
withMask = withMask.Set(componentManager.TypeToIndex[type]);
|
||||||
withMask = withMask.Set(_componentManager.TypeToIndex[type]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var withoutMask = BitSet512.Zero;
|
var withoutMask = BitSet512.Zero;
|
||||||
foreach (var type in QueryWithoutTypes)
|
foreach (var type in queryWithoutTypes)
|
||||||
{
|
{
|
||||||
if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
|
withoutMask = withoutMask.Set(componentManager.TypeToIndex[type]);
|
||||||
withoutMask = withoutMask.Set(_componentManager.TypeToIndex[type]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var immediateMask = BitSet512.Zero;
|
var immediateMask = BitSet512.Zero;
|
||||||
foreach (var type in ReadImmediateTypes)
|
foreach (var type in readImmediateTypes)
|
||||||
{
|
{
|
||||||
if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
|
immediateMask = immediateMask.Set(componentManager.TypeToIndex[type]);
|
||||||
immediateMask = immediateMask.Set(_componentManager.TypeToIndex[type]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var existingMask = BitSet512.Zero;
|
var existingMask = BitSet512.Zero;
|
||||||
foreach (var type in ReadTypes)
|
foreach (var type in readTypes)
|
||||||
{
|
{
|
||||||
if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
|
existingMask = existingMask.Set(componentManager.TypeToIndex[type]);
|
||||||
existingMask = existingMask.Set(_componentManager.TypeToIndex[type]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_entityQuery = new EntitySetQuery(
|
entityQuery = new EntitySetQuery(
|
||||||
withMask & immediateMask,
|
withMask & immediateMask,
|
||||||
withMask & existingMask,
|
withMask & existingMask,
|
||||||
withoutMask & immediateMask,
|
withoutMask & immediateMask,
|
||||||
|
|
|
@ -6,27 +6,27 @@ namespace Encompass
|
||||||
/// A Spawner is a special type of Engine that runs a Spawn method in response to each Message it receives.
|
/// A Spawner is a special type of Engine that runs a Spawn method in response to each Message it receives.
|
||||||
/// Spawners are useful for organizing the building of new Entities in your game.
|
/// Spawners are useful for organizing the building of new Entities in your game.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class Spawner<TMessage> : Engine where TMessage : struct
|
public abstract class Spawner<TMessage> : Engine where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
protected Spawner() : base()
|
protected Spawner() : base()
|
||||||
{
|
{
|
||||||
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
||||||
if (readsAttribute != null)
|
if (readsAttribute != null)
|
||||||
{
|
{
|
||||||
readsAttribute.ReadTypes.Add(typeof(TMessage));
|
readsAttribute.readTypes.Add(typeof(TMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
ReceiveTypes.Add(typeof(TMessage));
|
receiveTypes.Add(typeof(TMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var message in ReadMessages<TMessage>())
|
foreach (var message in ReadMessages<TMessage>())
|
||||||
{
|
{
|
||||||
Spawn(message);
|
Spawn(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void Spawn(in TMessage message);
|
protected abstract void Spawn(TMessage message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ namespace Encompass
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
return HashCode.Combine(ID);
|
return ID.GetHashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,48 +5,48 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
internal class EntityManager
|
internal class EntityManager
|
||||||
{
|
{
|
||||||
private readonly int _entityCapacity;
|
private readonly int entityCapacity;
|
||||||
private readonly IDManager _idManager = new IDManager();
|
private readonly IDManager idManager = new IDManager();
|
||||||
private readonly HashSet<int> _ids = new HashSet<int>();
|
private readonly HashSet<int> IDs = new HashSet<int>();
|
||||||
|
|
||||||
private readonly HashSet<int> _entitiesMarkedForDestroy = new HashSet<int>();
|
private readonly HashSet<int> entitiesMarkedForDestroy = new HashSet<int>();
|
||||||
|
|
||||||
private readonly ComponentManager _componentManager;
|
private readonly ComponentManager componentManager;
|
||||||
|
|
||||||
public IEnumerable<int> EntityIDs
|
public IEnumerable<int> EntityIDs
|
||||||
{
|
{
|
||||||
get { return _ids; }
|
get { return IDs; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityManager(ComponentManager componentManager, int entityCapacity)
|
public EntityManager(ComponentManager componentManager, int entityCapacity)
|
||||||
{
|
{
|
||||||
_componentManager = componentManager;
|
this.componentManager = componentManager;
|
||||||
_entityCapacity = entityCapacity;
|
this.entityCapacity = entityCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int NextID()
|
private int NextID()
|
||||||
{
|
{
|
||||||
return _idManager.NextID();
|
return idManager.NextID();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity CreateEntity()
|
public Entity CreateEntity()
|
||||||
{
|
{
|
||||||
if (_ids.Count < _entityCapacity)
|
if (IDs.Count < entityCapacity)
|
||||||
{
|
{
|
||||||
var id = NextID();
|
var id = NextID();
|
||||||
var entity = new Entity(id);
|
var entity = new Entity(id);
|
||||||
_ids.Add(id);
|
IDs.Add(id);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", _entityCapacity);
|
throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", entityCapacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool EntityExists(int id)
|
public bool EntityExists(int id)
|
||||||
{
|
{
|
||||||
return _ids.Contains(id);
|
return IDs.Contains(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entity GetEntity(int id)
|
public Entity GetEntity(int id)
|
||||||
|
@ -61,20 +61,20 @@ namespace Encompass
|
||||||
|
|
||||||
public void MarkForDestroy(int entityID)
|
public void MarkForDestroy(int entityID)
|
||||||
{
|
{
|
||||||
_entitiesMarkedForDestroy.Add(entityID);
|
entitiesMarkedForDestroy.Add(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DestroyMarkedEntities(IEnumerable<Engine> engines)
|
public void DestroyMarkedEntities(IEnumerable<Engine> engines)
|
||||||
{
|
{
|
||||||
foreach (var entityID in _entitiesMarkedForDestroy)
|
foreach (var entityID in entitiesMarkedForDestroy)
|
||||||
{
|
{
|
||||||
foreach (var engine in engines) { engine.RegisterDestroyedEntity(entityID); }
|
foreach (var engine in engines) { engine.RegisterDestroyedEntity(entityID); }
|
||||||
_componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
|
componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
|
||||||
_ids.Remove(entityID);
|
IDs.Remove(entityID);
|
||||||
_idManager.Free(entityID);
|
idManager.Free(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
_entitiesMarkedForDestroy.Clear();
|
entitiesMarkedForDestroy.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: this is very suboptimal
|
// NOTE: this is very suboptimal
|
||||||
|
@ -82,7 +82,7 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
foreach (var id in EntityIDs)
|
foreach (var id in EntityIDs)
|
||||||
{
|
{
|
||||||
if (_componentManager.UpToDateEntityIsEmpty(id))
|
if (componentManager.UpToDateEntityIsEmpty(id))
|
||||||
{
|
{
|
||||||
MarkForDestroy(id);
|
MarkForDestroy(id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
namespace Encompass
|
using MoonTools.FastCollections;
|
||||||
|
|
||||||
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal struct EntitySetQuery
|
internal struct EntitySetQuery
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,9 +2,9 @@ using System;
|
||||||
|
|
||||||
namespace Encompass.Exceptions
|
namespace Encompass.Exceptions
|
||||||
{
|
{
|
||||||
public class NoMessageOfTypeException : Exception
|
public class IllegalReadTypeException : Exception
|
||||||
{
|
{
|
||||||
public NoMessageOfTypeException(
|
public IllegalReadTypeException(
|
||||||
string format,
|
string format,
|
||||||
params object[] args
|
params object[] args
|
||||||
) : base(string.Format(format, args)) { }
|
) : base(string.Format(format, args)) { }
|
|
@ -1,11 +1,10 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Encompass.Exceptions
|
namespace Encompass.Exceptions
|
||||||
{
|
{
|
||||||
public class IllegalDrawLayerException : Exception
|
public class IllegalSendTypeException : Exception
|
||||||
{
|
{
|
||||||
public IllegalDrawLayerException(
|
public IllegalSendTypeException(
|
||||||
string format,
|
string format,
|
||||||
params object[] args
|
params object[] args
|
||||||
) : base(string.Format(format, args)) { }
|
) : base(string.Format(format, args)) { }
|
|
@ -1,12 +1,25 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
namespace Encompass.Exceptions
|
namespace Encompass.Exceptions
|
||||||
{
|
{
|
||||||
public class NoComponentOfTypeException : Exception
|
[Serializable]
|
||||||
|
internal class NoComponentOfTypeException : Exception
|
||||||
{
|
{
|
||||||
public NoComponentOfTypeException(
|
public NoComponentOfTypeException()
|
||||||
string format,
|
{
|
||||||
params object[] args
|
}
|
||||||
) : base(string.Format(format, args)) { }
|
|
||||||
|
public NoComponentOfTypeException(string message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public NoComponentOfTypeException(string message, Exception innerException) : base(message, innerException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected NoComponentOfTypeException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,390 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Encompass
|
|
||||||
{
|
|
||||||
internal class DirectedGraph<TNode> where TNode : IEquatable<TNode>
|
|
||||||
{
|
|
||||||
protected HashSet<TNode> nodes = new HashSet<TNode>();
|
|
||||||
protected Dictionary<TNode, HashSet<TNode>> neighbors = new Dictionary<TNode, HashSet<TNode>>();
|
|
||||||
|
|
||||||
protected HashSet<(TNode, TNode)> edges = new HashSet<(TNode, TNode)>();
|
|
||||||
|
|
||||||
public IEnumerable<TNode> Nodes => nodes;
|
|
||||||
public IEnumerable<(TNode, TNode)> Edges => edges;
|
|
||||||
|
|
||||||
public void AddNode(TNode node)
|
|
||||||
{
|
|
||||||
if (!Exists(node))
|
|
||||||
{
|
|
||||||
nodes.Add(node);
|
|
||||||
neighbors.Add(node, new HashSet<TNode>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddNodes(params TNode[] nodes)
|
|
||||||
{
|
|
||||||
foreach (var node in nodes)
|
|
||||||
{
|
|
||||||
AddNode(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveNode(TNode node)
|
|
||||||
{
|
|
||||||
CheckNodes(node);
|
|
||||||
|
|
||||||
var edgesToRemove = new List<(TNode, TNode)>();
|
|
||||||
|
|
||||||
foreach (var entry in neighbors)
|
|
||||||
{
|
|
||||||
if (entry.Value.Contains(node))
|
|
||||||
{
|
|
||||||
edgesToRemove.Add((entry.Key, node));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var edge in edgesToRemove)
|
|
||||||
{
|
|
||||||
RemoveEdge(edge.Item1, edge.Item2);
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes.Remove(node);
|
|
||||||
neighbors.Remove(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveEdge(TNode v, TNode u)
|
|
||||||
{
|
|
||||||
CheckEdge(v, u);
|
|
||||||
neighbors[v].Remove(u);
|
|
||||||
edges.Remove((v, u));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddEdge(TNode v, TNode u)
|
|
||||||
{
|
|
||||||
CheckNodes(v, u);
|
|
||||||
if (Exists(v, u)) { throw new ArgumentException($"Edge between {v} and {u} already exists in the graph"); }
|
|
||||||
|
|
||||||
if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); }
|
|
||||||
|
|
||||||
neighbors[v].Add(u);
|
|
||||||
edges.Add((v, u));
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Exists(TNode node)
|
|
||||||
{
|
|
||||||
return nodes.Contains(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Exists(TNode v, TNode u)
|
|
||||||
{
|
|
||||||
CheckNodes(v, u);
|
|
||||||
return edges.Contains((v, u));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void CheckNodes(params TNode[] givenNodes)
|
|
||||||
{
|
|
||||||
foreach (var node in givenNodes)
|
|
||||||
{
|
|
||||||
if (!Exists(node))
|
|
||||||
{
|
|
||||||
throw new System.ArgumentException($"Vertex {node} does not exist in the graph");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void CheckEdge(TNode v, TNode u)
|
|
||||||
{
|
|
||||||
CheckNodes(v, u);
|
|
||||||
if (!Exists(v, u)) { throw new ArgumentException($"Edge between vertex {v} and vertex {u} does not exist in the graph"); }
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<TNode> Neighbors(TNode node)
|
|
||||||
{
|
|
||||||
CheckNodes(node);
|
|
||||||
return neighbors[node];
|
|
||||||
}
|
|
||||||
|
|
||||||
public DirectedGraph<TNode> Clone()
|
|
||||||
{
|
|
||||||
var clone = new DirectedGraph<TNode>();
|
|
||||||
clone.AddNodes(Nodes.ToArray());
|
|
||||||
|
|
||||||
foreach (var v in Nodes)
|
|
||||||
{
|
|
||||||
foreach (var n in Neighbors(v))
|
|
||||||
{
|
|
||||||
clone.AddEdge(v, n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return clone;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DirectedGraph<TNode> SubGraph(params TNode[] subVertices)
|
|
||||||
{
|
|
||||||
var subGraph = new DirectedGraph<TNode>();
|
|
||||||
subGraph.AddNodes(subVertices.ToArray());
|
|
||||||
|
|
||||||
foreach (var n in Nodes)
|
|
||||||
{
|
|
||||||
if (Nodes.Contains(n))
|
|
||||||
{
|
|
||||||
var neighbors = Neighbors(n);
|
|
||||||
foreach (var u in neighbors)
|
|
||||||
{
|
|
||||||
if (subVertices.Contains(u))
|
|
||||||
{
|
|
||||||
subGraph.AddEdge(n, u);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return subGraph;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<TNode> PostorderNodeDFSHelper(HashSet<TNode> discovered, TNode v)
|
|
||||||
{
|
|
||||||
discovered.Add(v);
|
|
||||||
|
|
||||||
foreach (var neighbor in Neighbors(v))
|
|
||||||
{
|
|
||||||
if (!discovered.Contains(neighbor))
|
|
||||||
{
|
|
||||||
foreach (var node in PostorderNodeDFSHelper(discovered, neighbor))
|
|
||||||
{
|
|
||||||
yield return node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
yield return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IEnumerable<TNode> PostorderNodeDFS()
|
|
||||||
{
|
|
||||||
var dfsDiscovered = new HashSet<TNode>();
|
|
||||||
|
|
||||||
foreach (var node in Nodes)
|
|
||||||
{
|
|
||||||
if (!dfsDiscovered.Contains(node))
|
|
||||||
{
|
|
||||||
foreach (var thing in PostorderNodeDFSHelper(dfsDiscovered, node))
|
|
||||||
{
|
|
||||||
yield return thing;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<TNode> TopologicalSort()
|
|
||||||
{
|
|
||||||
return PostorderNodeDFS().Reverse();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Cyclic()
|
|
||||||
{
|
|
||||||
return StronglyConnectedComponents().Any((scc) => scc.Count() > 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<IEnumerable<TNode>> SimpleCycles()
|
|
||||||
{
|
|
||||||
void unblock(TNode thisnode, HashSet<TNode> blocked, Dictionary<TNode, HashSet<TNode>> B) //refactor to remove closure
|
|
||||||
{
|
|
||||||
var stack = new Stack<TNode>();
|
|
||||||
stack.Push(thisnode);
|
|
||||||
|
|
||||||
while (stack.Count > 0)
|
|
||||||
{
|
|
||||||
var node = stack.Pop();
|
|
||||||
if (blocked.Contains(thisnode))
|
|
||||||
{
|
|
||||||
blocked.Remove(thisnode);
|
|
||||||
if (B.ContainsKey(node))
|
|
||||||
{
|
|
||||||
foreach (var n in B[node])
|
|
||||||
{
|
|
||||||
if (!stack.Contains(n))
|
|
||||||
{
|
|
||||||
stack.Push(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
B[node].Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
List<List<TNode>> result = new List<List<TNode>>();
|
|
||||||
var subGraph = Clone();
|
|
||||||
|
|
||||||
var sccs = new Stack<IEnumerable<TNode>>();
|
|
||||||
foreach (var scc in StronglyConnectedComponents())
|
|
||||||
{
|
|
||||||
sccs.Push(scc);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (sccs.Count > 0)
|
|
||||||
{
|
|
||||||
var scc = new Stack<TNode>(sccs.Pop());
|
|
||||||
var startNode = scc.Pop();
|
|
||||||
var path = new Stack<TNode>();
|
|
||||||
path.Push(startNode);
|
|
||||||
var blocked = new HashSet<TNode>
|
|
||||||
{
|
|
||||||
startNode
|
|
||||||
};
|
|
||||||
var closed = new HashSet<TNode>();
|
|
||||||
var B = new Dictionary<TNode, HashSet<TNode>>();
|
|
||||||
var stack = new Stack<(TNode, Stack<TNode>)>();
|
|
||||||
stack.Push((startNode, new Stack<TNode>(subGraph.Neighbors(startNode))));
|
|
||||||
|
|
||||||
while (stack.Count > 0)
|
|
||||||
{
|
|
||||||
var entry = stack.Peek();
|
|
||||||
var thisnode = entry.Item1;
|
|
||||||
var neighbors = entry.Item2;
|
|
||||||
|
|
||||||
if (neighbors.Count > 0)
|
|
||||||
{
|
|
||||||
var nextNode = neighbors.Pop();
|
|
||||||
|
|
||||||
if (nextNode.Equals(startNode))
|
|
||||||
{
|
|
||||||
var resultPath = new List<TNode>();
|
|
||||||
foreach (var v in path)
|
|
||||||
{
|
|
||||||
resultPath.Add(v);
|
|
||||||
}
|
|
||||||
result.Add(resultPath);
|
|
||||||
foreach (var v in path)
|
|
||||||
{
|
|
||||||
closed.Add(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!blocked.Contains(nextNode))
|
|
||||||
{
|
|
||||||
path.Push(nextNode);
|
|
||||||
stack.Push((nextNode, new Stack<TNode>(subGraph.Neighbors(nextNode))));
|
|
||||||
closed.Remove(nextNode);
|
|
||||||
blocked.Add(nextNode);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (neighbors.Count == 0)
|
|
||||||
{
|
|
||||||
if (closed.Contains(thisnode))
|
|
||||||
{
|
|
||||||
unblock(thisnode, blocked, B);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (var neighbor in subGraph.Neighbors(thisnode))
|
|
||||||
{
|
|
||||||
if (!B.ContainsKey(neighbor))
|
|
||||||
{
|
|
||||||
B[neighbor] = new HashSet<TNode>();
|
|
||||||
}
|
|
||||||
B[neighbor].Add(thisnode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stack.Pop();
|
|
||||||
path.Pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
subGraph.RemoveNode(startNode);
|
|
||||||
var H = subGraph.SubGraph(scc.ToArray());
|
|
||||||
var HSccs = H.StronglyConnectedComponents();
|
|
||||||
foreach (var HScc in HSccs)
|
|
||||||
{
|
|
||||||
sccs.Push(HScc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.Distinct(new SimpleCycleComparer<TNode>());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IEnumerable<IEnumerable<TNode>> StronglyConnectedComponents()
|
|
||||||
{
|
|
||||||
var preorder = new Dictionary<TNode, uint>();
|
|
||||||
var lowlink = new Dictionary<TNode, uint>();
|
|
||||||
var sccFound = new Dictionary<TNode, bool>();
|
|
||||||
var sccQueue = new Stack<TNode>();
|
|
||||||
|
|
||||||
uint preorderCounter = 0;
|
|
||||||
|
|
||||||
foreach (var source in Nodes)
|
|
||||||
{
|
|
||||||
if (!sccFound.ContainsKey(source))
|
|
||||||
{
|
|
||||||
var queue = new Stack<TNode>();
|
|
||||||
queue.Push(source);
|
|
||||||
|
|
||||||
while (queue.Count > 0)
|
|
||||||
{
|
|
||||||
var v = queue.Peek();
|
|
||||||
if (!preorder.ContainsKey(v))
|
|
||||||
{
|
|
||||||
preorderCounter++;
|
|
||||||
preorder[v] = preorderCounter;
|
|
||||||
}
|
|
||||||
|
|
||||||
var done = true;
|
|
||||||
var vNeighbors = Neighbors(v);
|
|
||||||
foreach (var w in vNeighbors)
|
|
||||||
{
|
|
||||||
if (!preorder.ContainsKey(w))
|
|
||||||
{
|
|
||||||
queue.Push(w);
|
|
||||||
done = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (done)
|
|
||||||
{
|
|
||||||
lowlink[v] = preorder[v];
|
|
||||||
foreach (var w in vNeighbors)
|
|
||||||
{
|
|
||||||
if (!sccFound.ContainsKey(w))
|
|
||||||
{
|
|
||||||
if (preorder[w] > preorder[v])
|
|
||||||
{
|
|
||||||
lowlink[v] = Math.Min(lowlink[v], lowlink[w]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lowlink[v] = Math.Min(lowlink[v], preorder[w]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
queue.Pop();
|
|
||||||
if (lowlink[v] == preorder[v])
|
|
||||||
{
|
|
||||||
sccFound[v] = true;
|
|
||||||
var scc = new List<TNode>() { v };
|
|
||||||
while (sccQueue.Count > 0 && preorder[sccQueue.Peek()] > preorder[v])
|
|
||||||
{
|
|
||||||
var k = sccQueue.Pop();
|
|
||||||
sccFound[k] = true;
|
|
||||||
scc.Add(k);
|
|
||||||
}
|
|
||||||
|
|
||||||
yield return scc;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sccQueue.Push(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Encompass
|
|
||||||
{
|
|
||||||
internal class SimpleCycleComparer<TNode> : IEqualityComparer<IEnumerable<TNode>>
|
|
||||||
{
|
|
||||||
public bool Equals(IEnumerable<TNode> x, IEnumerable<TNode> y)
|
|
||||||
{
|
|
||||||
return x.SequenceEqual(y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetHashCode(IEnumerable<TNode> obj)
|
|
||||||
{
|
|
||||||
return obj.Aggregate(0, (current, next) => current.GetHashCode() ^ next.GetHashCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace Encompass
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Structs that implement IComponent are considered to be Components.
|
||||||
|
/// </summary>
|
||||||
|
public interface IComponent { }
|
||||||
|
}
|
|
@ -4,25 +4,25 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
internal class IDManager
|
internal class IDManager
|
||||||
{
|
{
|
||||||
int _nextID = 0;
|
int nextID = 0;
|
||||||
|
|
||||||
private readonly Stack<int> _availableIDs = new Stack<int>();
|
private Stack<int> availableIDs = new Stack<int>();
|
||||||
|
|
||||||
public int NextID()
|
public int NextID()
|
||||||
{
|
{
|
||||||
if (_availableIDs.Count > 0)
|
if (availableIDs.Count > 0)
|
||||||
{
|
{
|
||||||
return _availableIDs.Pop();
|
return availableIDs.Pop();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return _nextID++;
|
return nextID++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Free(int id)
|
public void Free(int ID)
|
||||||
{
|
{
|
||||||
_availableIDs.Push(id);
|
availableIDs.Push(ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace Encompass
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Structs that implement IMessage are considered to be Messages.
|
||||||
|
/// </summary>
|
||||||
|
public interface IMessage { }
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace Encompass
|
||||||
|
{
|
||||||
|
public interface IDrawableComponent
|
||||||
|
{
|
||||||
|
int Layer { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,71 +1,72 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal class MessageManager
|
internal class MessageManager
|
||||||
{
|
{
|
||||||
private readonly TimeManager _timeManager;
|
private readonly TimeManager timeManager;
|
||||||
private readonly MessageStore _messageStore = new MessageStore();
|
private readonly MessageStore messageStore = new MessageStore();
|
||||||
|
|
||||||
public MessageManager(TimeManager timeManager)
|
public MessageManager(TimeManager timeManager)
|
||||||
{
|
{
|
||||||
_timeManager = timeManager;
|
this.timeManager = timeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddMessage<TMessage>(in TMessage message) where TMessage : struct
|
internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageStore.AddMessage(message);
|
messageStore.AddMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct
|
internal void AddMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageStore.AddMessage(message, time);
|
messageStore.AddMessage(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct
|
internal void AddMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageStore.AddMessageIgnoringTimeDilation(message, time);
|
messageStore.AddMessageIgnoringTimeDilation(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ClearMessages()
|
internal void ClearMessages()
|
||||||
{
|
{
|
||||||
_messageStore.ClearAll();
|
messageStore.ClearAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ProcessDelayedMessages(double dt)
|
internal void ProcessDelayedMessages(double dt)
|
||||||
{
|
{
|
||||||
_messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt);
|
messageStore.ProcessDelayedMessages(dt * timeManager.TimeDilationFactor, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ReadOnlySpan<TMessage> GetMessagesByType<TMessage>() where TMessage : struct
|
internal IEnumerable<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
return _messageStore.All<TMessage>();
|
return messageStore.All<TMessage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool Any<TMessage>() where TMessage : struct
|
internal bool Any<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
return _messageStore.Any<TMessage>();
|
return messageStore.Any<TMessage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref readonly TMessage First<TMessage>() where TMessage : struct
|
internal TMessage First<TMessage>() where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
return ref _messageStore.First<TMessage>();
|
return messageStore.First<TMessage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
return _messageStore.WithEntity<TMessage>(entityID);
|
return messageStore.WithEntity<TMessage>(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ref readonly TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
internal TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
return ref _messageStore.FirstWithEntity<TMessage>(entityID);
|
var enumerator = messageStore.WithEntity<TMessage>(entityID).GetEnumerator();
|
||||||
|
enumerator.MoveNext();
|
||||||
|
return enumerator.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IHasEntity
|
internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
return _messageStore.SomeWithEntity<TMessage>(entityID);
|
return messageStore.SomeWithEntity<TMessage>(entityID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,51 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal class RenderManager
|
internal class RenderManager
|
||||||
{
|
{
|
||||||
private readonly EntityManager _entityManager;
|
private readonly EntityManager entityManager;
|
||||||
private readonly List<Renderer> renderers = new List<Renderer>();
|
private readonly DrawLayerManager drawLayerManager;
|
||||||
|
|
||||||
public RenderManager(EntityManager entityManager)
|
private readonly Dictionary<Type, Action<Entity, IComponent>> drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity, IComponent>>(256);
|
||||||
|
|
||||||
|
public RenderManager(EntityManager entityManager, DrawLayerManager drawLayerManager)
|
||||||
{
|
{
|
||||||
_entityManager = entityManager;
|
this.entityManager = entityManager;
|
||||||
|
this.drawLayerManager = drawLayerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddRenderer(Renderer renderer)
|
public void RegisterOrderedRenderer<TComponent>(Action<Entity, IComponent> renderAction) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
renderers.Add(renderer);
|
drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction);
|
||||||
|
drawLayerManager.RegisterOrderedDrawable<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(double dt, double alpha)
|
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
|
||||||
{
|
{
|
||||||
foreach (var renderer in renderers)
|
drawLayerManager.RegisterGeneralRendererWithLayer(renderer, layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Draw()
|
||||||
|
{
|
||||||
|
foreach (var layer in drawLayerManager.LayerOrder)
|
||||||
{
|
{
|
||||||
renderer.Render(dt, alpha);
|
var generalRendererSet = drawLayerManager.GeneralRenderersByLayer(layer);
|
||||||
|
|
||||||
|
foreach (var (entityID, componentType, component) in drawLayerManager.AllInLayer(layer))
|
||||||
|
{
|
||||||
|
if (drawComponentTypeToOrderedRenderer.ContainsKey(componentType))
|
||||||
|
{
|
||||||
|
var internalRenderAction = drawComponentTypeToOrderedRenderer[componentType];
|
||||||
|
internalRenderAction(entityManager.GetEntity(entityID), component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var generalRenderer in generalRendererSet)
|
||||||
|
{
|
||||||
|
generalRenderer.Render();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,68 +1,75 @@
|
||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
public abstract class Renderer
|
public abstract class Renderer
|
||||||
{
|
{
|
||||||
internal EntityManager _entityManager;
|
internal EntityManager entityManager;
|
||||||
internal ComponentManager _componentManager;
|
internal ComponentManager componentManager;
|
||||||
|
|
||||||
internal void AssignEntityManager(EntityManager entityManager)
|
internal void AssignEntityManager(EntityManager entityManager)
|
||||||
{
|
{
|
||||||
_entityManager = entityManager;
|
this.entityManager = entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void AssignComponentManager(ComponentManager componentManager)
|
internal void AssignComponentManager(ComponentManager componentManager)
|
||||||
{
|
{
|
||||||
_componentManager = componentManager;
|
this.componentManager = componentManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void Render(double dt, double alpha);
|
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
|
||||||
|
|
||||||
protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
|
|
||||||
{
|
{
|
||||||
return _componentManager.GetExistingEntities<TComponent>();
|
foreach (var pair in ReadComponentsIncludingEntity<TComponent>())
|
||||||
|
{
|
||||||
|
yield return pair.Item2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IEnumerable<Entity> ReadEntitiesAsEnumerable<TComponent>() where TComponent : struct
|
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _componentManager.GetExistingEntitiesAsEnumerable<TComponent>();
|
return ReadComponentIncludingEntity<TComponent>().Item2;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct
|
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _componentManager.ExistingSingularEntity<TComponent>();
|
return componentManager.GetComponentsByType<TComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
|
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _componentManager.GetComponentsByType<TComponent>();
|
foreach (var (component, id) in componentManager.GetComponentsIncludingEntity<TComponent>())
|
||||||
|
{
|
||||||
|
yield return (component, entityManager.GetEntity(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected IEnumerable<TComponent> ReadComponentsAsEnumerable<TComponent>() where TComponent : struct
|
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _componentManager.GetComponentsByTypeEnumerable<TComponent>();
|
var enumerator = ReadComponents<TComponent>().GetEnumerator();
|
||||||
|
enumerator.MoveNext();
|
||||||
|
return enumerator.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct
|
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _componentManager.ExistingSingular<TComponent>();
|
var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator();
|
||||||
|
enumerator.MoveNext();
|
||||||
|
return enumerator.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct
|
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
|
return componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct
|
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
|
return componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected bool SomeComponent<TComponent>() where TComponent : struct
|
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
return _componentManager.SomeExistingComponent<TComponent>();
|
return componentManager.SomeExistingComponent<TComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
namespace Encompass
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// GeneralRenderer is a Renderer which generically reads the game state in order to draw elements to the screen.
|
||||||
|
/// GeneralRenderers have a layer specified when they are added to the World.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class GeneralRenderer : Renderer
|
||||||
|
{
|
||||||
|
public abstract void Render();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Encompass
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IComponent, IDrawableComponent
|
||||||
|
{
|
||||||
|
public abstract void Render(Entity entity, TComponent drawComponent);
|
||||||
|
|
||||||
|
internal void InternalRender(Entity entity, IComponent component)
|
||||||
|
{
|
||||||
|
Render(entity, (TComponent)component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,56 +1,35 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal struct TimeDilationData
|
internal struct TimeDilationData
|
||||||
{
|
{
|
||||||
private readonly double _factor;
|
public double elapsedTime;
|
||||||
|
public double easeInTime;
|
||||||
|
public System.Func<double, double, double, double, double> easeInFunction;
|
||||||
|
public double activeTime;
|
||||||
|
public double easeOutTime;
|
||||||
|
public System.Func<double, double, double, double, double> easeOutFunction;
|
||||||
|
public double factor;
|
||||||
|
|
||||||
public double Factor
|
public double Factor
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (ElapsedTime < EaseInTime)
|
if (elapsedTime < easeInTime)
|
||||||
{
|
{
|
||||||
return EaseInFunction(ElapsedTime, 1, _factor - 1, EaseInTime);
|
return easeInFunction(elapsedTime, 1, factor - 1, easeInTime);
|
||||||
}
|
}
|
||||||
else if (ElapsedTime < EaseInTime + ActiveTime)
|
else if (elapsedTime < easeInTime + activeTime)
|
||||||
{
|
{
|
||||||
return _factor;
|
return factor;
|
||||||
}
|
}
|
||||||
else if (ElapsedTime < EaseInTime + ActiveTime + EaseOutTime)
|
else if (elapsedTime < easeInTime + activeTime + easeOutTime)
|
||||||
{
|
{
|
||||||
var elapsedOutTime = ElapsedTime - EaseInTime - ActiveTime;
|
var elapsedOutTime = elapsedTime - easeInTime - activeTime;
|
||||||
return EaseOutFunction(elapsedOutTime, _factor, 1 - _factor, EaseOutTime);
|
return easeOutFunction(elapsedOutTime, factor, 1 - factor, easeOutTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double ElapsedTime { get; set; }
|
|
||||||
public double EaseInTime { get; }
|
|
||||||
public Func<double, double, double, double, double> EaseInFunction { get; }
|
|
||||||
public double ActiveTime { get; }
|
|
||||||
public double EaseOutTime { get; }
|
|
||||||
public Func<double, double, double, double, double> EaseOutFunction { get; }
|
|
||||||
|
|
||||||
public TimeDilationData(
|
|
||||||
double factor,
|
|
||||||
double easeInTime,
|
|
||||||
Func<double, double, double, double, double> easeInfunction,
|
|
||||||
double activeTime,
|
|
||||||
double easeOutTime,
|
|
||||||
Func<double, double, double, double, double> easeOutFunction
|
|
||||||
)
|
|
||||||
{
|
|
||||||
_factor = factor;
|
|
||||||
EaseInTime = easeInTime;
|
|
||||||
EaseInFunction = easeInfunction;
|
|
||||||
ActiveTime = activeTime;
|
|
||||||
EaseOutTime = easeOutTime;
|
|
||||||
EaseOutFunction = easeOutFunction;
|
|
||||||
ElapsedTime = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
internal class TimeManager
|
internal class TimeManager
|
||||||
{
|
{
|
||||||
private readonly List<TimeDilationData> _timeDilationDatas = new List<TimeDilationData>(32);
|
private readonly List<TimeDilationData> timeDilationDatas = new List<TimeDilationData>(32);
|
||||||
|
|
||||||
private double Linear(double t, double b, double c, double d)
|
private double Linear(double t, double b, double c, double d)
|
||||||
{
|
{
|
||||||
|
@ -15,36 +15,36 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_timeDilationDatas.Count == 0) { return 1; }
|
if (timeDilationDatas.Count == 0) { return 1; }
|
||||||
var average = 0.0;
|
var average = 0.0;
|
||||||
foreach (var data in _timeDilationDatas)
|
foreach (var data in timeDilationDatas)
|
||||||
{
|
{
|
||||||
average += data.Factor;
|
average += data.Factor;
|
||||||
}
|
}
|
||||||
return average / _timeDilationDatas.Count;
|
return average / timeDilationDatas.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TimeDilationActive
|
public bool TimeDilationActive
|
||||||
{
|
{
|
||||||
get => _timeDilationDatas.Count != 0;
|
get => timeDilationDatas.Count != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(double dt)
|
public void Update(double dt)
|
||||||
{
|
{
|
||||||
for (var i = _timeDilationDatas.Count - 1; i >= 0; i--)
|
for (var i = timeDilationDatas.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var data = _timeDilationDatas[i];
|
var data = timeDilationDatas[i];
|
||||||
|
|
||||||
data.ElapsedTime += dt;
|
data.elapsedTime += dt;
|
||||||
|
|
||||||
if (data.ElapsedTime > data.EaseInTime + data.ActiveTime + data.EaseOutTime)
|
if (data.elapsedTime > data.easeInTime + data.activeTime + data.easeOutTime)
|
||||||
{
|
{
|
||||||
_timeDilationDatas.RemoveAt(i);
|
timeDilationDatas.RemoveAt(i);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_timeDilationDatas[i] = data;
|
timeDilationDatas[i] = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,15 +66,16 @@ namespace Encompass
|
||||||
|
|
||||||
public void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
public void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
||||||
{
|
{
|
||||||
_timeDilationDatas.Add(new TimeDilationData
|
timeDilationDatas.Add(new TimeDilationData
|
||||||
(
|
{
|
||||||
factor,
|
elapsedTime = 0,
|
||||||
easeInTime,
|
easeInTime = easeInTime,
|
||||||
easeInFunction,
|
easeInFunction = easeInFunction,
|
||||||
activeTime,
|
activeTime = activeTime,
|
||||||
easeOutTime,
|
easeOutTime = easeOutTime,
|
||||||
easeOutFunction
|
easeOutFunction = easeOutFunction,
|
||||||
));
|
factor = factor
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,13 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
internal class TrackingManager
|
internal class TrackingManager
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Type, HashSet<Engine>> _immediateComponentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
|
private Dictionary<Type, HashSet<Engine>> _immediateComponentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
|
||||||
private readonly Dictionary<Type, HashSet<Engine>> _componentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
|
private Dictionary<Type, HashSet<Engine>> _componentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
|
||||||
|
|
||||||
private readonly HashSet<(int, Type)> _additions = new HashSet<(int, Type)>();
|
private HashSet<(int, Type)> _additions = new HashSet<(int, Type)>();
|
||||||
private readonly HashSet<(int, Type)> _removals = new HashSet<(int, Type)>();
|
private HashSet<(int, Type)> _removals = new HashSet<(int, Type)>();
|
||||||
|
|
||||||
private readonly HashSet<(int, Engine)> _pairsToCheck = new HashSet<(int, Engine)>();
|
private HashSet<(int, Engine)> _pairsToCheck = new HashSet<(int, Engine)>();
|
||||||
|
|
||||||
public void RegisterComponentTypeToEngine(Type type, Engine engine)
|
public void RegisterComponentTypeToEngine(Type type, Engine engine)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
internal class UberEngine : Engine
|
internal class UberEngine : Engine
|
||||||
{
|
{
|
||||||
private readonly IEnumerable<Type> _componentTypes;
|
private IEnumerable<Type> _componentTypes;
|
||||||
private readonly IEnumerable<Type> _messageTypes;
|
private IEnumerable<Type> _messageTypes;
|
||||||
public Entity Entity { get; private set; }
|
public Entity Entity { get; private set; }
|
||||||
|
|
||||||
public UberEngine(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
|
public UberEngine(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
|
||||||
{
|
{
|
||||||
_componentTypes = componentTypes;
|
_componentTypes = componentTypes;
|
||||||
_messageTypes = messageTypes;
|
_messageTypes = messageTypes;
|
||||||
AddTypes.UnionWith(componentTypes);
|
readTypes.UnionWith(componentTypes);
|
||||||
ReadTypes.UnionWith(componentTypes);
|
writeTypes.UnionWith(componentTypes);
|
||||||
WriteTypes.UnionWith(componentTypes);
|
sendTypes.UnionWith(messageTypes);
|
||||||
SendTypes.UnionWith(messageTypes);
|
receiveTypes.UnionWith(messageTypes);
|
||||||
ReceiveTypes.UnionWith(messageTypes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write()
|
public void Write()
|
||||||
|
@ -27,8 +27,7 @@ namespace Encompass
|
||||||
|
|
||||||
foreach (var type in _componentTypes)
|
foreach (var type in _componentTypes)
|
||||||
{
|
{
|
||||||
var instance = Activator.CreateInstance(type);
|
var instanceParam = new object[] { Entity, Activator.CreateInstance(type) };
|
||||||
var instanceParam = new object[] { Entity, instance };
|
|
||||||
var setComponentMethod = typeof(Engine).GetMethod("SetComponent", BindingFlags.NonPublic | BindingFlags.Instance);
|
var setComponentMethod = typeof(Engine).GetMethod("SetComponent", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
var genericSetComponentMethod = setComponentMethod.MakeGenericMethod(type);
|
var genericSetComponentMethod = setComponentMethod.MakeGenericMethod(type);
|
||||||
genericSetComponentMethod.Invoke(this, instanceParam);
|
genericSetComponentMethod.Invoke(this, instanceParam);
|
||||||
|
@ -39,59 +38,54 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
foreach (var type in _componentTypes)
|
foreach (var type in _componentTypes)
|
||||||
{
|
{
|
||||||
CallGenericWrappedMethod(type, "CallAllComponentMethods", null);
|
CallGenericMethod(type, "ReadComponent", null);
|
||||||
|
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
|
||||||
|
CallGenericMethod(type, "ReadComponents", null);
|
||||||
|
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
|
||||||
|
CallGenericMethod(type, "ReadEntity", null);
|
||||||
|
CallGenericMethod(type, "ReadEntities", null);
|
||||||
|
CallGenericMethod(type, "GetComponent", new Type[] { typeof(Entity) }, new object[] { Entity });
|
||||||
|
CallGenericMethod(type, "HasComponent", new Type[] { typeof(Entity) }, new object[] { Entity });
|
||||||
|
CallGenericMethod(type, "SomeComponent", null);
|
||||||
|
CallGenericMethod(type, "DestroyWith", null);
|
||||||
|
CallGenericMethod(type, "DestroyAllWith", null);
|
||||||
|
CallGenericMethod(type, "RemoveComponent", new Type[] { typeof(Entity) }, new object[] { Entity });
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var type in _messageTypes)
|
foreach (var type in _messageTypes)
|
||||||
{
|
{
|
||||||
CallGenericWrappedMethod(type, "CallAllMessageMethods", null);
|
CallGenericMethod(type, "SendMessageIgnoringTimeDilation", new object[] { Activator.CreateInstance(type), 1 });
|
||||||
|
CallGenericMethod(type, "SendMessage", 1, new object[] { Activator.CreateInstance(type) });
|
||||||
|
CallGenericMethod(type, "SendMessage", 2, new object[] { Activator.CreateInstance(type), 1 });
|
||||||
|
CallGenericMethod(type, "ReadMessage", null);
|
||||||
|
CallGenericMethod(type, "ReadMessages", null);
|
||||||
|
CallGenericMethod(type, "SomeMessage", null);
|
||||||
if (typeof(IHasEntity).IsAssignableFrom(type))
|
if (typeof(IHasEntity).IsAssignableFrom(type))
|
||||||
{
|
{
|
||||||
CallGenericWrappedMethod(type, "CallAllEntityMessageMethods", null);
|
CallGenericMethod(type, "ReadMessagesWithEntity", new Type[] { typeof(Entity) }, new object[] { Entity });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// we can't reflect invoke on byref returns or Span returns right now... so we have non-return wrapper methods
|
private void CallGenericMethod(Type type, string methodName, object[] parameters)
|
||||||
|
|
||||||
protected void CallAllComponentMethods<TComponent>() where TComponent : struct
|
|
||||||
{
|
{
|
||||||
ReadComponent<TComponent>();
|
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
ReadComponents<TComponent>();
|
|
||||||
ReadEntity<TComponent>();
|
|
||||||
ReadEntities<TComponent>();
|
|
||||||
GetComponent<TComponent>(Entity);
|
|
||||||
HasComponent<TComponent>(Entity);
|
|
||||||
SomeComponent<TComponent>();
|
|
||||||
DestroyWith<TComponent>();
|
|
||||||
DestroyAllWith<TComponent>();
|
|
||||||
RemoveComponent<TComponent>(Entity);
|
|
||||||
AddComponent<TComponent>(Entity, default);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void CallAllMessageMethods<TMessage>() where TMessage : struct
|
|
||||||
{
|
|
||||||
SendMessageIgnoringTimeDilation<TMessage>(default, 0.1);
|
|
||||||
SendMessage<TMessage>(default);
|
|
||||||
SendMessage<TMessage>(default, 0.1);
|
|
||||||
ReadMessage<TMessage>();
|
|
||||||
ReadMessages<TMessage>();
|
|
||||||
SomeMessage<TMessage>();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void CallAllEntityMessageMethods<TMessage>() where TMessage : struct, IHasEntity
|
|
||||||
{
|
|
||||||
ReadMessagesWithEntity<TMessage>(Entity);
|
|
||||||
ReadMessageWithEntity<TMessage>(Entity);
|
|
||||||
SomeMessageWithEntity<TMessage>(Entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CallGenericWrappedMethod(Type type, string methodName, object[] parameters)
|
|
||||||
{
|
|
||||||
var readComponentMethod = typeof(UberEngine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
|
||||||
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
|
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
|
||||||
genericReadComponentMethod.Invoke(this, parameters);
|
genericReadComponentMethod.Invoke(this, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void CallGenericMethod(Type type, string methodName, Type[] types, object[] parameters)
|
||||||
|
{
|
||||||
|
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
|
||||||
|
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
|
||||||
|
genericReadComponentMethod.Invoke(this, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CallGenericMethod(Type type, string methodName, int argumentNum, object[] parameters)
|
||||||
|
{
|
||||||
|
var method = typeof(Engine).GetRuntimeMethods().Where(m => m.Name == methodName && m.GetParameters().Length == argumentNum).First();
|
||||||
|
var genericMethod = method.MakeGenericMethod(type);
|
||||||
|
genericMethod.Invoke(this, parameters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
class UberRenderer : Renderer
|
class UberRenderer : Renderer
|
||||||
{
|
{
|
||||||
private readonly IEnumerable<Type> _componentTypes;
|
private IEnumerable<Type> _componentTypes;
|
||||||
private Entity _entity;
|
private Entity _entity;
|
||||||
|
|
||||||
public UberRenderer(IEnumerable<Type> componentTypes)
|
public UberRenderer(IEnumerable<Type> componentTypes)
|
||||||
|
@ -19,29 +19,25 @@ namespace Encompass
|
||||||
_entity = entity;
|
_entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
// can't reflect invoke on Span returns...
|
public void Render()
|
||||||
public override void Render(double dt, double alpha)
|
|
||||||
{
|
{
|
||||||
foreach (var type in _componentTypes)
|
foreach (var type in _componentTypes)
|
||||||
{
|
{
|
||||||
CallGenericWrappedMethod(type, "CallAllComponentMethods", null);
|
CallGenericMethod(type, "ReadEntities", null);
|
||||||
|
CallGenericMethod(type, "ReadEntity", null);
|
||||||
|
CallGenericMethod(type, "ReadComponent", null);
|
||||||
|
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
|
||||||
|
CallGenericMethod(type, "ReadComponents", null);
|
||||||
|
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
|
||||||
|
CallGenericMethod(type, "GetComponent", new object[] { _entity });
|
||||||
|
CallGenericMethod(type, "HasComponent", new object[] { _entity });
|
||||||
|
CallGenericMethod(type, "SomeComponent", null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void CallAllComponentMethods<TComponent>() where TComponent : struct
|
private void CallGenericMethod(Type type, string methodName, object[] parameters)
|
||||||
{
|
{
|
||||||
ReadEntity<TComponent>();
|
var readComponentMethod = typeof(Renderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
ReadEntities<TComponent>();
|
|
||||||
ReadComponent<TComponent>();
|
|
||||||
ReadComponents<TComponent>();
|
|
||||||
GetComponent<TComponent>(_entity);
|
|
||||||
HasComponent<TComponent>(_entity);
|
|
||||||
SomeComponent<TComponent>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CallGenericWrappedMethod(Type type, string methodName, object[] parameters)
|
|
||||||
{
|
|
||||||
var readComponentMethod = typeof(UberRenderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
|
||||||
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
|
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
|
||||||
genericReadComponentMethod.Invoke(this, parameters);
|
genericReadComponentMethod.Invoke(this, parameters);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,13 @@ namespace Encompass
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class World
|
public class World
|
||||||
{
|
{
|
||||||
private readonly List<Engine> _enginesInOrder;
|
private readonly List<Engine> enginesInOrder;
|
||||||
private readonly EntityManager _entityManager;
|
private readonly EntityManager entityManager;
|
||||||
private readonly ComponentManager _componentManager;
|
private readonly ComponentManager componentManager;
|
||||||
private readonly TrackingManager _trackingManager;
|
private readonly TrackingManager trackingManager;
|
||||||
private readonly MessageManager _messageManager;
|
private readonly MessageManager messageManager;
|
||||||
private readonly TimeManager _timeManager;
|
private readonly TimeManager timeManager;
|
||||||
private readonly RenderManager _renderManager;
|
private readonly RenderManager renderManager;
|
||||||
|
|
||||||
internal World(
|
internal World(
|
||||||
List<Engine> enginesInOrder,
|
List<Engine> enginesInOrder,
|
||||||
|
@ -25,13 +25,13 @@ namespace Encompass
|
||||||
RenderManager renderManager
|
RenderManager renderManager
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_enginesInOrder = enginesInOrder;
|
this.enginesInOrder = enginesInOrder;
|
||||||
_entityManager = entityManager;
|
this.entityManager = entityManager;
|
||||||
_componentManager = componentManager;
|
this.componentManager = componentManager;
|
||||||
_trackingManager = trackingManager;
|
this.trackingManager = trackingManager;
|
||||||
_messageManager = messageManager;
|
this.messageManager = messageManager;
|
||||||
_timeManager = timeManager;
|
this.timeManager = timeManager;
|
||||||
_renderManager = renderManager;
|
this.renderManager = renderManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -40,15 +40,15 @@ namespace Encompass
|
||||||
/// <param name="dt">The time in seconds that has passed since the previous frame.</param>
|
/// <param name="dt">The time in seconds that has passed since the previous frame.</param>
|
||||||
public void Update(double dt)
|
public void Update(double dt)
|
||||||
{
|
{
|
||||||
_trackingManager.UpdateTracking();
|
trackingManager.UpdateTracking();
|
||||||
_messageManager.ProcessDelayedMessages(dt);
|
messageManager.ProcessDelayedMessages(dt);
|
||||||
_timeManager.Update(dt);
|
timeManager.Update(dt);
|
||||||
|
|
||||||
foreach (var engine in _enginesInOrder)
|
foreach (var engine in enginesInOrder)
|
||||||
{
|
{
|
||||||
if (engine._usesTimeDilation)
|
if (engine.usesTimeDilation)
|
||||||
{
|
{
|
||||||
engine.Update(dt * _timeManager.TimeDilationFactor);
|
engine.Update(dt * timeManager.TimeDilationFactor);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -58,20 +58,20 @@ namespace Encompass
|
||||||
engine.ClearNewlyCreatedEntities();
|
engine.ClearNewlyCreatedEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
_messageManager.ClearMessages();
|
messageManager.ClearMessages();
|
||||||
_entityManager.PruneEmptyEntities();
|
entityManager.PruneEmptyEntities();
|
||||||
_entityManager.DestroyMarkedEntities(_enginesInOrder);
|
entityManager.DestroyMarkedEntities(enginesInOrder);
|
||||||
|
|
||||||
_componentManager.RemoveMarkedComponents();
|
componentManager.RemoveMarkedComponents();
|
||||||
_componentManager.WriteComponents();
|
componentManager.WriteComponents();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Causes the Renderers to draw.
|
/// Causes the Renderers to draw.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Draw(double dt, double alpha)
|
public void Draw()
|
||||||
{
|
{
|
||||||
_renderManager.Draw(dt, alpha);
|
renderManager.Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Encompass.Exceptions;
|
using Encompass.Exceptions;
|
||||||
|
using MoonTools.Core.Graph;
|
||||||
|
using MoonTools.Core.Graph.Extensions;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
{
|
{
|
||||||
|
@ -16,37 +18,43 @@ namespace Encompass
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class WorldBuilder
|
public class WorldBuilder
|
||||||
{
|
{
|
||||||
private readonly int _entityCapacity;
|
private readonly int entityCapacity;
|
||||||
private readonly List<Engine> _engines = new List<Engine>();
|
private readonly List<Engine> engines = new List<Engine>();
|
||||||
private readonly DirectedGraph<Engine> _engineGraph = new DirectedGraph<Engine>();
|
private readonly DirectedGraph<Engine, Unit> engineGraph = GraphBuilder.DirectedGraph<Engine>();
|
||||||
private readonly ComponentStore _startingExistingComponentStore;
|
private readonly ComponentStore startingExistingComponentStore;
|
||||||
private readonly ComponentStore _startingUpToDateComponentStore;
|
private readonly ComponentStore startingUpToDateComponentStore;
|
||||||
|
|
||||||
private readonly ComponentManager _componentManager;
|
private readonly ComponentManager componentManager;
|
||||||
private readonly EntityManager _entityManager;
|
private readonly EntityManager entityManager;
|
||||||
private readonly MessageManager _messageManager;
|
private readonly MessageManager messageManager;
|
||||||
private readonly TimeManager _timeManager;
|
private readonly TimeManager timeManager;
|
||||||
private readonly RenderManager _renderManager;
|
private readonly DrawLayerManager drawLayerManager;
|
||||||
private readonly TrackingManager _trackingManager;
|
private readonly RenderManager renderManager;
|
||||||
|
private readonly TrackingManager trackingManager;
|
||||||
|
|
||||||
private readonly Dictionary<Type, HashSet<Engine>> _typeToReaders = new Dictionary<Type, HashSet<Engine>>();
|
private readonly Dictionary<Type, HashSet<Engine>> typeToReaders = new Dictionary<Type, HashSet<Engine>>();
|
||||||
private readonly HashSet<Engine> _senders = new HashSet<Engine>();
|
|
||||||
private readonly HashSet<Type> _componentTypesToPreload = new HashSet<Type>();
|
private readonly HashSet<Engine> senders = new HashSet<Engine>();
|
||||||
private readonly HashSet<Type> _messageTypes = new HashSet<Type>();
|
|
||||||
private readonly Dictionary<Type, int> _typeToIndex = new Dictionary<Type, int>();
|
private readonly HashSet<Type> componentTypesToPreload = new HashSet<Type>();
|
||||||
|
|
||||||
|
private readonly HashSet<Type> messageTypes = new HashSet<Type>();
|
||||||
|
|
||||||
|
private readonly Dictionary<Type, int> typeToIndex = new Dictionary<Type, int>();
|
||||||
|
|
||||||
public WorldBuilder(int entityCapacity = 32768)
|
public WorldBuilder(int entityCapacity = 32768)
|
||||||
{
|
{
|
||||||
_entityCapacity = entityCapacity;
|
this.entityCapacity = entityCapacity;
|
||||||
_timeManager = new TimeManager();
|
drawLayerManager = new DrawLayerManager(typeToIndex);
|
||||||
_trackingManager = new TrackingManager();
|
timeManager = new TimeManager();
|
||||||
_componentManager = new ComponentManager(_typeToIndex);
|
trackingManager = new TrackingManager();
|
||||||
_messageManager = new MessageManager(_timeManager);
|
componentManager = new ComponentManager(drawLayerManager, typeToIndex);
|
||||||
_entityManager = new EntityManager(_componentManager, entityCapacity);
|
messageManager = new MessageManager(timeManager);
|
||||||
_renderManager = new RenderManager(_entityManager);
|
entityManager = new EntityManager(componentManager, entityCapacity);
|
||||||
|
renderManager = new RenderManager(entityManager, drawLayerManager);
|
||||||
|
|
||||||
_startingExistingComponentStore = new ComponentStore(_typeToIndex);
|
startingExistingComponentStore = new ComponentStore(typeToIndex);
|
||||||
_startingUpToDateComponentStore = new ComponentStore(_typeToIndex);
|
startingUpToDateComponentStore = new ComponentStore(typeToIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -54,57 +62,56 @@ namespace Encompass
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Entity CreateEntity()
|
public Entity CreateEntity()
|
||||||
{
|
{
|
||||||
return _entityManager.CreateEntity();
|
return entityManager.CreateEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies that the given Message should be sent immediately on the first World Update.
|
/// Specifies that the given Message should be sent immediately on the first World Update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void SendMessage<TMessage>(in TMessage message) where TMessage : struct
|
public void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageManager.AddMessage(message);
|
messageManager.AddMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies that the given Message should be sent after the specified number of seconds after the first World Update.
|
/// Specifies that the given Message should be sent after the specified number of seconds after the first World Update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct
|
public void SendMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
||||||
{
|
{
|
||||||
_messageManager.AddMessage<TMessage>(message, time);
|
messageManager.AddMessage<TMessage>(message, time);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets Component data for the specified Component Type on the specified Entity.
|
/// Sets Component data for the specified Component Type on the specified Entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct
|
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
||||||
{
|
{
|
||||||
RegisterComponentType<TComponent>();
|
RegisterComponentType<TComponent>();
|
||||||
_startingExistingComponentStore.Set(entity.ID, component);
|
startingExistingComponentStore.Set(entity.ID, component);
|
||||||
_startingUpToDateComponentStore.Set(entity.ID, component);
|
startingUpToDateComponentStore.Set(entity.ID, component);
|
||||||
}
|
|
||||||
|
|
||||||
internal void RegisterComponentTypeNonGeneric(Type type)
|
if (component is IDrawableComponent drawableComponent)
|
||||||
{
|
|
||||||
var method = GetType().GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
||||||
var generic = method.MakeGenericMethod(type);
|
|
||||||
generic.Invoke(this, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void RegisterComponentType<TComponent>() where TComponent : struct
|
|
||||||
{
|
|
||||||
if (!_typeToIndex.ContainsKey(typeof(TComponent)))
|
|
||||||
{
|
{
|
||||||
_typeToIndex.Add(typeof(TComponent), _typeToIndex.Count);
|
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
|
||||||
_componentTypesToPreload.Add(typeof(TComponent));
|
drawLayerManager.RegisterOrderedDrawable<TComponent>();
|
||||||
_componentManager.RegisterComponentType<TComponent>();
|
}
|
||||||
_startingExistingComponentStore.RegisterComponentType<TComponent>();
|
}
|
||||||
_startingUpToDateComponentStore.RegisterComponentType<TComponent>();
|
|
||||||
|
internal void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
|
||||||
|
{
|
||||||
|
if (!typeToIndex.ContainsKey(typeof(TComponent)))
|
||||||
|
{
|
||||||
|
typeToIndex.Add(typeof(TComponent), typeToIndex.Count);
|
||||||
|
componentTypesToPreload.Add(typeof(TComponent));
|
||||||
|
componentManager.RegisterComponentType<TComponent>();
|
||||||
|
startingExistingComponentStore.RegisterComponentType<TComponent>();
|
||||||
|
startingUpToDateComponentStore.RegisterComponentType<TComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void RegisterMessageTypes(IEnumerable<Type> types)
|
internal void RegisterMessageTypes(IEnumerable<Type> types)
|
||||||
{
|
{
|
||||||
_messageTypes.UnionWith(types);
|
messageTypes.UnionWith(types);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -113,151 +120,109 @@ namespace Encompass
|
||||||
/// <param name="engine">An instance of an Engine.</param>
|
/// <param name="engine">An instance of an Engine.</param>
|
||||||
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
|
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
|
||||||
{
|
{
|
||||||
engine.AssignEntityManager(_entityManager);
|
engine.AssignEntityManager(entityManager);
|
||||||
engine.AssignComponentManager(_componentManager);
|
engine.AssignComponentManager(componentManager);
|
||||||
engine.AssignMessageManager(_messageManager);
|
engine.AssignMessageManager(messageManager);
|
||||||
engine.AssignTimeManager(_timeManager);
|
engine.AssignTimeManager(timeManager);
|
||||||
engine.AssignTrackingManager(_trackingManager);
|
engine.AssignTrackingManager(trackingManager);
|
||||||
|
|
||||||
_engines.Add(engine);
|
engines.Add(engine);
|
||||||
_engineGraph.AddNode(engine);
|
engineGraph.AddNode(engine);
|
||||||
|
|
||||||
var messageReceiveTypes = engine.ReceiveTypes;
|
var messageReceiveTypes = engine.receiveTypes;
|
||||||
var messageSendTypes = engine.SendTypes;
|
var messageSendTypes = engine.sendTypes;
|
||||||
|
|
||||||
RegisterMessageTypes(engine.ReceiveTypes);
|
RegisterMessageTypes(engine.receiveTypes.Union(engine.sendTypes));
|
||||||
RegisterMessageTypes(engine.SendTypes);
|
|
||||||
|
|
||||||
foreach (var writeImmediateType in engine.WriteImmediateTypes)
|
foreach (var writeImmediateType in engine.writeImmediateTypes.Intersect(engine.readImmediateTypes))
|
||||||
{
|
{
|
||||||
foreach (var readImmediateType in engine.ReadImmediateTypes)
|
throw new EngineSelfCycleException("Engine {0} both writes and reads immediate Component {1}", engine.GetType().Name, writeImmediateType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var messageType in messageReceiveTypes.Intersect(messageSendTypes))
|
||||||
|
{
|
||||||
|
throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageType.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messageSendTypes.Count > 0 || engine.writeImmediateTypes.Count > 0)
|
||||||
|
{
|
||||||
|
senders.Add(engine);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var componentType in engine.queryWithTypes.Union(engine.queryWithoutTypes))
|
||||||
|
{
|
||||||
|
trackingManager.RegisterComponentTypeToEngine(componentType, engine);
|
||||||
|
if (engine.readImmediateTypes.Contains(componentType))
|
||||||
{
|
{
|
||||||
if (readImmediateType == writeImmediateType)
|
trackingManager.RegisterImmediateComponentTypeToEngine(componentType, engine);
|
||||||
{
|
|
||||||
throw new EngineSelfCycleException("Engine {0} both writes and reads immediate Component {1}", engine.GetType().Name, writeImmediateType.Name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var messageReceiveType in messageReceiveTypes)
|
foreach (var receiveType in engine.receiveTypes.Union(engine.readImmediateTypes))
|
||||||
{
|
{
|
||||||
foreach (var messageSendType in messageSendTypes)
|
if (!typeToReaders.ContainsKey(receiveType))
|
||||||
{
|
{
|
||||||
if (messageReceiveType == messageSendType)
|
typeToReaders.Add(receiveType, new HashSet<Engine>());
|
||||||
{
|
|
||||||
throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageReceiveType.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (messageSendTypes.Count > 0 || engine.WriteImmediateTypes.Count > 0)
|
|
||||||
{
|
|
||||||
_senders.Add(engine);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentType in engine.ReadTypes)
|
|
||||||
{
|
|
||||||
RegisterComponentTypeNonGeneric(componentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentType in engine.AddTypes)
|
|
||||||
{
|
|
||||||
RegisterComponentTypeNonGeneric(componentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentType in engine.WriteTypes)
|
|
||||||
{
|
|
||||||
RegisterComponentTypeNonGeneric(componentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentType in engine.WriteImmediateTypes)
|
|
||||||
{
|
|
||||||
RegisterComponentTypeNonGeneric(componentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentType in engine.QueryWithTypes)
|
|
||||||
{
|
|
||||||
_trackingManager.RegisterComponentTypeToEngine(componentType, engine);
|
|
||||||
if (engine.ReadImmediateTypes.Contains(componentType))
|
|
||||||
{
|
|
||||||
_trackingManager.RegisterImmediateComponentTypeToEngine(componentType, engine);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var componentType in engine.QueryWithoutTypes)
|
|
||||||
{
|
|
||||||
_trackingManager.RegisterComponentTypeToEngine(componentType, engine);
|
|
||||||
if (engine.ReadImmediateTypes.Contains(componentType))
|
|
||||||
{
|
|
||||||
_trackingManager.RegisterImmediateComponentTypeToEngine(componentType, engine);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var receiveType in engine.ReceiveTypes)
|
|
||||||
{
|
|
||||||
if (!_typeToReaders.ContainsKey(receiveType))
|
|
||||||
{
|
|
||||||
_typeToReaders.Add(receiveType, new HashSet<Engine>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_typeToReaders[receiveType].Add(engine);
|
typeToReaders[receiveType].Add(engine);
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var receiveType in engine.ReadImmediateTypes)
|
|
||||||
{
|
|
||||||
if (!_typeToReaders.ContainsKey(receiveType))
|
|
||||||
{
|
|
||||||
_typeToReaders.Add(receiveType, new HashSet<Engine>());
|
|
||||||
}
|
|
||||||
|
|
||||||
_typeToReaders[receiveType].Add(engine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified Renderer to the World.
|
/// Registers a draw layer. This must be done before any Renderers are registered.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddRenderer(Renderer renderer)
|
/// <param name="layer">The draw layer to register.</param>
|
||||||
|
public void RegisterDrawLayer(int layer)
|
||||||
{
|
{
|
||||||
renderer.AssignComponentManager(_componentManager);
|
drawLayerManager.RegisterDrawLayer(layer);
|
||||||
renderer.AssignEntityManager(_entityManager);
|
}
|
||||||
_renderManager.AddRenderer(renderer);
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the specified OrderedRenderer to the World.
|
||||||
|
/// </summary>
|
||||||
|
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
|
||||||
|
{
|
||||||
|
RegisterComponentType<TComponent>();
|
||||||
|
renderer.AssignEntityManager(entityManager);
|
||||||
|
renderer.AssignComponentManager(componentManager);
|
||||||
|
renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
|
||||||
|
return renderer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the specified GeneralRenderer to the World at the specified layer.
|
||||||
|
/// Higher layer numbers draw on top of lower layer numbers.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="renderer">An instance of a GeneralRenderer.</param>
|
||||||
|
/// <param name="layer">The layer at which the GeneralRenderer should render. Higher numbers draw over lower numbers.</param>
|
||||||
|
public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer
|
||||||
|
{
|
||||||
|
renderer.AssignEntityManager(entityManager);
|
||||||
|
renderer.AssignComponentManager(componentManager);
|
||||||
|
|
||||||
|
renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
|
||||||
|
|
||||||
|
return renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BuildEngineGraph()
|
private void BuildEngineGraph()
|
||||||
{
|
{
|
||||||
foreach (var senderEngine in _senders)
|
foreach (var senderEngine in senders)
|
||||||
{
|
{
|
||||||
foreach (var messageType in senderEngine.SendTypes)
|
foreach (var messageType in senderEngine.sendTypes.Union(senderEngine.writeImmediateTypes))
|
||||||
{
|
{
|
||||||
if (_typeToReaders.ContainsKey(messageType))
|
if (typeToReaders.ContainsKey(messageType))
|
||||||
{
|
{
|
||||||
foreach (var readerEngine in _typeToReaders[messageType])
|
foreach (var readerEngine in typeToReaders[messageType])
|
||||||
{
|
{
|
||||||
if (senderEngine != readerEngine)
|
if (senderEngine != readerEngine)
|
||||||
{
|
{
|
||||||
if (!_engineGraph.Exists(senderEngine, readerEngine))
|
if (!engineGraph.Exists(senderEngine, readerEngine))
|
||||||
{
|
{
|
||||||
_engineGraph.AddEdge(senderEngine, readerEngine);
|
engineGraph.AddEdge(senderEngine, readerEngine);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var messageType in senderEngine.WriteImmediateTypes)
|
|
||||||
{
|
|
||||||
if (_typeToReaders.ContainsKey(messageType))
|
|
||||||
{
|
|
||||||
foreach (var readerEngine in _typeToReaders[messageType])
|
|
||||||
{
|
|
||||||
if (senderEngine != readerEngine)
|
|
||||||
{
|
|
||||||
if (!_engineGraph.Exists(senderEngine, readerEngine))
|
|
||||||
{
|
|
||||||
_engineGraph.AddEdge(senderEngine, readerEngine);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,9 +240,9 @@ namespace Encompass
|
||||||
{
|
{
|
||||||
BuildEngineGraph();
|
BuildEngineGraph();
|
||||||
|
|
||||||
if (_engineGraph.Cyclic())
|
if (engineGraph.Cyclic())
|
||||||
{
|
{
|
||||||
var cycles = _engineGraph.SimpleCycles();
|
var cycles = engineGraph.SimpleCycles();
|
||||||
var errorString = "Cycle(s) found in Engines: ";
|
var errorString = "Cycle(s) found in Engines: ";
|
||||||
foreach (var cycle in cycles)
|
foreach (var cycle in cycles)
|
||||||
{
|
{
|
||||||
|
@ -297,25 +262,25 @@ namespace Encompass
|
||||||
var writePriorities = new Dictionary<Type, HashSet<int>>();
|
var writePriorities = new Dictionary<Type, HashSet<int>>();
|
||||||
var writeMessageToEngines = new Dictionary<Type, List<Engine>>();
|
var writeMessageToEngines = new Dictionary<Type, List<Engine>>();
|
||||||
|
|
||||||
foreach (var engine in _engines)
|
foreach (var engine in engines)
|
||||||
{
|
{
|
||||||
if (engine.GetType().GetCustomAttribute<IgnoresTimeDilation>() != null)
|
if (engine.GetType().GetCustomAttribute<IgnoresTimeDilation>() != null)
|
||||||
{
|
{
|
||||||
engine._usesTimeDilation = false;
|
engine.usesTimeDilation = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultWritePriorityAttribute = engine.GetType().GetCustomAttribute<DefaultWritePriority>(false);
|
var defaultWritePriorityAttribute = engine.GetType().GetCustomAttribute<DefaultWritePriority>(false);
|
||||||
|
|
||||||
foreach (var writeType in engine.WriteTypes)
|
foreach (var writeType in engine.writeTypes)
|
||||||
{
|
{
|
||||||
int? priority = null;
|
int? priority = null;
|
||||||
if (engine.WritePriorities.ContainsKey(writeType))
|
if (engine.writePriorities.ContainsKey(writeType))
|
||||||
{
|
{
|
||||||
priority = engine.WritePriorities[writeType];
|
priority = engine.writePriorities[writeType];
|
||||||
}
|
}
|
||||||
else if (defaultWritePriorityAttribute != null)
|
else if (defaultWritePriorityAttribute != null)
|
||||||
{
|
{
|
||||||
priority = defaultWritePriorityAttribute.WritePriority;
|
priority = defaultWritePriorityAttribute.writePriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (priority.HasValue)
|
if (priority.HasValue)
|
||||||
|
@ -389,11 +354,25 @@ namespace Encompass
|
||||||
throw new EngineWriteConflictException(errorString);
|
throw new EngineWriteConflictException(errorString);
|
||||||
}
|
}
|
||||||
|
|
||||||
PreloadJIT(_messageTypes);
|
// doing reflection to grab all component types, because not all writes need to be declared
|
||||||
|
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||||
|
{
|
||||||
|
foreach (var componentType in assembly.GetTypes())
|
||||||
|
{
|
||||||
|
if (typeof(IComponent).IsAssignableFrom(componentType) && componentType.IsValueType && !componentType.IsEnum && !componentType.IsPrimitive)
|
||||||
|
{
|
||||||
|
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
var generic = method.MakeGenericMethod(componentType);
|
||||||
|
generic.Invoke(this, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PreloadJIT(componentTypesToPreload, messageTypes);
|
||||||
|
|
||||||
var engineOrder = new List<Engine>();
|
var engineOrder = new List<Engine>();
|
||||||
|
|
||||||
foreach (var engine in _engineGraph.TopologicalSort())
|
foreach (var engine in engineGraph.TopologicalSort())
|
||||||
{
|
{
|
||||||
engineOrder.Add(engine);
|
engineOrder.Add(engine);
|
||||||
engine.BuildEntityQuery();
|
engine.BuildEntityQuery();
|
||||||
|
@ -401,18 +380,18 @@ namespace Encompass
|
||||||
|
|
||||||
var world = new World(
|
var world = new World(
|
||||||
engineOrder,
|
engineOrder,
|
||||||
_entityManager,
|
entityManager,
|
||||||
_componentManager,
|
componentManager,
|
||||||
_trackingManager,
|
trackingManager,
|
||||||
_messageManager,
|
messageManager,
|
||||||
_timeManager,
|
timeManager,
|
||||||
_renderManager
|
renderManager
|
||||||
);
|
);
|
||||||
|
|
||||||
_componentManager.SetExistingComponentStore(_startingExistingComponentStore);
|
componentManager.SetExistingComponentStore(startingExistingComponentStore);
|
||||||
_componentManager.SetUpToDateComponentStore(_startingUpToDateComponentStore);
|
componentManager.SetUpToDateComponentStore(startingUpToDateComponentStore);
|
||||||
|
|
||||||
_trackingManager.InitializeTracking(_entityManager.EntityIDs);
|
trackingManager.InitializeTracking(entityManager.EntityIDs);
|
||||||
|
|
||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
|
@ -423,25 +402,19 @@ namespace Encompass
|
||||||
/// It does so by grabbing all component and message types known to the WorldBuilder and
|
/// It does so by grabbing all component and message types known to the WorldBuilder and
|
||||||
/// executing every possible generic method that could be executed with those types.
|
/// executing every possible generic method that could be executed with those types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void PreloadJIT(IEnumerable<Type> messageTypes)
|
private void PreloadJIT(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
|
||||||
{
|
{
|
||||||
var dummyTimeManager = new TimeManager();
|
var dummyTimeManager = new TimeManager();
|
||||||
var dummyMessageManager = new MessageManager(dummyTimeManager);
|
var dummyMessageManager = new MessageManager(dummyTimeManager);
|
||||||
|
var dummyDrawLayerManager = new DrawLayerManager(typeToIndex);
|
||||||
var dummyTrackingManager = new TrackingManager();
|
var dummyTrackingManager = new TrackingManager();
|
||||||
var dummyComponentManager = new ComponentManager(_typeToIndex);
|
var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, typeToIndex);
|
||||||
var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);
|
var dummyEntityManager = new EntityManager(dummyComponentManager, entityCapacity);
|
||||||
var dummyRenderManager = new RenderManager(dummyEntityManager);
|
var dummyRenderManager = new RenderManager(dummyEntityManager, dummyDrawLayerManager);
|
||||||
|
|
||||||
var prepEngineOrder = new List<Engine>();
|
var prepEngineOrder = new List<Engine>();
|
||||||
|
|
||||||
foreach (var componentType in _componentTypesToPreload)
|
var uberEngine = new UberEngine(componentTypes, messageTypes);
|
||||||
{
|
|
||||||
var dummyRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType", BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
var dummyGeneric = dummyRegisterMethod.MakeGenericMethod(componentType);
|
|
||||||
dummyGeneric.Invoke(dummyComponentManager, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
var uberEngine = new UberEngine(_componentTypesToPreload, messageTypes);
|
|
||||||
|
|
||||||
uberEngine.AssignEntityManager(dummyEntityManager);
|
uberEngine.AssignEntityManager(dummyEntityManager);
|
||||||
uberEngine.AssignComponentManager(dummyComponentManager);
|
uberEngine.AssignComponentManager(dummyComponentManager);
|
||||||
|
@ -449,10 +422,24 @@ namespace Encompass
|
||||||
uberEngine.AssignTimeManager(dummyTimeManager);
|
uberEngine.AssignTimeManager(dummyTimeManager);
|
||||||
uberEngine.AssignTrackingManager(dummyTrackingManager);
|
uberEngine.AssignTrackingManager(dummyTrackingManager);
|
||||||
|
|
||||||
var uberRenderer = new UberRenderer(_componentTypesToPreload);
|
var uberRenderer = new UberRenderer(componentTypes);
|
||||||
uberRenderer.AssignComponentManager(dummyComponentManager);
|
uberRenderer.AssignComponentManager(dummyComponentManager);
|
||||||
uberRenderer.AssignEntityManager(dummyEntityManager);
|
uberRenderer.AssignEntityManager(dummyEntityManager);
|
||||||
|
|
||||||
|
foreach (var type in componentTypes)
|
||||||
|
{
|
||||||
|
var componentManagerRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType");
|
||||||
|
var componentManagerRegisterGenericMethod = componentManagerRegisterMethod.MakeGenericMethod(type);
|
||||||
|
componentManagerRegisterGenericMethod.Invoke(dummyComponentManager, null);
|
||||||
|
|
||||||
|
if (type.GetInterface("IDrawableComponent") != null)
|
||||||
|
{
|
||||||
|
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
|
||||||
|
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(type);
|
||||||
|
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
prepEngineOrder.Add(uberEngine);
|
prepEngineOrder.Add(uberEngine);
|
||||||
|
|
||||||
var dummyWorld = new World(
|
var dummyWorld = new World(
|
||||||
|
@ -474,7 +461,7 @@ namespace Encompass
|
||||||
dummyComponentManager.WriteComponents();
|
dummyComponentManager.WriteComponents();
|
||||||
|
|
||||||
uberRenderer.SetEntity(uberEngine.Entity);
|
uberRenderer.SetEntity(uberEngine.Entity);
|
||||||
uberRenderer.Render(1, 0);
|
uberRenderer.Render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,20 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<LangVersion>8.0</LangVersion>
|
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
|
||||||
<RootNamespace>Encompass</RootNamespace>
|
<RootNamespace>Encompass</RootNamespace>
|
||||||
<PackageId>EncompassECS.Framework</PackageId>
|
<PackageId>EncompassECS.Framework</PackageId>
|
||||||
<Version>0.23.0</Version>
|
<Version>0.20.0</Version>
|
||||||
<Authors>Evan Hemsley</Authors>
|
<Authors>Evan Hemsley</Authors>
|
||||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||||
<Company>Moonside Games</Company>
|
<Company>Moonside Games</Company>
|
||||||
<Product>Encompass ECS</Product>
|
<Product>Encompass ECS</Product>
|
||||||
<PackageProjectUrl>https://github.com/encompass-ecs</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/encompass-ecs</PackageProjectUrl>
|
||||||
<PackageLicenseUrl />
|
<PackageLicenseUrl />
|
||||||
<Copyright>Evan Hemsley 2020</Copyright>
|
<Copyright>Evan Hemsley 2019</Copyright>
|
||||||
<Description>Encompass is an engine-agnostic MECS framework to help you code games, or other kinds of simulations.</Description>
|
<Description>Encompass is an engine-agnostic Hyper ECS framework to help you code games, or other kinds of simulations.</Description>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<AssemblyName>EncompassECS.Framework</AssemblyName>
|
<AssemblyName>EncompassECS.Framework</AssemblyName>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<Platforms>x64;x86</Platforms>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\LICENSE">
|
<None Include="..\LICENSE">
|
||||||
|
@ -26,7 +23,8 @@
|
||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="MoonTools.Core.Graph" Version="1.0.0" />
|
||||||
|
<PackageReference Include="MoonTools.FastCollections" Version="1.0.0" />
|
||||||
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
|
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
|
||||||
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -2,18 +2,18 @@ using NUnit.Framework;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
|
||||||
using Encompass;
|
using Encompass;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Tests
|
namespace Tests
|
||||||
{
|
{
|
||||||
public class ComponentTests
|
public class ComponentTests
|
||||||
{
|
{
|
||||||
struct MockComponent
|
struct MockComponent : Encompass.IComponent
|
||||||
{
|
{
|
||||||
|
public string myString;
|
||||||
public int myInt;
|
public int myInt;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EntityMessage
|
struct EntityMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,14 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entityMessage in ReadMessages<EntityMessage>())
|
foreach (var entityMessage in ReadMessages<EntityMessage>())
|
||||||
{
|
{
|
||||||
gottenMockComponent = GetComponent<MockComponent>(entityMessage.entity);
|
gottenMockComponent = GetComponent<MockComponent>(entityMessage.entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AddComponentTestMessage
|
struct AddComponentTestMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
public MockComponent mockComponent;
|
public MockComponent mockComponent;
|
||||||
|
@ -45,17 +45,16 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
|
foreach (var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
|
||||||
{
|
{
|
||||||
Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity));
|
Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity));
|
||||||
ref readonly var gottenComponent = ref GetComponent<MockComponent>(addComponentTestMessage.entity);
|
Assert.That(GetComponent<MockComponent>(addComponentTestMessage.entity), Is.EqualTo(addComponentTestMessage.mockComponent));
|
||||||
gottenComponent.Should().BeEquivalentTo(addComponentTestMessage.mockComponent);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public unsafe void AddComponent()
|
public void AddComponent()
|
||||||
{
|
{
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new AddComponentTestEngine());
|
worldBuilder.AddEngine(new AddComponentTestEngine());
|
||||||
|
@ -64,6 +63,7 @@ namespace Tests
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 3;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -85,15 +85,16 @@ namespace Tests
|
||||||
worldBuilder.AddEngine(new ReadMockComponentEngine());
|
worldBuilder.AddEngine(new ReadMockComponentEngine());
|
||||||
|
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
worldBuilder.SetComponent(entity, new MockComponent { myInt = 20 });
|
worldBuilder.SetComponent(entity, new MockComponent { myInt = 20, myString = "what" });
|
||||||
worldBuilder.SetComponent(entity, new MockComponent { myInt = 50 });
|
worldBuilder.SetComponent(entity, new MockComponent { myInt = 50, myString = "hi" });
|
||||||
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40 });
|
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40, myString = "wassup" });
|
||||||
|
|
||||||
var world = worldBuilder.Build();
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
world.Update(0.01);
|
world.Update(0.01);
|
||||||
|
|
||||||
Assert.That(gottenMockComponent.myInt, Is.EqualTo(40));
|
Assert.That(gottenMockComponent.myInt, Is.EqualTo(40));
|
||||||
|
Assert.That(gottenMockComponent.myString, Is.EqualTo("wassup"));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
|
@ -102,9 +103,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (mockComponent, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
|
|
||||||
SetComponent(entity, new MockComponent { myInt = mockComponent.myInt + 1 });
|
SetComponent(entity, new MockComponent { myInt = mockComponent.myInt + 1 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,9 +149,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (mockComponent, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
|
|
||||||
SetComponent(entity, mockComponent);
|
SetComponent(entity, mockComponent);
|
||||||
RemoveComponent<MockComponent>(entity);
|
RemoveComponent<MockComponent>(entity);
|
||||||
}
|
}
|
||||||
|
@ -172,7 +171,7 @@ namespace Tests
|
||||||
Assert.DoesNotThrow(() => world.Update(0.01));
|
Assert.DoesNotThrow(() => world.Update(0.01));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AddMockComponentMessage
|
struct AddMockComponentMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
public MockComponent mockComponent;
|
public MockComponent mockComponent;
|
||||||
|
@ -192,6 +191,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 10;
|
mockComponent.myInt = 10;
|
||||||
|
mockComponent.myString = "four";
|
||||||
|
|
||||||
AddMockComponentMessage addMockComponentMessage;
|
AddMockComponentMessage addMockComponentMessage;
|
||||||
addMockComponentMessage.entity = entity;
|
addMockComponentMessage.entity = entity;
|
||||||
|
@ -207,7 +207,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var message in ReadMessages<AddMockComponentMessage>())
|
foreach (var message in ReadMessages<AddMockComponentMessage>())
|
||||||
{
|
{
|
||||||
SetComponent(message.entity, message.mockComponent);
|
SetComponent(message.entity, message.mockComponent);
|
||||||
}
|
}
|
||||||
|
@ -255,8 +255,9 @@ namespace Tests
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 3;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent<MockComponent>(entity, mockComponent);
|
||||||
|
|
||||||
EntityMessage entityMessage;
|
EntityMessage entityMessage;
|
||||||
entityMessage.entity = entity;
|
entityMessage.entity = entity;
|
||||||
|
@ -269,7 +270,7 @@ namespace Tests
|
||||||
|
|
||||||
Assert.AreEqual(mockComponent, gottenMockComponent);
|
Assert.AreEqual(mockComponent, gottenMockComponent);
|
||||||
}
|
}
|
||||||
struct HasComponentTestMessage
|
struct HasComponentTestMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
}
|
}
|
||||||
|
@ -280,7 +281,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
|
foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
|
||||||
{
|
{
|
||||||
Assert.IsTrue(HasComponent<MockComponent>(hasComponentTestEngine.entity));
|
Assert.IsTrue(HasComponent<MockComponent>(hasComponentTestEngine.entity));
|
||||||
}
|
}
|
||||||
|
@ -297,6 +298,7 @@ namespace Tests
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 3;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -317,7 +319,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
|
foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
|
||||||
{
|
{
|
||||||
hasComponentRuntimeTypeResult = HasComponent(hasComponentTestEngine.entity, typeof(MockComponent));
|
hasComponentRuntimeTypeResult = HasComponent(hasComponentTestEngine.entity, typeof(MockComponent));
|
||||||
}
|
}
|
||||||
|
@ -334,6 +336,7 @@ namespace Tests
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 3;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -367,7 +370,7 @@ namespace Tests
|
||||||
Assert.IsFalse(hasComponentRuntimeTypeResult);
|
Assert.IsFalse(hasComponentRuntimeTypeResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RemoveComponentTestMessage
|
struct RemoveComponentTestMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
}
|
}
|
||||||
|
@ -379,7 +382,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var removeComponentMessage in ReadMessages<RemoveComponentTestMessage>())
|
foreach (var removeComponentMessage in ReadMessages<RemoveComponentTestMessage>())
|
||||||
{
|
{
|
||||||
RemoveComponent<MockComponent>(removeComponentMessage.entity);
|
RemoveComponent<MockComponent>(removeComponentMessage.entity);
|
||||||
}
|
}
|
||||||
|
@ -424,7 +427,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
|
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
|
||||||
{
|
{
|
||||||
hasComponentResult = HasComponent<MockComponent>(checkHasMockComponentMessage.entity);
|
hasComponentResult = HasComponent<MockComponent>(checkHasMockComponentMessage.entity);
|
||||||
}
|
}
|
||||||
|
@ -443,6 +446,7 @@ namespace Tests
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 3;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -461,7 +465,7 @@ namespace Tests
|
||||||
hasComponentResult.Should().BeFalse();
|
hasComponentResult.Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CheckHasMockComponentMessage
|
struct CheckHasMockComponentMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
public bool shouldHaveComponent;
|
public bool shouldHaveComponent;
|
||||||
|
@ -473,7 +477,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
|
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
|
||||||
{
|
{
|
||||||
Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
|
Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,29 +10,29 @@ using Encompass.Exceptions;
|
||||||
|
|
||||||
namespace Tests
|
namespace Tests
|
||||||
{
|
{
|
||||||
struct MockComponent
|
struct MockComponent : IComponent
|
||||||
{
|
{
|
||||||
public int myInt;
|
public int myInt;
|
||||||
|
public string myString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class EngineTest
|
public class EngineTest
|
||||||
{
|
{
|
||||||
|
static List<MockComponent> resultComponents;
|
||||||
static MockComponent resultComponent;
|
static MockComponent resultComponent;
|
||||||
static MockComponent[] resultComponents = new MockComponent[1];
|
|
||||||
|
|
||||||
static MockMessage resultMessage;
|
static List<MockMessage> resultMessages = new List<MockMessage>();
|
||||||
static MockMessage[] resultMessages = new MockMessage[1];
|
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
public class ReadComponentsTestEngine : Engine
|
public class ReadComponentsTestEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
resultComponents = ReadComponents<MockComponent>().ToArray();
|
resultComponents = ReadComponents<MockComponent>().ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<(MockComponent, Entity)> resultComponentsIncludingEntity = new List<(MockComponent, Entity)>();
|
static List<(MockComponent, Entity)> resultComponentsIncludingEntity;
|
||||||
static (MockComponent, Entity) resultComponentIncludingEntity;
|
static (MockComponent, Entity) resultComponentIncludingEntity;
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
|
@ -40,11 +40,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
resultComponentsIncludingEntity = ReadComponentsIncludingEntity<MockComponent>().ToList();
|
||||||
{
|
|
||||||
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
|
|
||||||
resultComponentsIncludingEntity.Add((mockComponent, entity));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,9 +58,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ref readonly var entity = ref ReadEntity<MockComponent>();
|
resultComponentIncludingEntity = ReadComponentIncludingEntity<MockComponent>();
|
||||||
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
|
|
||||||
resultComponentIncludingEntity = (mockComponent, entity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,10 +72,12 @@ namespace Tests
|
||||||
var entityB = worldBuilder.CreateEntity();
|
var entityB = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 2;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
MockComponent mockComponentB;
|
MockComponent mockComponentB;
|
||||||
mockComponentB.myInt = 1;
|
mockComponentB.myInt = 1;
|
||||||
|
mockComponentB.myString = "howdy";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
worldBuilder.SetComponent(entityB, mockComponentB);
|
worldBuilder.SetComponent(entityB, mockComponentB);
|
||||||
|
@ -97,8 +93,6 @@ namespace Tests
|
||||||
[Test]
|
[Test]
|
||||||
public void ReadComponentsIncludingEntity()
|
public void ReadComponentsIncludingEntity()
|
||||||
{
|
{
|
||||||
resultComponentsIncludingEntity.Clear();
|
|
||||||
|
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new ReadComponentsIncludingEntityEngine());
|
worldBuilder.AddEngine(new ReadComponentsIncludingEntityEngine());
|
||||||
|
|
||||||
|
@ -106,10 +100,12 @@ namespace Tests
|
||||||
var entityB = worldBuilder.CreateEntity();
|
var entityB = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 2;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
MockComponent mockComponentB;
|
MockComponent mockComponentB;
|
||||||
mockComponentB.myInt = 1;
|
mockComponentB.myInt = 1;
|
||||||
|
mockComponentB.myString = "howdy";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
worldBuilder.SetComponent(entityB, mockComponentB);
|
worldBuilder.SetComponent(entityB, mockComponentB);
|
||||||
|
@ -139,7 +135,8 @@ namespace Tests
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -150,17 +147,6 @@ namespace Tests
|
||||||
Assert.AreEqual(mockComponent, resultComponent);
|
Assert.AreEqual(mockComponent, resultComponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ReadComponentThrowsWhenNoneExist()
|
|
||||||
{
|
|
||||||
var worldBuilder = new WorldBuilder();
|
|
||||||
worldBuilder.AddEngine(new ReadComponentTestEngine());
|
|
||||||
|
|
||||||
var world = worldBuilder.Build();
|
|
||||||
|
|
||||||
Assert.Throws<NoComponentOfTypeException>(() => world.Update(0.01f), "No component of type MockComponent exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ReadComponentWhenMultipleComponents()
|
public void ReadComponentWhenMultipleComponents()
|
||||||
{
|
{
|
||||||
|
@ -171,10 +157,12 @@ namespace Tests
|
||||||
var entityB = worldBuilder.CreateEntity();
|
var entityB = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 2;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
MockComponent mockComponentB;
|
MockComponent mockComponentB;
|
||||||
mockComponentB.myInt = 1;
|
mockComponentB.myInt = 1;
|
||||||
|
mockComponentB.myString = "howdy";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
worldBuilder.SetComponent(entityB, mockComponentB);
|
worldBuilder.SetComponent(entityB, mockComponentB);
|
||||||
|
@ -195,7 +183,8 @@ namespace Tests
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 2;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -212,8 +201,11 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ref readonly var entity = ref ReadEntity<MockComponent>();
|
var (component, entity) = ReadComponentIncludingEntity<MockComponent>();
|
||||||
SetComponent(entity, new MockComponent { myInt = 420 });
|
|
||||||
|
component.myInt = 420;
|
||||||
|
component.myString = "blaze it";
|
||||||
|
SetComponent(entity, component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,7 +222,8 @@ namespace Tests
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -240,6 +233,7 @@ namespace Tests
|
||||||
world.Update(0.01);
|
world.Update(0.01);
|
||||||
|
|
||||||
Assert.AreEqual(420, resultComponent.myInt);
|
Assert.AreEqual(420, resultComponent.myInt);
|
||||||
|
Assert.AreEqual("blaze it", resultComponent.myString);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
|
@ -247,8 +241,13 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ref readonly var entity = ref ReadEntity<MockComponent>();
|
var (component, entity) = ReadComponentIncludingEntity<MockComponent>();
|
||||||
SetComponent(entity, new MockComponent { myInt = 420 });
|
|
||||||
|
component.myInt = 420;
|
||||||
|
component.myString = "blaze it";
|
||||||
|
SetComponent(entity, component);
|
||||||
|
|
||||||
|
component = ReadComponent<MockComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,7 +260,8 @@ namespace Tests
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 3;
|
mockComponent.myInt = 0;
|
||||||
|
mockComponent.myString = "hello";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
|
||||||
|
@ -271,7 +271,7 @@ namespace Tests
|
||||||
Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to update undeclared Component MockComponent"));
|
Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to update undeclared Component MockComponent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MockMessage
|
struct MockMessage : IMessage
|
||||||
{
|
{
|
||||||
public string myString;
|
public string myString;
|
||||||
}
|
}
|
||||||
|
@ -289,20 +289,11 @@ namespace Tests
|
||||||
}
|
}
|
||||||
|
|
||||||
[Receives(typeof(MockMessage))]
|
[Receives(typeof(MockMessage))]
|
||||||
public class ReadMessagesEngine : Engine
|
public class MessageReadEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
resultMessages = ReadMessages<MockMessage>().ToArray();
|
resultMessages = this.ReadMessages<MockMessage>().ToList();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Receives(typeof(MockMessage))]
|
|
||||||
public class ReadMessageEngine : Engine
|
|
||||||
{
|
|
||||||
public override void Update(double dt)
|
|
||||||
{
|
|
||||||
resultMessage = ReadMessage<MockMessage>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,24 +302,13 @@ namespace Tests
|
||||||
{
|
{
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new MessageEmitEngine());
|
worldBuilder.AddEngine(new MessageEmitEngine());
|
||||||
worldBuilder.AddEngine(new ReadMessageEngine());
|
worldBuilder.AddEngine(new MessageReadEngine());
|
||||||
|
|
||||||
var world = worldBuilder.Build();
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
world.Update(0.01f);
|
world.Update(0.01f);
|
||||||
|
|
||||||
Assert.AreEqual(resultMessage.myString, "howdy");
|
Assert.AreEqual(resultMessages.First().myString, "howdy");
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ReadMessageThrowsWhenNoneOfTypeExist()
|
|
||||||
{
|
|
||||||
var worldBuilder = new WorldBuilder();
|
|
||||||
worldBuilder.AddEngine(new ReadMessageEngine());
|
|
||||||
|
|
||||||
var world = worldBuilder.Build();
|
|
||||||
|
|
||||||
Assert.Throws<NoMessageOfTypeException>(() => world.Update(0.01), "No Message of type MockMessage exists");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UndeclaredMessageEmitEngine : Engine
|
public class UndeclaredMessageEmitEngine : Engine
|
||||||
|
@ -342,14 +322,14 @@ namespace Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static MockMessage[] emptyReadMessagesResult;
|
static IEnumerable<MockMessage> emptyReadMessagesResult;
|
||||||
|
|
||||||
[Receives(typeof(MockMessage))]
|
[Receives(typeof(MockMessage))]
|
||||||
class ReadMessagesWhenNoneExistEngine : Engine
|
class ReadMessagesWhenNoneExistEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
emptyReadMessagesResult = ReadMessages<MockMessage>().ToArray();
|
emptyReadMessagesResult = ReadMessages<MockMessage>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,7 +415,7 @@ namespace Tests
|
||||||
Assert.Throws<IllegalReadException>(() => world.Update(0.01f));
|
Assert.Throws<IllegalReadException>(() => world.Update(0.01f));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EntityMessage : IHasEntity
|
struct EntityMessage : IMessage, IHasEntity
|
||||||
{
|
{
|
||||||
public EntityMessage(Entity entity, int myInt)
|
public EntityMessage(Entity entity, int myInt)
|
||||||
{
|
{
|
||||||
|
@ -598,10 +578,10 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
var entities = ReadEntities<MockComponent>();
|
var components = ReadComponentsIncludingEntity<MockComponent>();
|
||||||
|
|
||||||
pairA = (GetComponent<MockComponent>(entities[0]), entities[0]);
|
pairA = components.First();
|
||||||
pairB = (GetComponent<MockComponent>(entities[1]), entities[1]);
|
pairB = components.Last();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -614,9 +594,11 @@ namespace Tests
|
||||||
|
|
||||||
MockComponent componentA;
|
MockComponent componentA;
|
||||||
componentA.myInt = 20;
|
componentA.myInt = 20;
|
||||||
|
componentA.myString = "hello";
|
||||||
|
|
||||||
MockComponent componentB;
|
MockComponent componentB;
|
||||||
componentB.myInt = 20;
|
componentB.myInt = 20;
|
||||||
|
componentB.myString = "hello";
|
||||||
|
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
worldBuilder.SetComponent(entity, componentA);
|
worldBuilder.SetComponent(entity, componentA);
|
||||||
|
@ -631,12 +613,14 @@ namespace Tests
|
||||||
Assert.That(EngineTest.pairA.Item1, Is.EqualTo(EngineTest.pairB.Item1));
|
Assert.That(EngineTest.pairA.Item1, Is.EqualTo(EngineTest.pairB.Item1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static IEnumerable<(MockComponent, Entity)> emptyComponentReadResult;
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
class ReadEmptyMockComponentsEngine : Engine
|
class ReadEmptyMockComponentsEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ReadEntities<MockComponent>().ToArray().Should().BeEmpty();
|
emptyComponentReadResult = ReadComponentsIncludingEntity<MockComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -648,36 +632,32 @@ namespace Tests
|
||||||
|
|
||||||
var world = worldBuilder.Build();
|
var world = worldBuilder.Build();
|
||||||
world.Update(0.01f);
|
world.Update(0.01f);
|
||||||
|
|
||||||
|
Assert.That(emptyComponentReadResult, Is.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DestroyerComponent { }
|
struct DestroyerComponent : IComponent { }
|
||||||
|
|
||||||
[Reads(typeof(DestroyerComponent))]
|
[Reads(typeof(DestroyerComponent))]
|
||||||
class DestroyerEngine : Engine
|
class DestroyerEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<DestroyerComponent>())
|
foreach (var (component, entity) in ReadComponentsIncludingEntity<DestroyerComponent>())
|
||||||
{
|
{
|
||||||
Destroy(entity);
|
Destroy(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<(MockComponent, Entity)> results = new List<(MockComponent, Entity)>();
|
static List<(MockComponent, Entity)> results;
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
class ReaderEngine : Engine
|
class ReaderEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
results.Clear();
|
results = ReadComponentsIncludingEntity<MockComponent>().ToList();
|
||||||
|
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
|
||||||
{
|
|
||||||
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
|
|
||||||
results.Add((mockComponent, entity));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -695,6 +675,7 @@ namespace Tests
|
||||||
DestroyerComponent destroyerComponent;
|
DestroyerComponent destroyerComponent;
|
||||||
MockComponent mockComponent;
|
MockComponent mockComponent;
|
||||||
mockComponent.myInt = 2;
|
mockComponent.myInt = 2;
|
||||||
|
mockComponent.myString = "blah";
|
||||||
|
|
||||||
worldBuilder.SetComponent(entity, destroyerComponent);
|
worldBuilder.SetComponent(entity, destroyerComponent);
|
||||||
worldBuilder.SetComponent(entity, mockComponent);
|
worldBuilder.SetComponent(entity, mockComponent);
|
||||||
|
@ -719,7 +700,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var message in ReadMessages<DestroyComponentMessage>())
|
foreach (var message in ReadMessages<DestroyComponentMessage>())
|
||||||
{
|
{
|
||||||
Destroy(message.entity);
|
Destroy(message.entity);
|
||||||
}
|
}
|
||||||
|
@ -729,8 +710,6 @@ namespace Tests
|
||||||
[Test]
|
[Test]
|
||||||
public void DestroyEntityWithoutID()
|
public void DestroyEntityWithoutID()
|
||||||
{
|
{
|
||||||
results.Clear();
|
|
||||||
|
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new AddComponentEngine());
|
worldBuilder.AddEngine(new AddComponentEngine());
|
||||||
worldBuilder.AddEngine(new DestroyEntityEngine());
|
worldBuilder.AddEngine(new DestroyEntityEngine());
|
||||||
|
@ -754,7 +733,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<DestroyerComponent>())
|
foreach (var (componentPair, entity) in ReadComponentsIncludingEntity<DestroyerComponent>())
|
||||||
{
|
{
|
||||||
RemoveComponent<MockComponent>(entity);
|
RemoveComponent<MockComponent>(entity);
|
||||||
Destroy(entity);
|
Destroy(entity);
|
||||||
|
@ -786,7 +765,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (mockComponent, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
RemoveComponent<MockComponent>(entity);
|
RemoveComponent<MockComponent>(entity);
|
||||||
SetComponent(entity, new MockComponent());
|
SetComponent(entity, new MockComponent());
|
||||||
|
@ -801,7 +780,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ref readonly var entity = ref ReadEntity<MockComponent>();
|
var (_, entity) = ReadComponentIncludingEntity<MockComponent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -826,7 +805,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
RemoveComponent<MockComponent>(entity);
|
RemoveComponent<MockComponent>(entity);
|
||||||
SendMessage(new MockMessage { }, 1);
|
SendMessage(new MockMessage { }, 1);
|
||||||
|
@ -837,12 +816,12 @@ namespace Tests
|
||||||
[Test]
|
[Test]
|
||||||
public void EngineSendMessageDelayed()
|
public void EngineSendMessageDelayed()
|
||||||
{
|
{
|
||||||
Array.Clear(resultMessages, 0, resultMessages.Length);
|
resultMessages.Clear();
|
||||||
|
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new ActivateTimeDilationEngine());
|
worldBuilder.AddEngine(new ActivateTimeDilationEngine());
|
||||||
worldBuilder.AddEngine(new DelayedMessageEngine());
|
worldBuilder.AddEngine(new DelayedMessageEngine());
|
||||||
worldBuilder.AddEngine(new ReadMessagesEngine());
|
worldBuilder.AddEngine(new MessageReadEngine());
|
||||||
|
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
worldBuilder.SetComponent(entity, new MockComponent { });
|
worldBuilder.SetComponent(entity, new MockComponent { });
|
||||||
|
@ -873,7 +852,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
RemoveComponent<MockComponent>(entity);
|
RemoveComponent<MockComponent>(entity);
|
||||||
SendMessageIgnoringTimeDilation(new MockMessage { }, 1);
|
SendMessageIgnoringTimeDilation(new MockMessage { }, 1);
|
||||||
|
@ -884,12 +863,12 @@ namespace Tests
|
||||||
[Test]
|
[Test]
|
||||||
public void EngineSendMessageDelayedIgnoringTimeDilation()
|
public void EngineSendMessageDelayedIgnoringTimeDilation()
|
||||||
{
|
{
|
||||||
Array.Clear(resultMessages, 0, resultMessages.Length);
|
resultMessages.Clear();
|
||||||
|
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new ActivateTimeDilationEngine());
|
worldBuilder.AddEngine(new ActivateTimeDilationEngine());
|
||||||
worldBuilder.AddEngine(new DelayedMessageIgnoringTimeDilationEngine());
|
worldBuilder.AddEngine(new DelayedMessageIgnoringTimeDilationEngine());
|
||||||
worldBuilder.AddEngine(new ReadMessagesEngine());
|
worldBuilder.AddEngine(new MessageReadEngine());
|
||||||
|
|
||||||
var entity = worldBuilder.CreateEntity();
|
var entity = worldBuilder.CreateEntity();
|
||||||
worldBuilder.SetComponent(entity, new MockComponent { });
|
worldBuilder.SetComponent(entity, new MockComponent { });
|
||||||
|
@ -917,7 +896,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var message in ReadMessages<MockMessage>())
|
foreach (var message in ReadMessages<MockMessage>())
|
||||||
{
|
{
|
||||||
var entity = CreateEntity();
|
var entity = CreateEntity();
|
||||||
SetComponent(entity, new MockComponent { });
|
SetComponent(entity, new MockComponent { });
|
||||||
|
@ -955,7 +934,7 @@ namespace Tests
|
||||||
resultComponents.Should().BeEmpty();
|
resultComponents.Should().BeEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DestroyComponentMessage { public Entity entity; }
|
struct DestroyComponentMessage : IMessage { public Entity entity; }
|
||||||
|
|
||||||
[Reads(typeof(MockComponent))]
|
[Reads(typeof(MockComponent))]
|
||||||
[Writes(typeof(MockComponent))]
|
[Writes(typeof(MockComponent))]
|
||||||
|
@ -982,7 +961,7 @@ namespace Tests
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ReadEntity()
|
public void GetEntityByComponentType()
|
||||||
{
|
{
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new ReadEntityByComponentTypeEngine());
|
worldBuilder.AddEngine(new ReadEntityByComponentTypeEngine());
|
||||||
|
@ -996,25 +975,14 @@ namespace Tests
|
||||||
entity.Should().BeEquivalentTo(readEntity);
|
entity.Should().BeEquivalentTo(readEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
struct MockComponentB : IComponent
|
||||||
public void ReadEntityThrowsWhenNoComponentOfTypeExists()
|
|
||||||
{
|
{
|
||||||
var worldBuilder = new WorldBuilder();
|
|
||||||
worldBuilder.AddEngine(new ReadEntityByComponentTypeEngine());
|
|
||||||
|
|
||||||
var world = worldBuilder.Build();
|
|
||||||
|
|
||||||
Assert.Throws<NoComponentOfTypeException>(() => world.Update(0.01), "No component of type MockComponent exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct MockComponentB
|
|
||||||
{
|
|
||||||
private int value;
|
|
||||||
|
|
||||||
public MockComponentB(int value)
|
public MockComponentB(int value)
|
||||||
{
|
{
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static MockComponentB getComponentResult;
|
static MockComponentB getComponentResult;
|
||||||
|
@ -1166,7 +1134,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (_, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
RemoveComponent<MockComponent>(entity);
|
RemoveComponent<MockComponent>(entity);
|
||||||
}
|
}
|
||||||
|
@ -1324,7 +1292,6 @@ namespace Tests
|
||||||
undilatedDeltaTime.Should().Be(0.5);
|
undilatedDeltaTime.Should().Be(0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Adds(typeof(MockComponent))]
|
|
||||||
class AddComponentWithoutPriorityEngine : Engine
|
class AddComponentWithoutPriorityEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
|
@ -1362,7 +1329,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ref readonly var entity = ref ReadEntity<MockComponent>();
|
var (component, entity) = ReadComponentIncludingEntity<MockComponent>();
|
||||||
|
|
||||||
AddComponent(entity, new MockComponent());
|
AddComponent(entity, new MockComponent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1381,7 +1349,6 @@ namespace Tests
|
||||||
Assert.Throws<IllegalWriteException>(() => world.Update(0.01));
|
Assert.Throws<IllegalWriteException>(() => world.Update(0.01));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Adds(typeof(MockComponentB))]
|
|
||||||
[WritesImmediate(typeof(MockComponentB))]
|
[WritesImmediate(typeof(MockComponentB))]
|
||||||
class AddImmediateComponentEngine : Engine
|
class AddImmediateComponentEngine : Engine
|
||||||
{
|
{
|
||||||
|
@ -1397,7 +1364,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
ref readonly var component = ref ReadComponent<MockComponentB>();
|
var (component, entity) = ReadComponentIncludingEntity<MockComponentB>();
|
||||||
|
|
||||||
getComponentResult = component;
|
getComponentResult = component;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1452,10 +1420,11 @@ namespace Tests
|
||||||
|
|
||||||
public class QueryTests
|
public class QueryTests
|
||||||
{
|
{
|
||||||
struct MockComponentB { }
|
struct MockComponentB : IComponent { }
|
||||||
struct MockComponentC { }
|
struct MockComponentC : IComponent { }
|
||||||
struct MockComponentD { }
|
struct MockComponentD : IComponent { }
|
||||||
|
|
||||||
|
[Reads(typeof(MockComponent), typeof(MockComponentB))]
|
||||||
[Writes(typeof(MockComponentB))]
|
[Writes(typeof(MockComponentB))]
|
||||||
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
|
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
|
||||||
class EntityQueryWithComponentsEngine : Engine
|
class EntityQueryWithComponentsEngine : Engine
|
||||||
|
@ -1509,6 +1478,7 @@ namespace Tests
|
||||||
queriedEntities.Should().BeEmpty();
|
queriedEntities.Should().BeEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Reads(typeof(MockComponent))]
|
||||||
[Writes(typeof(MockComponent))]
|
[Writes(typeof(MockComponent))]
|
||||||
[QueryWithout(typeof(MockComponent))]
|
[QueryWithout(typeof(MockComponent))]
|
||||||
class EntityQueryWithoutComponentsEngine : Engine
|
class EntityQueryWithoutComponentsEngine : Engine
|
||||||
|
@ -1562,6 +1532,7 @@ namespace Tests
|
||||||
queriedEntities.Should().BeEmpty();
|
queriedEntities.Should().BeEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Reads(typeof(MockComponent), typeof(MockComponentB), typeof(MockComponentD))]
|
||||||
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
|
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
|
||||||
[QueryWithout(typeof(MockComponentD))]
|
[QueryWithout(typeof(MockComponentD))]
|
||||||
class EntityQueryWithandWithoutComponentsEngine : Engine
|
class EntityQueryWithandWithoutComponentsEngine : Engine
|
||||||
|
@ -1787,6 +1758,7 @@ namespace Tests
|
||||||
}
|
}
|
||||||
|
|
||||||
[ReadsImmediate(typeof(MockComponentB))]
|
[ReadsImmediate(typeof(MockComponentB))]
|
||||||
|
[Reads(typeof(MockComponent))]
|
||||||
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
|
[QueryWith(typeof(MockComponent), typeof(MockComponentB))]
|
||||||
class EntityQueryWithImmediateAndNonImmediateComponents : Engine
|
class EntityQueryWithImmediateAndNonImmediateComponents : Engine
|
||||||
{
|
{
|
||||||
|
@ -1839,7 +1811,7 @@ namespace Tests
|
||||||
}
|
}
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
_components.AddRange(ReadComponents<MockComponentB>().ToArray());
|
_components.AddRange(ReadComponents<MockComponentB>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1875,11 +1847,12 @@ namespace Tests
|
||||||
|
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockComponent>())
|
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>())
|
||||||
{
|
{
|
||||||
if (HasComponent<MockComponentB>(entity))
|
if (HasComponent<MockComponentB>(entity))
|
||||||
{
|
{
|
||||||
_components.Add(GetComponent<MockComponentB>(entity));
|
_components.Add(GetComponent<MockComponentB>(entity));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1904,14 +1877,14 @@ namespace Tests
|
||||||
_components.Should().NotBeEmpty();
|
_components.Should().NotBeEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MockTimerComponent
|
struct MockTimerComponent : IComponent
|
||||||
{
|
{
|
||||||
public double Timer { get; }
|
|
||||||
|
|
||||||
public MockTimerComponent(double time)
|
public MockTimerComponent(double time)
|
||||||
{
|
{
|
||||||
Timer = time;
|
Timer = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double Timer { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
[Reads(typeof(MockTimerComponent))]
|
[Reads(typeof(MockTimerComponent))]
|
||||||
|
@ -1920,18 +1893,19 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var entity in ReadEntities<MockTimerComponent>())
|
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockTimerComponent>())
|
||||||
{
|
{
|
||||||
ref readonly var component = ref GetComponent<MockTimerComponent>(entity);
|
var updatedComponent = component;
|
||||||
|
updatedComponent.Timer -= dt;
|
||||||
|
|
||||||
if (component.Timer - dt <= 0)
|
if (updatedComponent.Timer <= 0)
|
||||||
{
|
{
|
||||||
RemoveComponent<MockTimerComponent>(entity);
|
RemoveComponent<MockTimerComponent>(entity);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SetComponent<MockTimerComponent>(entity, new MockTimerComponent(component.Timer - dt));
|
SetComponent<MockTimerComponent>(entity, updatedComponent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,18 +5,17 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public static class GeneralRendererTest
|
public static class GeneralRendererTest
|
||||||
{
|
{
|
||||||
struct AComponent { }
|
struct AComponent : IComponent { }
|
||||||
|
|
||||||
public class SingletonRead
|
public class SingletonRead
|
||||||
{
|
{
|
||||||
static (AComponent, Entity) result;
|
static (AComponent, Entity) result;
|
||||||
|
|
||||||
class TestRenderer : Renderer
|
class TestRenderer : GeneralRenderer
|
||||||
{
|
{
|
||||||
public override void Render(double dt, double alpha)
|
public override void Render()
|
||||||
{
|
{
|
||||||
ref readonly var entity = ref ReadEntity<AComponent>();
|
result = ReadComponentIncludingEntity<AComponent>();
|
||||||
result = (GetComponent<AComponent>(entity), entity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +23,7 @@ namespace Tests
|
||||||
public void SingletonComponent()
|
public void SingletonComponent()
|
||||||
{
|
{
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddRenderer(new TestRenderer());
|
worldBuilder.AddGeneralRenderer(new TestRenderer(), 1);
|
||||||
|
|
||||||
AComponent aComponent;
|
AComponent aComponent;
|
||||||
|
|
||||||
|
@ -34,10 +33,10 @@ namespace Tests
|
||||||
var world = worldBuilder.Build();
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
world.Update(0.01f);
|
world.Update(0.01f);
|
||||||
world.Draw(0.01f, 0);
|
world.Draw();
|
||||||
|
|
||||||
world.Update(0.01);
|
world.Update(0.01);
|
||||||
world.Draw(0.01f, 0);
|
world.Draw();
|
||||||
|
|
||||||
Assert.That(result, Is.EqualTo((aComponent, entity)));
|
Assert.That(result, Is.EqualTo((aComponent, entity)));
|
||||||
}
|
}
|
||||||
|
@ -46,7 +45,7 @@ namespace Tests
|
||||||
public void MultipleComponents()
|
public void MultipleComponents()
|
||||||
{
|
{
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddRenderer(new TestRenderer());
|
worldBuilder.AddGeneralRenderer(new TestRenderer(), 1);
|
||||||
|
|
||||||
AComponent aComponent;
|
AComponent aComponent;
|
||||||
AComponent aComponentTwo;
|
AComponent aComponentTwo;
|
||||||
|
@ -59,10 +58,10 @@ namespace Tests
|
||||||
var world = worldBuilder.Build();
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
world.Update(0.01f);
|
world.Update(0.01f);
|
||||||
world.Draw(0.01f, 0);
|
world.Draw();
|
||||||
|
|
||||||
world.Update(0.01f);
|
world.Update(0.01f);
|
||||||
world.Draw(0.01f, 0);
|
world.Draw();
|
||||||
|
|
||||||
Assert.That(result, Is.EqualTo((aComponent, entity)).Or.EqualTo((aComponentTwo, entityB)));
|
Assert.That(result, Is.EqualTo((aComponent, entity)).Or.EqualTo((aComponentTwo, entityB)));
|
||||||
}
|
}
|
|
@ -0,0 +1,109 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Encompass;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Tests
|
||||||
|
{
|
||||||
|
public class OrderedRendererTest
|
||||||
|
{
|
||||||
|
struct AComponent : IComponent { }
|
||||||
|
struct BComponent : IComponent { }
|
||||||
|
struct CComponent : IComponent { }
|
||||||
|
|
||||||
|
struct TestDrawComponent : IComponent, IDrawableComponent
|
||||||
|
{
|
||||||
|
public int Layer { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestRenderer : OrderedRenderer<TestDrawComponent>
|
||||||
|
{
|
||||||
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool called = false;
|
||||||
|
class DeactivatedRenderer : TestRenderer
|
||||||
|
{
|
||||||
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent)
|
||||||
|
{
|
||||||
|
called = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool calledOnDraw = false;
|
||||||
|
static (TestDrawComponent, Entity) resultComponent;
|
||||||
|
|
||||||
|
class CalledRenderer : OrderedRenderer<TestDrawComponent>
|
||||||
|
{
|
||||||
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent)
|
||||||
|
{
|
||||||
|
resultComponent = (testDrawComponent, entity);
|
||||||
|
calledOnDraw = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RenderMethodCalledOnWorldDraw()
|
||||||
|
{
|
||||||
|
var worldBuilder = new WorldBuilder();
|
||||||
|
worldBuilder.RegisterDrawLayer(2);
|
||||||
|
|
||||||
|
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
|
||||||
|
|
||||||
|
AComponent aComponent;
|
||||||
|
CComponent cComponent;
|
||||||
|
|
||||||
|
var testDrawComponent = new TestDrawComponent { Layer = 2 };
|
||||||
|
|
||||||
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
worldBuilder.SetComponent(entity, aComponent);
|
||||||
|
worldBuilder.SetComponent(entity, cComponent);
|
||||||
|
worldBuilder.SetComponent(entity, testDrawComponent);
|
||||||
|
|
||||||
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
|
world.Update(0.01f);
|
||||||
|
world.Draw();
|
||||||
|
|
||||||
|
Assert.IsTrue(calledOnDraw);
|
||||||
|
resultComponent.Should().BeEquivalentTo((testDrawComponent, entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Reads(typeof(TestDrawComponent))]
|
||||||
|
class DestroyerEngine : Engine
|
||||||
|
{
|
||||||
|
public override void Update(double dt)
|
||||||
|
{
|
||||||
|
foreach (var entity in ReadEntities<TestDrawComponent>())
|
||||||
|
{
|
||||||
|
Destroy(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void RenderMethodNotCalledAfterDestroy()
|
||||||
|
{
|
||||||
|
calledOnDraw = false;
|
||||||
|
|
||||||
|
var worldBuilder = new WorldBuilder();
|
||||||
|
worldBuilder.RegisterDrawLayer(1);
|
||||||
|
|
||||||
|
worldBuilder.AddEngine(new DestroyerEngine());
|
||||||
|
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
|
||||||
|
|
||||||
|
TestDrawComponent testDrawComponent = new TestDrawComponent { Layer = 1 };
|
||||||
|
|
||||||
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
worldBuilder.SetComponent(entity, testDrawComponent);
|
||||||
|
|
||||||
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
|
world.Update(0.01);
|
||||||
|
world.Draw();
|
||||||
|
|
||||||
|
Assert.IsFalse(calledOnDraw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,8 +5,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public class SpawnerTest
|
public class SpawnerTest
|
||||||
{
|
{
|
||||||
struct TestComponent { }
|
struct TestComponent : IComponent { }
|
||||||
struct SpawnMessageA { }
|
struct SpawnMessageA : IMessage { }
|
||||||
|
|
||||||
static Entity resultEntity;
|
static Entity resultEntity;
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ namespace Tests
|
||||||
[Writes(typeof(TestComponent))]
|
[Writes(typeof(TestComponent))]
|
||||||
class TestSpawner : Spawner<SpawnMessageA>
|
class TestSpawner : Spawner<SpawnMessageA>
|
||||||
{
|
{
|
||||||
protected override void Spawn(in SpawnMessageA message)
|
protected override void Spawn(SpawnMessageA message)
|
||||||
{
|
{
|
||||||
resultEntity = CreateEntity();
|
resultEntity = CreateEntity();
|
||||||
SetComponent(resultEntity, new TestComponent());
|
SetComponent(resultEntity, new TestComponent());
|
||||||
|
|
|
@ -5,7 +5,6 @@ using System.Collections.Generic;
|
||||||
using Encompass.Exceptions;
|
using Encompass.Exceptions;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Tests
|
namespace Tests
|
||||||
{
|
{
|
||||||
|
@ -13,8 +12,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public class EngineCycleSimple
|
public class EngineCycleSimple
|
||||||
{
|
{
|
||||||
struct AMessage { }
|
struct AMessage : IMessage { }
|
||||||
struct BMessage { }
|
struct BMessage : IMessage { }
|
||||||
|
|
||||||
[Receives(typeof(AMessage))]
|
[Receives(typeof(AMessage))]
|
||||||
[Sends(typeof(BMessage))]
|
[Sends(typeof(BMessage))]
|
||||||
|
@ -51,10 +50,10 @@ namespace Tests
|
||||||
|
|
||||||
public class EngineCycleComplex
|
public class EngineCycleComplex
|
||||||
{
|
{
|
||||||
struct AMessage { }
|
struct AMessage : IMessage { }
|
||||||
struct BMessage { }
|
struct BMessage : IMessage { }
|
||||||
struct CMessage { }
|
struct CMessage : IMessage { }
|
||||||
struct DMessage { }
|
struct DMessage : IMessage { }
|
||||||
|
|
||||||
[Receives(typeof(AMessage))]
|
[Receives(typeof(AMessage))]
|
||||||
[Sends(typeof(BMessage))]
|
[Sends(typeof(BMessage))]
|
||||||
|
@ -115,7 +114,7 @@ namespace Tests
|
||||||
|
|
||||||
public class MultipleEngineWriteConflict
|
public class MultipleEngineWriteConflict
|
||||||
{
|
{
|
||||||
struct AComponent { }
|
struct AComponent : IComponent { }
|
||||||
|
|
||||||
[Writes(typeof(AComponent))]
|
[Writes(typeof(AComponent))]
|
||||||
class AEngine : Engine
|
class AEngine : Engine
|
||||||
|
@ -142,12 +141,12 @@ namespace Tests
|
||||||
|
|
||||||
public class MultipleEngineWriteWithPriority
|
public class MultipleEngineWriteWithPriority
|
||||||
{
|
{
|
||||||
struct SetMessage
|
struct SetMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AComponent
|
struct AComponent : IComponent
|
||||||
{
|
{
|
||||||
public int myInt;
|
public int myInt;
|
||||||
}
|
}
|
||||||
|
@ -159,7 +158,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
|
foreach (var setMessage in ReadMessages<SetMessage>())
|
||||||
{
|
{
|
||||||
SetComponent(setMessage.entity, new AComponent { myInt = 0 });
|
SetComponent(setMessage.entity, new AComponent { myInt = 0 });
|
||||||
}
|
}
|
||||||
|
@ -173,7 +172,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
|
foreach (var setMessage in ReadMessages<SetMessage>())
|
||||||
{
|
{
|
||||||
SetComponent(setMessage.entity, new AComponent { myInt = 1 });
|
SetComponent(setMessage.entity, new AComponent { myInt = 1 });
|
||||||
}
|
}
|
||||||
|
@ -211,12 +210,12 @@ namespace Tests
|
||||||
|
|
||||||
public class DefaultWritePriority
|
public class DefaultWritePriority
|
||||||
{
|
{
|
||||||
struct SetMessage
|
struct SetMessage : IMessage
|
||||||
{
|
{
|
||||||
public Entity entity;
|
public Entity entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AComponent
|
struct AComponent : IComponent
|
||||||
{
|
{
|
||||||
public int myInt;
|
public int myInt;
|
||||||
}
|
}
|
||||||
|
@ -229,7 +228,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
|
foreach (var setMessage in ReadMessages<SetMessage>())
|
||||||
{
|
{
|
||||||
SetComponent(setMessage.entity, new AComponent { myInt = 5 });
|
SetComponent(setMessage.entity, new AComponent { myInt = 5 });
|
||||||
}
|
}
|
||||||
|
@ -244,7 +243,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
|
foreach (var setMessage in ReadMessages<SetMessage>())
|
||||||
{
|
{
|
||||||
SetComponent(setMessage.entity, new AComponent { myInt = 1 });
|
SetComponent(setMessage.entity, new AComponent { myInt = 1 });
|
||||||
}
|
}
|
||||||
|
@ -258,7 +257,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
|
foreach (var setMessage in ReadMessages<SetMessage>())
|
||||||
{
|
{
|
||||||
SetComponent(setMessage.entity, new AComponent { myInt = 3 });
|
SetComponent(setMessage.entity, new AComponent { myInt = 3 });
|
||||||
}
|
}
|
||||||
|
@ -298,7 +297,7 @@ namespace Tests
|
||||||
|
|
||||||
public class EngineMessageSelfCycle
|
public class EngineMessageSelfCycle
|
||||||
{
|
{
|
||||||
struct AMessage { }
|
struct AMessage : IMessage { }
|
||||||
|
|
||||||
[Receives(typeof(AMessage))]
|
[Receives(typeof(AMessage))]
|
||||||
[Sends(typeof(AMessage))]
|
[Sends(typeof(AMessage))]
|
||||||
|
@ -319,6 +318,50 @@ namespace Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class IllegalReadType
|
||||||
|
{
|
||||||
|
struct ANonMessage { }
|
||||||
|
|
||||||
|
[Reads(typeof(ANonMessage))]
|
||||||
|
class MyEngine : Engine
|
||||||
|
{
|
||||||
|
public override void Update(double dt)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ThrowsError()
|
||||||
|
{
|
||||||
|
var worldBuilder = new WorldBuilder();
|
||||||
|
|
||||||
|
Assert.Throws<IllegalReadTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IllegalWriteType
|
||||||
|
{
|
||||||
|
struct ANonMessage { }
|
||||||
|
|
||||||
|
[Sends(typeof(ANonMessage))]
|
||||||
|
class MyEngine : Engine
|
||||||
|
{
|
||||||
|
public override void Update(double dt)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ThrowsError()
|
||||||
|
{
|
||||||
|
var worldBuilder = new WorldBuilder();
|
||||||
|
|
||||||
|
Assert.Throws<IllegalSendTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class PriorityConflict
|
public class PriorityConflict
|
||||||
{
|
{
|
||||||
[Writes(typeof(MockComponent), 2)]
|
[Writes(typeof(MockComponent), 2)]
|
||||||
|
@ -385,13 +428,13 @@ namespace Tests
|
||||||
{
|
{
|
||||||
static List<Engine> order = new List<Engine>();
|
static List<Engine> order = new List<Engine>();
|
||||||
|
|
||||||
struct AComponent { }
|
struct AComponent : IComponent { }
|
||||||
struct BComponent { }
|
struct BComponent : IComponent { }
|
||||||
|
|
||||||
struct AMessage { }
|
struct AMessage : IMessage { }
|
||||||
struct BMessage { }
|
struct BMessage : IMessage { }
|
||||||
struct CMessage { }
|
struct CMessage : IMessage { }
|
||||||
struct DMessage { }
|
struct DMessage : IMessage { }
|
||||||
|
|
||||||
[Sends(typeof(AMessage))]
|
[Sends(typeof(AMessage))]
|
||||||
class AEngine : Engine
|
class AEngine : Engine
|
||||||
|
@ -458,22 +501,20 @@ namespace Tests
|
||||||
Assert.That(order.IndexOf(engineC), Is.LessThan(order.IndexOf(engineD)));
|
Assert.That(order.IndexOf(engineC), Is.LessThan(order.IndexOf(engineD)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static AMessage[] resultMessages;
|
static List<AMessage> resultMessages = new List<AMessage>();
|
||||||
|
|
||||||
[Receives(typeof(AMessage))]
|
[Receives(typeof(AMessage))]
|
||||||
class ReadMessageEngine : Engine
|
class ReadMessageEngine : Engine
|
||||||
{
|
{
|
||||||
public override void Update(double dt)
|
public override void Update(double dt)
|
||||||
{
|
{
|
||||||
resultMessages = ReadMessages<AMessage>().ToArray();
|
resultMessages = ReadMessages<AMessage>().ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void SendMessageDelayed()
|
public void SendMessageDelayed()
|
||||||
{
|
{
|
||||||
resultMessages = Array.Empty<AMessage>();
|
|
||||||
|
|
||||||
var worldBuilder = new WorldBuilder();
|
var worldBuilder = new WorldBuilder();
|
||||||
worldBuilder.AddEngine(new ReadMessageEngine());
|
worldBuilder.AddEngine(new ReadMessageEngine());
|
||||||
|
|
||||||
|
@ -498,8 +539,8 @@ namespace Tests
|
||||||
{
|
{
|
||||||
static List<Engine> order = new List<Engine>();
|
static List<Engine> order = new List<Engine>();
|
||||||
|
|
||||||
struct AMessage { }
|
struct AMessage : IMessage { }
|
||||||
struct BMessage { }
|
struct BMessage : IMessage { }
|
||||||
|
|
||||||
[Sends(typeof(AMessage), typeof(BMessage))]
|
[Sends(typeof(AMessage), typeof(BMessage))]
|
||||||
class AEngine : Engine
|
class AEngine : Engine
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
using NUnit.Framework;
|
||||||
|
using FluentAssertions;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using Encompass;
|
||||||
|
|
||||||
|
namespace Tests
|
||||||
|
{
|
||||||
|
public class WorldTest
|
||||||
|
{
|
||||||
|
struct TestComponent : IComponent { }
|
||||||
|
struct TestDrawComponent : IComponent, IDrawableComponent
|
||||||
|
{
|
||||||
|
public int Layer { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<object> drawOrder = new List<object>();
|
||||||
|
|
||||||
|
class TestEntityRenderer : OrderedRenderer<TestDrawComponent>
|
||||||
|
{
|
||||||
|
public override void Render(Entity entity, TestDrawComponent testDrawComponent)
|
||||||
|
{
|
||||||
|
drawOrder.Add(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestGeneralRenderer : GeneralRenderer
|
||||||
|
{
|
||||||
|
public override void Render()
|
||||||
|
{
|
||||||
|
drawOrder.Add(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DrawOrder()
|
||||||
|
{
|
||||||
|
var worldBuilder = new WorldBuilder();
|
||||||
|
worldBuilder.RegisterDrawLayer(1);
|
||||||
|
worldBuilder.RegisterDrawLayer(2);
|
||||||
|
worldBuilder.RegisterDrawLayer(3);
|
||||||
|
worldBuilder.RegisterDrawLayer(4);
|
||||||
|
worldBuilder.RegisterDrawLayer(7);
|
||||||
|
|
||||||
|
worldBuilder.AddOrderedRenderer(new TestEntityRenderer());
|
||||||
|
var testGeneralRenderer = worldBuilder.AddGeneralRenderer(new TestGeneralRenderer(), 7);
|
||||||
|
|
||||||
|
TestComponent testComponent;
|
||||||
|
TestDrawComponent drawComponentThree = new TestDrawComponent { Layer = 3 };
|
||||||
|
var drawComponentTwo = new TestDrawComponent { Layer = 2 };
|
||||||
|
var drawComponentOne = new TestDrawComponent { Layer = 1 };
|
||||||
|
var drawComponentFour = new TestDrawComponent { Layer = 4 };
|
||||||
|
|
||||||
|
var entity = worldBuilder.CreateEntity();
|
||||||
|
worldBuilder.SetComponent(entity, testComponent);
|
||||||
|
worldBuilder.SetComponent(entity, drawComponentThree);
|
||||||
|
|
||||||
|
var entityTwo = worldBuilder.CreateEntity();
|
||||||
|
worldBuilder.SetComponent(entityTwo, testComponent);
|
||||||
|
worldBuilder.SetComponent(entityTwo, drawComponentTwo);
|
||||||
|
|
||||||
|
var entityThree = worldBuilder.CreateEntity();
|
||||||
|
worldBuilder.SetComponent(entityThree, testComponent);
|
||||||
|
worldBuilder.SetComponent(entityThree, drawComponentThree);
|
||||||
|
|
||||||
|
var entityFour = worldBuilder.CreateEntity();
|
||||||
|
worldBuilder.SetComponent(entityFour, testComponent);
|
||||||
|
worldBuilder.SetComponent(entityFour, drawComponentFour);
|
||||||
|
|
||||||
|
var world = worldBuilder.Build();
|
||||||
|
|
||||||
|
world.Update(0.01f);
|
||||||
|
world.Draw();
|
||||||
|
|
||||||
|
drawOrder.Should().BeEquivalentTo(entityFour, entityTwo, entity, entityThree, testGeneralRenderer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,10 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<RootNamespace>Tests</RootNamespace>
|
<RootNamespace>Tests</RootNamespace>
|
||||||
<AssemblyName>EncompassECS.Framework.Tests</AssemblyName>
|
<AssemblyName>EncompassECS.Framework.Tests</AssemblyName>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
|
||||||
<Platforms>x64;x86</Platforms>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="5.7.0" />
|
<PackageReference Include="FluentAssertions" Version="5.7.0" />
|
||||||
|
|
Loading…
Reference in New Issue